From 804d4c595c46adc2ed2c423e3941d38e30da1a15 Mon Sep 17 00:00:00 2001 From: Vincent Gasbarro Date: Wed, 7 Feb 2018 19:19:25 -0500 Subject: [PATCH 1/3] solved numbers --- .../mastering_loops/NumberUtilities.java | 42 ++++++++++++++++--- .../mastering_loops/NumberUtilitiesTest.java | 6 ++- 2 files changed, 41 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..4d1f106 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,58 @@ public class NumberUtilities { public static String getEvenNumbers(int start, int stop) { - return null; + + String evens = ""; + for (int i = start; i < stop; i++) { + if (i % 2 == 0) { + evens += Integer.toString(i); + } + } + + return evens; } public static String getOddNumbers(int start, int stop) { - return null; + + String odds = ""; + for (int i = start; i < stop; i++) { + if (i % 2 != 0) { + odds += Integer.toString(i); + } + } + + return odds; + } public static String getSquareNumbers(int start, int stop, int step) { - return null; + + String roots = ""; + for (int i = start; i < stop; i += step) { + roots += Integer.toString(i * i); + } + return roots; } public static String getRange(int start, int stop, int step) { - return null; + + String rangeString = ""; + for (int i = start; i < stop; i += step) { + rangeString += Integer.toString(i); + } + return rangeString; } public static String getExponentiations(int start, int stop, int step, int exponent) { - return null; + + String exponentString = ""; + for (int i = start; i < stop; i += step) { + exponentString += (int) Math.pow(i, exponent); + } + return exponentString; } } 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..d30be8e 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,8 @@ public void testGetRange2() { @Test public void testGetEvenNumbers() { // : Given - String expected = "5791113151719"; + //String expected = "5791113151719"; + String expected = "681012141618"; int start = 5; int stop = 20; @@ -65,7 +66,8 @@ public void testGetEvenNumbers() { @Test public void testGetOddNumbers() { // : Given - String expected = "681012141618"; + //String expected = "681012141618"; + String expected = "5791113151719"; int start = 5; int stop = 20; int step = 5; From 00653faf7b306cd8ad2577804d3933fd43eaab1e Mon Sep 17 00:00:00 2001 From: Vincent Gasbarro Date: Thu, 8 Feb 2018 13:11:37 -0500 Subject: [PATCH 2/3] initial save --- .../mastering_loops/TableUtilities.java | 8 +++ .../mastering_loops/TriangleUtilities.java | 50 ++++++++++++++++--- 2 files changed, 52 insertions(+), 6 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..5db9bf3 100644 --- a/src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java +++ b/src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java @@ -2,6 +2,14 @@ public class TableUtilities { public static String getSmallMultiplicationTable() { + + int value = 5; + StringBuilder sb = new StringBuilder(); + + for (int i = 1; i <= value; i++) { + sb.append(String.format("%3d_|", i)); + } + return null; } 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..c1b728a 100644 --- a/src/main/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilities.java +++ b/src/main/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilities.java @@ -2,19 +2,57 @@ public class TriangleUtilities { - public static String getTriangle(int numberOfRows) { - return null; - } + public static String getTriangle(int numberOfRows) { return null; } + + public static String getRow(int numberOfStars) { - return null; + + StringBuilder asteriskRow = new StringBuilder(); + + for (int i = 0; i <= numberOfStars - 1; i++) { + asteriskRow.append("*"); + } + + return asteriskRow.toString(); } + public static String getSmallTriangle() { - return null; + + StringBuilder smallTriangle = new StringBuilder(); + int size = 4; + int row = 1; + + for (int i = 1; i <= size; i++) { + for (int k = 1; k <= row; k++) { + smallTriangle.append("*"); + } + smallTriangle.append("\n"); + row++; + } + return smallTriangle.toString(); + } + public static String getLargeTriangle() { - return null; + + StringBuilder largeTriangle = new StringBuilder(); + int size = 9; + int row = 1; + + for (int i = 1; i <= size; i++) { + + for (int k = 1; k <= row; k++) { + largeTriangle.append("*"); + } + + largeTriangle.append("\n"); + row++; + } + return largeTriangle.toString(); + + } } From e3a26527da04d62939a14f7736bd897e8c68ec0a Mon Sep 17 00:00:00 2001 From: Vincent Gasbarro Date: Thu, 8 Feb 2018 14:15:59 -0500 Subject: [PATCH 3/3] to turn in --- .../mastering_loops/TableUtilities.java | 40 +++++++++++++++---- .../mastering_loops/TriangleUtilities.java | 18 ++++++++- .../TriangleUtilitiesTest.java | 2 +- 3 files changed, 50 insertions(+), 10 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 5db9bf3..085da13 100644 --- a/src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java +++ b/src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java @@ -3,21 +3,45 @@ public class TableUtilities { public static String getSmallMultiplicationTable() { - int value = 5; - StringBuilder sb = new StringBuilder(); + StringBuilder smallTable = new StringBuilder(); + int size = 5; - for (int i = 1; i <= value; i++) { - sb.append(String.format("%3d_|", i)); + for (int i = 1; i <= size; i++) { + for (int k = 1; k <= size; k++) { + smallTable.append(String.format("%3d |", i * k)); + } + smallTable.append("\n"); } - - return null; + return smallTable.toString(); } public static String getLargeMultiplicationTable() { - return null; + + StringBuilder smallTable = new StringBuilder(); + int size = 10; + + for (int i = 1; i <= size; i++) { + for (int k = 1; k <= size; k++) { + smallTable.append(String.format("%3d |", i * k)); + } + smallTable.append("\n"); + } + return smallTable.toString(); + } public static String getMultiplicationTable(int tableSize) { - return null; + + StringBuilder smallTable = new StringBuilder(); + int size = tableSize; + + for (int i = 1; i <= size; i++) { + for (int k = 1; k <= size; k++) { + smallTable.append(String.format("%3d |", i * k)); + } + smallTable.append("\n"); + } + return smallTable.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 c1b728a..fe9dbf9 100644 --- a/src/main/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilities.java +++ b/src/main/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilities.java @@ -2,7 +2,23 @@ public class TriangleUtilities { - public static String getTriangle(int numberOfRows) { return null; } + public static String getTriangle(int numberOfRows) { + + StringBuilder anyTriangle = new StringBuilder(); + int size = numberOfRows; + int row = 1; + + for (int i = 1; i <= size; i++) { + + for (int k = 1; k <= row; k++) { + anyTriangle.append("*"); + } + anyTriangle.append("\n"); + row++; + } + + return anyTriangle.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..486d7bb 100644 --- a/src/test/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilitiesTest.java +++ b/src/test/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilitiesTest.java @@ -38,7 +38,7 @@ public void getTriangleTest2() { "**\n" + "***\n" + "****\n"; - String actual = TriangleUtilities.getTriangle(5); + String actual = TriangleUtilities.getTriangle(4); Assert.assertEquals(expected, actual); }