From 1fa9099301a1bfceabbad7c8af0c400781b56b76 Mon Sep 17 00:00:00 2001 From: glones Date: Mon, 27 Jul 2020 17:33:38 +0300 Subject: [PATCH 1/3] added solution --- src/main/java/core/basesyntax/BinaryString.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/java/core/basesyntax/BinaryString.java b/src/main/java/core/basesyntax/BinaryString.java index ebec6bf..dfb1614 100644 --- a/src/main/java/core/basesyntax/BinaryString.java +++ b/src/main/java/core/basesyntax/BinaryString.java @@ -7,6 +7,20 @@ public class BinaryString { * а возвращает String с представлением этого числа в двоичном виде. */ public String toBinaryString(int value) { - return null; + int residue; + String result = ""; + StringBuffer container = new StringBuffer(); + if (value == 0) { + return "0"; + } else { + while (value != 0) { + residue = value % 2; + container.append(residue); + value = value / 2; + } + } + container.reverse(); + result = container.toString(); + return result; } } From 5038117026e18e4db3ca305324a7a9f367ff124f Mon Sep 17 00:00:00 2001 From: glones Date: Mon, 27 Jul 2020 19:29:08 +0300 Subject: [PATCH 2/3] without StringBuffer and else --- pom.xml | 2 +- src/main/java/core/basesyntax/BinaryString.java | 17 +++++++---------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/pom.xml b/pom.xml index 0413b15..95093b8 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ 1.0-SNAPSHOT - 11 + 8 UTF-8 UTF-8 3.1.1 diff --git a/src/main/java/core/basesyntax/BinaryString.java b/src/main/java/core/basesyntax/BinaryString.java index dfb1614..b25c4c2 100644 --- a/src/main/java/core/basesyntax/BinaryString.java +++ b/src/main/java/core/basesyntax/BinaryString.java @@ -8,19 +8,16 @@ public class BinaryString { */ public String toBinaryString(int value) { int residue; - String result = ""; - StringBuffer container = new StringBuffer(); + StringBuilder container = new StringBuilder(); if (value == 0) { return "0"; - } else { - while (value != 0) { - residue = value % 2; - container.append(residue); - value = value / 2; - } + } + while (value != 0) { + residue = value % 2; + container.append(residue); + value = value / 2; } container.reverse(); - result = container.toString(); - return result; + return container.toString(); } } From 58bcc4b486e8dd7baf5265fbc58ce8b38a8226e4 Mon Sep 17 00:00:00 2001 From: glones Date: Tue, 28 Jul 2020 10:33:34 +0300 Subject: [PATCH 3/3] remove residue variable --- src/main/java/core/basesyntax/BinaryString.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/core/basesyntax/BinaryString.java b/src/main/java/core/basesyntax/BinaryString.java index b25c4c2..c2f7a67 100644 --- a/src/main/java/core/basesyntax/BinaryString.java +++ b/src/main/java/core/basesyntax/BinaryString.java @@ -7,14 +7,12 @@ public class BinaryString { * а возвращает String с представлением этого числа в двоичном виде. */ public String toBinaryString(int value) { - int residue; StringBuilder container = new StringBuilder(); if (value == 0) { return "0"; } while (value != 0) { - residue = value % 2; - container.append(residue); + container.append(value % 2); value = value / 2; } container.reverse();