From 40e30491c590936c74820edd9d250c3a1eec28ed Mon Sep 17 00:00:00 2001 From: Peter McCormick Date: Thu, 8 Feb 2018 07:54:46 -0500 Subject: [PATCH 1/2] numbers util --- .../mastering_loops/NumberUtilities.java | 42 +++++++++++++++++-- .../mastering_loops/NumberUtilitiesTest.java | 7 ++-- 2 files changed, 42 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..fc4db17 100644 --- a/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java +++ b/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java @@ -2,23 +2,57 @@ public class NumberUtilities { + + public static String getEvenNumbers(int start, int stop) { - return null; + StringBuilder evenNumbers = new StringBuilder(); + for (int i = start; i < stop ; i++) { + if(i % 2 == 0) + { + evenNumbers.append(i); + } + + } + + + return evenNumbers.toString(); + } public static String getOddNumbers(int start, int stop) { - return null; + StringBuilder builderOddNumbers = new StringBuilder(); + for (int i = start; i < stop ; i++) { + if(!(i % 2 == 0)) + { + builderOddNumbers.append(i); + } + + } + + + return builderOddNumbers.toString(); + } public static String getSquareNumbers(int start, int stop, int step) { - return null; + StringBuilder squareNumbersBuilder = new StringBuilder(); + for (int i = start; i Date: Thu, 8 Feb 2018 14:39:05 -0500 Subject: [PATCH 2/2] Looping program finished used nested for loops --- .../mastering_loops/NumberUtilities.java | 9 ++++- .../mastering_loops/TableUtilities.java | 35 ++++++++++++++++-- .../mastering_loops/TriangleUtilities.java | 37 +++++++++++++++++-- .../TriangleUtilitiesTest.java | 4 +- 4 files changed, 75 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 fc4db17..2552802 100644 --- a/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java +++ b/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java @@ -57,6 +57,13 @@ 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 exponentBuilder = new StringBuilder(); + for (int i = start; i < stop ; i = i + step){ + long exponentiatedValue = (long)Math.pow(i, exponent); + exponentBuilder.append(exponentiatedValue); + } + + return exponentBuilder.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..525f56e 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,43 @@ public class TableUtilities { public static String getSmallMultiplicationTable() { - return null; + StringBuilder smallMultiBuilder = new StringBuilder(); + for (int i = 1; i <= 5; i++) { + + for (int w = 1; w <= 5; w++) { + int productIW = i * w; + smallMultiBuilder.append(String.format("%3d |", productIW)); + } + smallMultiBuilder.append("\n"); + } + return smallMultiBuilder.toString(); } public static String getLargeMultiplicationTable() { - return null; + StringBuilder largeMultiplicationBuilder = new StringBuilder(); + for (int i = 1; i <= 10; i++) { + for (int w = 1; w <= 10; w++) { + int productIW = i * w; + largeMultiplicationBuilder.append(String.format("%3d |", productIW)); + } + largeMultiplicationBuilder.append("\n"); + } + return largeMultiplicationBuilder.toString(); + } public static String getMultiplicationTable(int tableSize) { - return null; + StringBuilder multiplicationBuilder = new StringBuilder(); + for (int i = 1; i <= 20; i++) { + + for (int w = 1; w <= 20; w++) { + int productIW = (i * w); + multiplicationBuilder.append(String.format("%3d |", productIW)); + + } + multiplicationBuilder.append("\n"); + + } + return multiplicationBuilder.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..5aa6862 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 triangleBuilder = new StringBuilder(); + + for (int i = 1; i <= numberOfRows-1; i++) { + for (int w = 1; w <= i ; w++) { + triangleBuilder.append("*"); + } + triangleBuilder.append("\n"); + } + + return triangleBuilder.toString(); } public static String getRow(int numberOfStars) { - return null; + + StringBuilder asteriskRowBuild = new StringBuilder(); + for(int i = 1; i <= numberOfStars; i++) { + asteriskRowBuild.append("*"); + } + return asteriskRowBuild.toString(); } public static String getSmallTriangle() { - return null; + + StringBuilder smallTriangle = new StringBuilder(); + for (int i =1; i <= 4; i++) { + for (int w = 1; w <= i; w ++) { + smallTriangle.append("*"); + } + smallTriangle.append("\n"); + } + return smallTriangle.toString(); } public static String getLargeTriangle() { - return null; + StringBuilder largeTriangle = new StringBuilder(); + for (int i =1; i <= 9; i++) { + for (int w = 1; w <= i; w++) { + largeTriangle.append("*"); + } + largeTriangle.append("\n"); + } + return largeTriangle.toString(); } } 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..bc3bb17 100644 --- a/src/test/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilitiesTest.java +++ b/src/test/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilitiesTest.java @@ -18,7 +18,7 @@ public void getRow() { @Test public void getTriangleTest1() { String expected = - "*\n" + + "*\n" + "**\n" + "***\n" + "****\n" + @@ -34,7 +34,7 @@ public void getTriangleTest1() { @Test public void getTriangleTest2() { String expected = - "*\n" + + "*\n" + "**\n" + "***\n" + "****\n";