From f2a6f25f0be02f3049905b6ad5e2d06e6daeb6d1 Mon Sep 17 00:00:00 2001 From: vvmk Date: Wed, 7 Feb 2018 15:35:20 -0500 Subject: [PATCH 1/9] original getRange tests pass --- .../microlabs/mastering_loops/NumberUtilities.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java b/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java index 5a742c4..2391310 100644 --- a/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java +++ b/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java @@ -3,7 +3,7 @@ public class NumberUtilities { public static String getEvenNumbers(int start, int stop) { - return null; + return null; } @@ -18,7 +18,11 @@ public static String getSquareNumbers(int start, int stop, int step) { public static String getRange(int start, int stop, int step) { - return null; + StringBuilder sb = new StringBuilder(); + for (int i=start;i Date: Wed, 7 Feb 2018 15:51:15 -0500 Subject: [PATCH 2/9] overload getRange, write tests, tests pass --- .../mastering_loops/NumberUtilities.java | 15 +++++++++++ .../mastering_loops/NumberUtilitiesTest.java | 25 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java b/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java index 2391310..0f29586 100644 --- a/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java +++ b/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java @@ -16,6 +16,21 @@ public static String getSquareNumbers(int start, int stop, int step) { return null; } + public static String getRange(int stop) { + StringBuilder sb = new StringBuilder(); + for (int i=0;i < stop;i++) { + sb.append(i); + } + return sb.toString(); + } + + public static String getRange(int start, int stop) { + StringBuilder sb = new StringBuilder(); + for (int i=start;i < stop;i++) { + sb.append(i); + } + return sb.toString(); + } public static String getRange(int start, int stop, int step) { StringBuilder sb = new StringBuilder(); diff --git a/src/test/java/io/zipcoder/microlabs/mastering_loops/NumberUtilitiesTest.java b/src/test/java/io/zipcoder/microlabs/mastering_loops/NumberUtilitiesTest.java index e579b43..dc6742c 100644 --- a/src/test/java/io/zipcoder/microlabs/mastering_loops/NumberUtilitiesTest.java +++ b/src/test/java/io/zipcoder/microlabs/mastering_loops/NumberUtilitiesTest.java @@ -109,4 +109,29 @@ public void testGetExponentiationNumbers() { Assert.assertEquals(expected, actual); } + + @Test + public void testGetRangeWithOneParam() { + // : Given + String expected = "0123456789101112131415161718"; + int stop = 19; + + // : When + String actual = NumberUtilities.getRange(stop); + + Assert.assertEquals(expected, actual); + } + + @Test + public void testGetRangeWithTwoParams() { + // : Given + String expected = "5678910"; + int start = 5; + int stop = 11; + + // : When + String actual = NumberUtilities.getRange(start, stop); + + Assert.assertEquals(expected, actual); + } } From 8946cb71dba20d92e6c126f1c4fb6b1f3bc4874c Mon Sep 17 00:00:00 2001 From: vvmk Date: Wed, 7 Feb 2018 16:14:45 -0500 Subject: [PATCH 3/9] fix even/odd tests --- .../microlabs/mastering_loops/NumberUtilities.java | 7 ++++++- .../microlabs/mastering_loops/NumberUtilitiesTest.java | 9 ++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java b/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java index 0f29586..004eea3 100644 --- a/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java +++ b/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java @@ -3,7 +3,12 @@ public class NumberUtilities { public static String getEvenNumbers(int start, int stop) { - return null; + StringBuilder sb = new StringBuilder(); + for (int i=start;i < stop;i++) { + if (i % 2 == 0) + sb.append(i); + } + return sb.toString(); } diff --git a/src/test/java/io/zipcoder/microlabs/mastering_loops/NumberUtilitiesTest.java b/src/test/java/io/zipcoder/microlabs/mastering_loops/NumberUtilitiesTest.java index dc6742c..bbce42f 100644 --- a/src/test/java/io/zipcoder/microlabs/mastering_loops/NumberUtilitiesTest.java +++ b/src/test/java/io/zipcoder/microlabs/mastering_loops/NumberUtilitiesTest.java @@ -49,29 +49,28 @@ public void testGetRange2() { @Test - public void testGetEvenNumbers() { + public void testGetOddNumbers() { // : Given String expected = "5791113151719"; int start = 5; int stop = 20; // : When - String actual = NumberUtilities.getEvenNumbers(start, stop); + String actual = NumberUtilities.getOddNumbers(start, stop); // : Then Assert.assertEquals(expected, actual); } @Test - public void testGetOddNumbers() { + public void testGetEvenNumbers() { // : Given String expected = "681012141618"; int start = 5; int stop = 20; - int step = 5; // : When - String actual = NumberUtilities.getOddNumbers(start, stop); + String actual = NumberUtilities.getEvenNumbers(start, stop); // : Then Assert.assertEquals(expected, actual); From c1068c1826362c167edefd16869c3ffdbac53b5b Mon Sep 17 00:00:00 2001 From: vvmk Date: Wed, 7 Feb 2018 16:59:28 -0500 Subject: [PATCH 4/9] misunderstood getSquareNumbers, but I like it. kept it, wrote a test --- .../mastering_loops/NumberUtilities.java | 25 ++++++++++++++++--- .../mastering_loops/NumberUtilitiesTest.java | 11 ++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java b/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java index 004eea3..8e47824 100644 --- a/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java +++ b/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java @@ -13,12 +13,31 @@ public static String getEvenNumbers(int start, int stop) { public static String getOddNumbers(int start, int stop) { - return null; + StringBuilder sb = new StringBuilder(); + for (int i=start;i < stop;i++) { + if (i % 2 != 0) { + sb.append(i); + } + } + return sb.toString(); } - public static String getSquareNumbers(int start, int stop, int step) { - return null; + StringBuilder sb = new StringBuilder(); + for (int i=start;i Date: Wed, 7 Feb 2018 17:05:13 -0500 Subject: [PATCH 5/9] all NumberUtilities tests pass --- .../zipcoder/microlabs/mastering_loops/NumberUtilities.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java b/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java index 8e47824..0b5eb43 100644 --- a/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java +++ b/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java @@ -66,6 +66,10 @@ public static String getRange(int start, int stop, int step) { public static String getExponentiations(int start, int stop, int step, int exponent) { - return null; + StringBuilder sb = new StringBuilder(); + for (int i = start;i Date: Wed, 7 Feb 2018 18:20:25 -0500 Subject: [PATCH 6/9] triangle tests pass, getEvenNumbers reduced to 1 line because Leon --- .../mastering_loops/NumberUtilities.java | 25 +++++++------------ .../mastering_loops/TriangleUtilities.java | 17 ++++++++++--- .../TriangleUtilitiesTest.java | 7 +++--- 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java b/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java index 0b5eb43..f688dfd 100644 --- a/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java +++ b/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java @@ -3,14 +3,15 @@ public class NumberUtilities { public static String getEvenNumbers(int start, int stop) { - StringBuilder sb = new StringBuilder(); - for (int i=start;i < stop;i++) { - if (i % 2 == 0) - sb.append(i); - } - return sb.toString(); + return getRange(start + (start % 2), stop, 2); } +/* //Kris wrote this, look at it later + public static String getEvenNumbersk(int start, int stop) { // + for ({String s = ""; int i=start;}; i < stop;i++) s = s + ((i % 2 == 0) ? i : ""); + return s; + } +*/ public static String getOddNumbers(int start, int stop) { StringBuilder sb = new StringBuilder(); @@ -41,19 +42,11 @@ public static String getPerfectSquares(int start, int stop) { } public static String getRange(int stop) { - StringBuilder sb = new StringBuilder(); - for (int i=0;i < stop;i++) { - sb.append(i); - } - return sb.toString(); + return getRange(0, stop, 1); } public static String getRange(int start, int stop) { - StringBuilder sb = new StringBuilder(); - for (int i=start;i < stop;i++) { - sb.append(i); - } - return sb.toString(); + return getRange(start, stop, 1); } public static String getRange(int start, int stop, int step) { diff --git a/src/main/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilities.java b/src/main/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilities.java index 0c7cd35..9268a2e 100644 --- a/src/main/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilities.java +++ b/src/main/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilities.java @@ -3,18 +3,27 @@ public class TriangleUtilities { public static String getTriangle(int numberOfRows) { - return null; + StringBuilder sb = new StringBuilder(); + for (int i=1;i<=numberOfRows;i++) { + sb.append(getRow((i))); + sb.append("\n"); + } + return sb.toString(); } public static String getRow(int numberOfStars) { - return null; + StringBuilder sb = new StringBuilder(); + for (int i=0;i Date: Wed, 7 Feb 2018 22:37:00 -0500 Subject: [PATCH 7/9] write table utils, all tests pass --- .../microlabs/mastering_loops/NumberUtilities.java | 8 +------- .../microlabs/mastering_loops/TableUtilities.java | 14 +++++++++++--- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java b/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java index f688dfd..84ef336 100644 --- a/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java +++ b/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java @@ -14,13 +14,7 @@ public static String getEvenNumbersk(int start, int stop) { // */ public static String getOddNumbers(int start, int stop) { - StringBuilder sb = new StringBuilder(); - for (int i=start;i < stop;i++) { - if (i % 2 != 0) { - sb.append(i); - } - } - return sb.toString(); + return getRange((start % 2 != 0) ? start : start+1, stop, 2); } public static String getSquareNumbers(int start, int stop, int step) { diff --git a/src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java b/src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java index e27a826..a5d6a0c 100644 --- a/src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java +++ b/src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java @@ -2,14 +2,22 @@ public class TableUtilities { public static String getSmallMultiplicationTable() { - return null; + return getMultiplicationTable(5); } public static String getLargeMultiplicationTable() { - return null; + return getMultiplicationTable(10); } public static String getMultiplicationTable(int tableSize) { - return null; + StringBuilder sb = new StringBuilder(); + for (int i = 1; i <= tableSize; i++) { + for (int j = 1; j <= tableSize; j++) { + int n = i * j; + sb.append(String.format("%1$3s |", n)); + } + sb.append("\n"); + } + return sb.toString(); } } From 89c9ee1f465f1bb938a592e012a317b1d35a72ef Mon Sep 17 00:00:00 2001 From: vvmk Date: Wed, 7 Feb 2018 22:39:38 -0500 Subject: [PATCH 8/9] nitpick --- .../io/zipcoder/microlabs/mastering_loops/TableUtilities.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java b/src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java index a5d6a0c..e053d64 100644 --- a/src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java +++ b/src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java @@ -13,8 +13,7 @@ public static String getMultiplicationTable(int tableSize) { StringBuilder sb = new StringBuilder(); for (int i = 1; i <= tableSize; i++) { for (int j = 1; j <= tableSize; j++) { - int n = i * j; - sb.append(String.format("%1$3s |", n)); + sb.append(String.format("%1$3s |", (i * j))); } sb.append("\n"); } From 113c619dcabeb2bc17a900f84945ba76b093c296 Mon Sep 17 00:00:00 2001 From: vvmk Date: Thu, 8 Feb 2018 22:27:53 -0500 Subject: [PATCH 9/9] getSquareNumbers calls getExponentiations --- .../zipcoder/microlabs/mastering_loops/NumberUtilities.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java b/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java index 84ef336..1b4dcc3 100644 --- a/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java +++ b/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java @@ -18,11 +18,7 @@ public static String getOddNumbers(int start, int stop) { } public static String getSquareNumbers(int start, int stop, int step) { - StringBuilder sb = new StringBuilder(); - for (int i=start;i