From 2358a3ed8d2f9da7737bd94a96be187b3ab96d86 Mon Sep 17 00:00:00 2001 From: Corinne Winarski Date: Wed, 7 Feb 2018 19:39:00 -0500 Subject: [PATCH 1/4] Finished First Part --- .../mastering_loops/NumberUtilities.java | 72 +++++++++++++++++-- .../mastering_loops/NumberUtilitiesTest.java | 4 +- 2 files changed, 69 insertions(+), 7 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..18c9e55 100644 --- a/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java +++ b/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java @@ -3,26 +3,88 @@ public class NumberUtilities { public static String getEvenNumbers(int start, int stop) { - return null; + + StringBuilder even = new StringBuilder(); + + + if (start %2 != 0){ + + start += 1; + + } + + for (int i = start; i < stop; i += 2){ + + even.append(i); + } + + return even.toString(); } public static String getOddNumbers(int start, int stop) { - return null; + + StringBuilder odd = new StringBuilder(); + + if (start %2 == 0){ + + start -= 1; + + } + + for (int i = start; i < stop; i += 2){ + + odd.append(i); + } + + return odd.toString(); } public static String getSquareNumbers(int start, int stop, int step) { - return null; + + StringBuilder square = new StringBuilder(); + + for (int i = start; i < stop; i += step) { + + int sqrRoot = (int) Math.pow(i,2); + square.append(sqrRoot); + } + + return square.toString(); + } public static String getRange(int start, int stop, int step) { - return null; + + StringBuilder range = new StringBuilder(); + + for (int i = start; i < stop; i += step){ + + range.append(i); + } + + return range.toString(); + } public static String getExponentiations(int start, int stop, int step, int exponent) { - return null; + + StringBuilder expon = new StringBuilder(); + + for (int i = start; i < stop; i += step){ + + int exponVar = exponent; + + int startExpon = (int) Math.pow(i, exponVar); + + expon.append(startExpon); + + } + + return expon.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 e579b43..5ce91f7 100644 --- a/src/test/java/io/zipcoder/microlabs/mastering_loops/NumberUtilitiesTest.java +++ b/src/test/java/io/zipcoder/microlabs/mastering_loops/NumberUtilitiesTest.java @@ -51,7 +51,7 @@ public void testGetRange2() { @Test public void testGetEvenNumbers() { // : Given - String expected = "5791113151719"; + String expected = "681012141618"; int start = 5; int stop = 20; @@ -65,7 +65,7 @@ public void testGetEvenNumbers() { @Test public void testGetOddNumbers() { // : Given - String expected = "681012141618"; + String expected = "5791113151719"; int start = 5; int stop = 20; int step = 5; From dfa35b1f7cd3a3c95c26a83e828f9610a9d131b4 Mon Sep 17 00:00:00 2001 From: Corinne Winarski Date: Wed, 7 Feb 2018 20:59:28 -0500 Subject: [PATCH 2/4] Finished Second Part --- .../mastering_loops/TableUtilities.java | 48 +++++++++++++++++-- 1 file changed, 45 insertions(+), 3 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 e27a826..3fa6030 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,56 @@ public class TableUtilities { public static String getSmallMultiplicationTable() { - return null; + + StringBuilder getSmol = new StringBuilder(); + + for (int r = 1; r <= 5; r++){ + + for(int c = 1; c <= 5; c++){ + + getSmol.append(String.format("%3d |", r*c)); + + } + + getSmol.append("\n"); + } + + return getSmol.toString(); } public static String getLargeMultiplicationTable() { - return null; + + StringBuilder getBig = new StringBuilder(); + + for (int r = 1; r <= 10; r++){ + + for(int c = 1; c <= 10; c++){ + + getBig.append(String.format("%3d |", r*c)); + + } + + getBig.append("\n"); + } + + return getBig.toString(); } public static String getMultiplicationTable(int tableSize) { - return null; + + StringBuilder getSmol = new StringBuilder(); + + for (int r = 1; r <= tableSize; r++){ + + for(int c = 1; c <= tableSize; c++){ + + getSmol.append(String.format("%3d |", r*c)); + + } + + getSmol.append("\n"); + } + + return getSmol.toString(); } } From 1262dfa99de533e46ee15f8753ab4b3ea4549596 Mon Sep 17 00:00:00 2001 From: Corinne Winarski Date: Wed, 7 Feb 2018 22:14:33 -0500 Subject: [PATCH 3/4] Finished the Lab --- .../mastering_loops/TriangleUtilities.java | 53 +++++++++++++++++-- 1 file changed, 49 insertions(+), 4 deletions(-) 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..0855dbd 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,63 @@ public class TriangleUtilities { public static String getTriangle(int numberOfRows) { - return null; + + String getTriangle = ""; + + for (int row = 1; row < numberOfRows; row++){ + + for (int column = 0; column < row; column++){ + + getTriangle += "*"; + } + getTriangle += "\n"; + } + + return getTriangle; } public static String getRow(int numberOfStars) { - return null; + + String getRow = ""; + + for (int row = 0; row < numberOfStars; row++){ + + getRow += "*"; + } + + return getRow; + } public static String getSmallTriangle() { - return null; + + String triangle = ""; + + for (int row = 1; row <= 4; row++){ + + for (int column = 0; column < row; column++){ + + triangle += "*"; + } + triangle += "\n"; + } + + return triangle; } public static String getLargeTriangle() { - return null; + + String largeTriangle = ""; + + for (int row = 1; row <= 9; row++){ + + for (int column = 0; column < row; column++){ + + largeTriangle += "*"; + } + largeTriangle += "\n"; + } + + return largeTriangle; } } From 15da80dd5ddb14971ee4de731f3fe1f10971f7a2 Mon Sep 17 00:00:00 2001 From: Corinne Winarski Date: Thu, 8 Feb 2018 12:00:20 -0500 Subject: [PATCH 4/4] First Refactor --- .../mastering_loops/TableUtilities.java | 30 +------------ .../mastering_loops/TriangleUtilities.java | 44 ++++--------------- 2 files changed, 10 insertions(+), 64 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 3fa6030..0fdb5e6 100644 --- a/src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java +++ b/src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java @@ -3,38 +3,12 @@ public class TableUtilities { public static String getSmallMultiplicationTable() { - StringBuilder getSmol = new StringBuilder(); - - for (int r = 1; r <= 5; r++){ - - for(int c = 1; c <= 5; c++){ - - getSmol.append(String.format("%3d |", r*c)); - - } - - getSmol.append("\n"); - } - - return getSmol.toString(); + return getMultiplicationTable(5); } public static String getLargeMultiplicationTable() { - StringBuilder getBig = new StringBuilder(); - - for (int r = 1; r <= 10; r++){ - - for(int c = 1; c <= 10; c++){ - - getBig.append(String.format("%3d |", r*c)); - - } - - getBig.append("\n"); - } - - return getBig.toString(); + return getMultiplicationTable(10); } public static String getMultiplicationTable(int tableSize) { 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 0855dbd..49ab5e4 100644 --- a/src/main/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilities.java +++ b/src/main/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilities.java @@ -4,62 +4,34 @@ public class TriangleUtilities { public static String getTriangle(int numberOfRows) { - String getTriangle = ""; + String result = ""; - for (int row = 1; row < numberOfRows; row++){ - - for (int column = 0; column < row; column++){ - - getTriangle += "*"; - } - getTriangle += "\n"; + for (int iteration = 1; iteration < numberOfRows; iteration++) { + result += getRow(iteration) + "\n"; } - return getTriangle; + return result; } public static String getRow(int numberOfStars) { String getRow = ""; - for (int row = 0; row < numberOfStars; row++){ + for (int row = 0; row < numberOfStars; row++) { getRow += "*"; } return getRow; - + } public static String getSmallTriangle() { - - String triangle = ""; - - for (int row = 1; row <= 4; row++){ - - for (int column = 0; column < row; column++){ - - triangle += "*"; - } - triangle += "\n"; - } - - return triangle; + return getTriangle(5); } public static String getLargeTriangle() { - String largeTriangle = ""; - - for (int row = 1; row <= 9; row++){ - - for (int column = 0; column < row; column++){ - - largeTriangle += "*"; - } - largeTriangle += "\n"; - } - - return largeTriangle; + return getTriangle(10); } }