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..e4371ab 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,84 @@ public class NumberUtilities { public static String getEvenNumbers(int start, int stop) { - return null; + StringBuilder evenNumbers = new StringBuilder(); + for (int count = start; count < stop; count++) { + if (count % 2 == 0) { + evenNumbers.append(count); + + } + + } + + return evenNumbers.toString(); } public static String getOddNumbers(int start, int stop) { - return null; + + StringBuilder oddNumbers = new StringBuilder(); + for (int count = start; count < stop; count++) { + if (count % 2 == 1) { + oddNumbers.append(count); + + } + + } + + return oddNumbers.toString(); } public static String getSquareNumbers(int start, int stop, int step) { - return null; + StringBuilder squaredNumbers = new StringBuilder(); + for (int count = start; count < stop; count += step) { + + squaredNumbers.append(count * count); + + + } + + return squaredNumbers.toString(); } public static String getRange(int start, int stop, int step) { - return null; + StringBuilder rangeNumbers = new StringBuilder(); + for (int count = start; count < stop; count += step) { + + rangeNumbers.append(count); + + + } + + return rangeNumbers.toString(); } public static String getExponentiations(int start, int stop, int step, int exponent) { - return null; + StringBuilder expoNumbers = new StringBuilder(); + + for (int count = start; count < stop; count += step) { + int exp = count; + for (int count2 = 1; count2 < exponent; count2++) { + exp = exp * count; + + } + expoNumbers.append(exp); + + + } + + return expoNumbers.toString(); + } + + public static String getRange(int stop) { + String result = NumberUtilities.getRange(0, stop, 1); + return result; + } + + public static String getRange(int start, int stop) { + String result = NumberUtilities.getRange(start, stop, 1); + return result; } -} +} \ No newline at end of file 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..a906a6e 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,48 @@ public class TableUtilities { public static String getSmallMultiplicationTable() { - return null; + StringBuilder table = new StringBuilder(); + for (int count = 1; count <= 5; count++) { + for (int count2 = 1; count2 <= 5; count2++) { + String a = String.format("%3d |", count * count2); + table.append(a); + + } + table.append("\n"); + + } + + return table.toString(); } public static String getLargeMultiplicationTable() { - return null; + StringBuilder table = new StringBuilder(); + for (int count = 1; count <= 10; count++) { + for (int count2 = 1; count2 <= 10; count2++) { + String a = String.format("%3d |", count * count2); + table.append(a); + + } + table.append("\n"); + + } + + return table.toString(); } public static String getMultiplicationTable(int tableSize) { - return null; + StringBuilder table = new StringBuilder(); + for (int count = 1; count <= tableSize; count++) { + for (int count2 = 1; count2 <= tableSize; count2++) { + String a = String.format("%3d |", count * count2); + table.append(a); + + } + table.append("\n"); + + } + + return table.toString(); } } + 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..16792e1 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,47 @@ public class TriangleUtilities { public static String getTriangle(int numberOfRows) { - return null; + StringBuilder triangle = new StringBuilder(); + for (int count = 1; count < numberOfRows; count++) { + for (int count2 = 0; count2 < count; count2++) { + triangle.append("*"); + + } + triangle.append("\n"); + } + return triangle.toString(); } - public static String getRow(int numberOfStars) { - return null; + public static String getRow(int numberOfStars) { + StringBuilder row = new StringBuilder(); + for (int count = 0; count < numberOfStars; count++) { + row.append("*"); + } + + return row.toString(); } public static String getSmallTriangle() { - return null; + StringBuilder triangle = new StringBuilder(); + for (int count = 1; count < 5; count++) { + for (int count2 = 0; count2 < count; count2++) { + triangle.append("*"); + + } + triangle.append("\n"); + } + return triangle.toString(); } public static String getLargeTriangle() { - return null; + StringBuilder triangle = new StringBuilder(); + for (int count = 1; count < 11; count++) { + for (int count2 = 0; count2 < count; count2++) { + triangle.append("*"); + + } + triangle.append("\n"); + } + return triangle.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..f407190 100644 --- a/src/test/java/io/zipcoder/microlabs/mastering_loops/NumberUtilitiesTest.java +++ b/src/test/java/io/zipcoder/microlabs/mastering_loops/NumberUtilitiesTest.java @@ -35,6 +35,31 @@ public void testGetRange2() { Assert.assertEquals(expected, actual); } + @Test + public void testGetRange3() { + // : Given + String expected = "012345678910"; + int stop = 11; + + // : When + String actual = NumberUtilities.getRange(stop); + + // : Then + Assert.assertEquals(expected, actual); + } + @Test + public void testGetRange4() { + // : Given + String expected = "012345678910111213141516171819"; + int start = 0; + int stop = 20; + + // : When + String actual = NumberUtilities.getRange(start, stop); + + // : Then + Assert.assertEquals(expected, actual); + } @@ -51,7 +76,7 @@ public void testGetRange2() { @Test public void testGetEvenNumbers() { // : Given - String expected = "5791113151719"; + String expected = "681012141618"; int start = 5; int stop = 20; @@ -65,7 +90,7 @@ public void testGetEvenNumbers() { @Test public void testGetOddNumbers() { // : Given - String expected = "681012141618"; + String expected = "5791113151719"; int start = 5; int stop = 20; int step = 5; diff --git a/src/test/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilitiesTest.java b/src/test/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilitiesTest.java index 47846c2..5986adb 100644 --- a/src/test/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilitiesTest.java +++ b/src/test/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilitiesTest.java @@ -53,7 +53,8 @@ public void testGetLargeTriangle() { "******\n" + "*******\n" + "********\n" + - "*********\n"; + "*********\n" + + "**********\n"; String actual = TriangleUtilities.getLargeTriangle(); Assert.assertEquals(expected, actual); }