diff --git a/src/main/java/io/zipcoder/microlabs/mastering_loops/MainApp.java b/src/main/java/io/zipcoder/microlabs/mastering_loops/MainApp.java index 29e339b..b35c288 100644 --- a/src/main/java/io/zipcoder/microlabs/mastering_loops/MainApp.java +++ b/src/main/java/io/zipcoder/microlabs/mastering_loops/MainApp.java @@ -5,7 +5,10 @@ /** * Created by leon on 1/31/18. */ -public class MainApp { +public class MainApp extends TableUtilities{ public static void main(String[] args) { + TableUtilities result = new TableUtilities(); + System.out.print(result.getMultiplicationTable(20)); + } } 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 7d979d2..7575eb9 100644 --- a/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java +++ b/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java @@ -1,36 +1,66 @@ package io.zipcoder.microlabs.mastering_loops; - public class NumberUtilities { public static String getEvenNumbers(int start, int stop) { - return null; - } + StringBuilder numList = new StringBuilder(); + for (int i = start; i < stop; i++) { + if (i % 2 == 0) { + numList.append(i); + } + } + return numList.toString(); + } public static String getOddNumbers(int start, int stop) { - return null; - } + StringBuilder numList = new StringBuilder(); + for (int i = start; i < stop; i++) { + if (i % 2 != 0) { + numList.append(i); + } + } + return numList.toString(); + } public static String getSquareNumbers(int start, int stop, int step) { - return null; + StringBuilder numList = new StringBuilder(); + for (int i = start; i < stop; i += step) { + int x = (int) Math.pow(i, 2); + numList.append(String.valueOf(x)); + } + + return numList.toString(); } - public static String getRange(int start) { - return null; + public static String getRange(int stop) { + StringBuilder numList = new StringBuilder(); + for (int i = 0; i < stop; i++) + numList.append(String.valueOf(i)); + return numList.toString(); } public static String getRange(int start, int stop) { - return null; + StringBuilder numList = new StringBuilder(); + for (int i = start; i < stop; i++) + numList.append(String.valueOf(i)); + return numList.toString(); } - public static String getRange(int start, int stop, int step) { - return null; + StringBuilder numList = new StringBuilder(); + for (int i = start; i < stop; i+=step) + numList.append(String.valueOf(i)); + return numList.toString(); } - public static String getExponentiations(int start, int stop, int step, int exponent) { - return null; + StringBuilder numList = new StringBuilder(); + for (int i = start; i < stop; i += step) { + int x = (int) Math.pow(i, exponent); + numList.append(String.valueOf(x)); + } + + return numList.toString(); } -} +} \ 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..7773a9b 100644 --- a/src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java +++ b/src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java @@ -1,15 +1,53 @@ package io.zipcoder.microlabs.mastering_loops; public class TableUtilities { - public static String getSmallMultiplicationTable() { - return null; + /** + * + * @return a formatted multiplication table of various sizes + */ + + public String getSmallMultiplicationTable() { + StringBuilder smallTable = new StringBuilder(); + for (int i = 1; i < 6; i++) { + StringBuilder grow = new StringBuilder(); + for (int a = 1; a < 6; a++) { + int b = a * i; + grow.append(String.format("%3d", b)).append(" |"); + } + + smallTable.append(grow).append("\n"); + } + + return smallTable.toString(); } - public static String getLargeMultiplicationTable() { - return null; + public String getLargeMultiplicationTable() { + StringBuilder bigTable = new StringBuilder(); + for (int i = 1; i < 11; i++) { + StringBuilder grow = new StringBuilder(); + for (int a = 1; a < 11; a++) { + int b = a * i; + grow.append(String.format("%3d", b)).append(" |"); + } + + bigTable.append(grow).append("\n"); + } + + return bigTable.toString(); } - public static String getMultiplicationTable(int tableSize) { - return null; + public String getMultiplicationTable(int tableSize) { + StringBuilder table = new StringBuilder(); + for (int i = 1; i < 21; i++) { + StringBuilder grow = new StringBuilder(); + for (int a = 1; a < 21; a++) { + int b = a * i; + grow.append(String.format("%3d", b)).append(" |"); + } + + table.append(grow).append("\n"); + } + + return table.toString(); } -} +} \ No newline at end of file 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..aaded9b 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,52 @@ public class TriangleUtilities { public static String getTriangle(int numberOfRows) { - return null; + StringBuilder newTriangle = new StringBuilder("*\n"); + StringBuilder newLine = new StringBuilder("*"); + int counter = 1; + while (counter < numberOfRows - 1) { + newLine.append("*"); + newTriangle.append(newLine).append("\n"); + counter++; + } + + return newTriangle.toString(); } public static String getRow(int numberOfStars) { - return null; + StringBuilder star = new StringBuilder(); + int counter = 1; + while (counter <= numberOfStars) { + star.append("*"); + counter++; + } + + return star.toString(); } public static String getSmallTriangle() { - return null; + StringBuilder newTriangle = new StringBuilder("*\n"); + StringBuilder newLine = new StringBuilder("*"); + int counter = 1; + while (counter < 4) { + newLine.append("*"); + newTriangle.append(newLine).append("\n"); + counter++; + } + + return newTriangle.toString(); } public static String getLargeTriangle() { - return null; + StringBuilder newTriangle = new StringBuilder("*\n"); + StringBuilder newLine = new StringBuilder("*"); + int counter = 1; + while (counter < 9) { + newLine.append("*"); + newTriangle.append(newLine).append("\n"); + counter++; + } + + return newTriangle.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 9f65abc..bdfc96a 100644 --- a/src/test/java/io/zipcoder/microlabs/mastering_loops/NumberUtilitiesTest.java +++ b/src/test/java/io/zipcoder/microlabs/mastering_loops/NumberUtilitiesTest.java @@ -8,7 +8,7 @@ public class NumberUtilitiesTest { public void testGetRange1A() { // : Given String expected = "0123456789"; - int stop = 11; + int stop = 10; // : When String actual = NumberUtilities.getRange(stop); @@ -87,7 +87,7 @@ public void testGetRange2B() { @Test public void testGetRange3B() { // : Given - String expected = "100101103104105106107108109"; + String expected = "100101102103104105106107108109"; int start = 100; int stop = 110; @@ -173,7 +173,7 @@ public void testGetRange3C() { @Test public void testGetEvenNumbers() { // : Given - String expected = "5791113151719"; + String expected = "681012141618"; int start = 5; int stop = 20; @@ -187,7 +187,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/TableUtilitiesTest.java b/src/test/java/io/zipcoder/microlabs/mastering_loops/TableUtilitiesTest.java index 1903290..57a2074 100644 --- a/src/test/java/io/zipcoder/microlabs/mastering_loops/TableUtilitiesTest.java +++ b/src/test/java/io/zipcoder/microlabs/mastering_loops/TableUtilitiesTest.java @@ -7,6 +7,8 @@ * Created by leon on 1/31/18. */ public class TableUtilitiesTest { + + TableUtilities table = new TableUtilities(); @Test public void testGetLargeMultiplicationTable() { String expected = @@ -21,7 +23,7 @@ public void testGetLargeMultiplicationTable() { " 9 | 18 | 27 | 36 | 45 | 54 | 63 | 72 | 81 | 90 |\n" + " 10 | 20 | 30 | 40 | 50 | 60 | 70 | 80 | 90 |100 |\n"; - String actual = TableUtilities.getLargeMultiplicationTable(); + String actual = table.getLargeMultiplicationTable(); Assert.assertEquals(expected, actual); } @@ -35,7 +37,7 @@ public void testGetSmallMultiplicationTable() { " 4 | 8 | 12 | 16 | 20 |\n" + " 5 | 10 | 15 | 20 | 25 |\n"; - String actual = TableUtilities.getSmallMultiplicationTable(); + String actual = table.getSmallMultiplicationTable(); Assert.assertEquals(expected, actual); } @@ -62,7 +64,7 @@ public void testGetMultiplicationTable() { " 18 | 36 | 54 | 72 | 90 |108 |126 |144 |162 |180 |198 |216 |234 |252 |270 |288 |306 |324 |342 |360 |\n" + " 19 | 38 | 57 | 76 | 95 |114 |133 |152 |171 |190 |209 |228 |247 |266 |285 |304 |323 |342 |361 |380 |\n" + " 20 | 40 | 60 | 80 |100 |120 |140 |160 |180 |200 |220 |240 |260 |280 |300 |320 |340 |360 |380 |400 |\n"; - String actual = TableUtilities.getMultiplicationTable(20); + String actual = table.getMultiplicationTable(20); Assert.assertEquals(expected, actual); }