From 62cb85924ee6931c2fe8d6c06ecf2bf4d7205505 Mon Sep 17 00:00:00 2001 From: Borisko130 Date: Mon, 27 Jul 2020 18:18:23 +0300 Subject: [PATCH 1/3] Homework 3 completed --- src/main/java/core/basesyntax/BinaryString.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/main/java/core/basesyntax/BinaryString.java b/src/main/java/core/basesyntax/BinaryString.java index ebec6bf..b3d1d92 100644 --- a/src/main/java/core/basesyntax/BinaryString.java +++ b/src/main/java/core/basesyntax/BinaryString.java @@ -7,6 +7,17 @@ public class BinaryString { * а возвращает String с представлением этого числа в двоичном виде. */ public String toBinaryString(int value) { - return null; + StringBuilder binaryResult = new StringBuilder(); + + if (value == 0) { + return "0"; + } + + for (int i = 0; value > 0; i++) { + binaryResult.append(((value % 2) == 0 ? "0" : "1")); + value /= 2; + } + + return binaryResult.reverse().toString(); } } From 73744076495679448564d441b9bcb7831e1672ec Mon Sep 17 00:00:00 2001 From: Borisko130 Date: Mon, 27 Jul 2020 19:03:36 +0300 Subject: [PATCH 2/3] Removed line, changed loop and changed division --- src/main/java/core/basesyntax/BinaryString.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/core/basesyntax/BinaryString.java b/src/main/java/core/basesyntax/BinaryString.java index b3d1d92..788c933 100644 --- a/src/main/java/core/basesyntax/BinaryString.java +++ b/src/main/java/core/basesyntax/BinaryString.java @@ -8,14 +8,13 @@ public class BinaryString { */ public String toBinaryString(int value) { StringBuilder binaryResult = new StringBuilder(); - if (value == 0) { return "0"; } - for (int i = 0; value > 0; i++) { - binaryResult.append(((value % 2) == 0 ? "0" : "1")); - value /= 2; + while (value > 0) { + binaryResult.append((value % 2)); + value >>= 1; } return binaryResult.reverse().toString(); From 8c3bdcd39c22b73c4410e047dbaeb72452e8735c Mon Sep 17 00:00:00 2001 From: Borisko130 Date: Mon, 27 Jul 2020 19:05:08 +0300 Subject: [PATCH 3/3] Removed another unnecesary line --- src/main/java/core/basesyntax/BinaryString.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/core/basesyntax/BinaryString.java b/src/main/java/core/basesyntax/BinaryString.java index 788c933..60d3346 100644 --- a/src/main/java/core/basesyntax/BinaryString.java +++ b/src/main/java/core/basesyntax/BinaryString.java @@ -11,7 +11,6 @@ public String toBinaryString(int value) { if (value == 0) { return "0"; } - while (value > 0) { binaryResult.append((value % 2)); value >>= 1;