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..b4d49c3 100644 --- a/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java +++ b/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java @@ -1,28 +1,62 @@ package io.zipcoder.microlabs.mastering_loops; +import java.util.Arrays; + public class NumberUtilities { public static String getEvenNumbers(int start, int stop) { - return null; + StringBuilder evenN = new StringBuilder(); + + for (int i = start; i < stop; i++) { + if ((i % 2) == 0) { + evenN.append(i); + } + + }return evenN.toString(); } public static String getOddNumbers(int start, int stop) { - return null; - } + StringBuilder newStringy = new StringBuilder(); + for (int i = start; i < stop; i++) { + if ((i % 2) != 0) { + newStringy.append(i); + } - public static String getSquareNumbers(int start, int stop, int step) { - return null; - } + }return newStringy.toString(); + } + + + public static String getSquareNumbers ( int start, int stop, int step){ + StringBuilder newSt = new StringBuilder(); + + for (int i = start; i < stop; i += step) { + int squareD = (int) Math.pow(i, 2); + newSt.append(squareD); + } + return newSt.toString(); + } + + + public static String getRange ( int start, int stop, int step){ + StringBuilder newArr = new StringBuilder(); + + for (int i = start; i < stop; i += step) { + newArr.append(i); + } + return newArr.toString(); + } - public static String getRange(int start, int stop, int step) { - return null; - } + public static String getExponentiationNumbers( int start, int stop, int step, int exponent){ + StringBuilder expoS = new StringBuilder(); - public static String getExponentiations(int start, int stop, int step, int exponent) { - return null; + for(int i = start; i < stop; i+= step){ + int powerS = (int) Math.pow(i, exponent); + expoS.append(powerS); + } + return expoS.toString(); + } } -} 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..5cb7a02 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,26 @@ package io.zipcoder.microlabs.mastering_loops; public class TableUtilities { + public static String getSmallMultiplicationTable() { - return null; + String table = getMultiplicationTable(5); + return table; } public static String getLargeMultiplicationTable() { - return null; + String largeTable = getMultiplicationTable(10); + return largeTable; } public static String getMultiplicationTable(int tableSize) { - return null; + StringBuilder newTable = new StringBuilder(); + + for (int i = 1; i <= tableSize; i++) { + for (int m = 1; m <= tableSize; m++) { + newTable.append((String.format("%3d |", i * m)) + ""); + } + newTable.append("" + "\n"); + } + return newTable.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..099d83b 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,46 @@ public class TriangleUtilities { public static String getTriangle(int numberOfRows) { - return null; + StringBuilder newTri = new StringBuilder(); + + for (int i = 1; i < numberOfRows; i++) { + for (int m = 0; m < i; m++) { + newTri.append("*"); + } + newTri.append("\n"); + } + return newTri.toString(); } public static String getRow(int numberOfStars) { - return null; + String newRow = ""; + for (int i = 0; i < numberOfStars; i++) { + newRow += "*"; + } + return newRow; } public static String getSmallTriangle() { - return null; + StringBuilder smallTri = new StringBuilder(); + for (int i = 1; i <= 4; i++) { + for (int m = 0; m < i; m++) { + smallTri.append("*"); + } + smallTri.append("\n"); + } + return smallTri.toString(); } - public static String getLargeTriangle() { - return null; + + public static String getLargeTriangle () { + StringBuilder largeTri = new StringBuilder(); + for(int i = 1; i <= 9; i++){ + for(int m = 0; m < i; m++){ + largeTri.append("*"); + } + largeTri.append("\n"); + } + return largeTri.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..5f7d783 100644 --- a/src/test/java/io/zipcoder/microlabs/mastering_loops/NumberUtilitiesTest.java +++ b/src/test/java/io/zipcoder/microlabs/mastering_loops/NumberUtilitiesTest.java @@ -36,22 +36,10 @@ public void testGetRange2() { } - - - - - - - - - - - - @Test public void testGetEvenNumbers() { // : Given - String expected = "5791113151719"; + String expected = "681012141618"; int start = 5; int stop = 20; @@ -65,7 +53,7 @@ public void testGetEvenNumbers() { @Test public void testGetOddNumbers() { // : Given - String expected = "681012141618"; + String expected = "5791113151719"; int start = 5; int stop = 20; int step = 5; @@ -78,7 +66,6 @@ public void testGetOddNumbers() { } - @Test public void testGetSquareNumbers() { // : Given @@ -105,7 +92,7 @@ public void testGetExponentiationNumbers() { int exponent = 2; // : When - String actual = NumberUtilities.getExponentiations(start, stop, step, exponent); + String actual = NumberUtilities.getExponentiationNumbers(start, stop, step, exponent); Assert.assertEquals(expected, actual); }