From 62161ae215bc3e1bd8cd1a106b0d111a8fca225c Mon Sep 17 00:00:00 2001 From: Roman Date: Mon, 27 Jul 2020 17:44:52 +0300 Subject: [PATCH 1/2] added solution --- src/main/java/core/basesyntax/BinaryString.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main/java/core/basesyntax/BinaryString.java b/src/main/java/core/basesyntax/BinaryString.java index ebec6bf..ff60473 100644 --- a/src/main/java/core/basesyntax/BinaryString.java +++ b/src/main/java/core/basesyntax/BinaryString.java @@ -7,6 +7,15 @@ public class BinaryString { * а возвращает String с представлением этого числа в двоичном виде. */ public String toBinaryString(int value) { - return null; + if (value == 0) { + return "0"; + } + StringBuilder builder = new StringBuilder(); + while (value > 0) { + builder.append(value % 2); + value /= 2; + } + builder = builder.reverse(); + return builder.toString(); } } From dc86b7e59a98e2cc85ed65012a4503ca8e6a7843 Mon Sep 17 00:00:00 2001 From: Roman Date: Mon, 27 Jul 2020 18:18:53 +0300 Subject: [PATCH 2/2] rename object & code became smaller --- 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 ff60473..fcc3a86 100644 --- a/src/main/java/core/basesyntax/BinaryString.java +++ b/src/main/java/core/basesyntax/BinaryString.java @@ -10,12 +10,11 @@ public String toBinaryString(int value) { if (value == 0) { return "0"; } - StringBuilder builder = new StringBuilder(); + StringBuilder result = new StringBuilder(); while (value > 0) { - builder.append(value % 2); + result.append(value % 2); value /= 2; } - builder = builder.reverse(); - return builder.toString(); + return result.reverse().toString(); } }