From a9f695003ecd66ec9baed7596da6420f6dc09c7c Mon Sep 17 00:00:00 2001 From: jenn-182 Date: Mon, 14 Jul 2025 22:19:10 -0400 Subject: [PATCH 1/4] only number utilities and test finished --- .../mastering_loops/NumberUtilities.java | 99 +++++++++++++++++-- .../mastering_loops/TriangleUtilities.java | 20 +++- .../mastering_loops/NumberUtilitiesTest.java | 2 +- 3 files changed, 109 insertions(+), 12 deletions(-) diff --git a/java/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java b/java/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java index a0c6b1a..383230d 100644 --- a/java/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java +++ b/java/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java @@ -4,38 +4,119 @@ public class NumberUtilities { public static String getExponentiations(int start, int stop, int step, int exponent) { - return null; + + // Initialize an empty string to hold result + String result=""; + + // Iterate through the range from start to stop with the given step + // step skips by 5 so it should be 5,10,15 + for (int i=start;i set equal and exclude 'stop' ->not equal) + for (int i=start;i set equal and exclude 'stop' ->not equal) + for (int i = start; i < stop; i++) { + if (i % 2 != 0) { + result = result + i; + } + } + //return result + return result; } + public static String getOddNumbers(int start, int stop) { - return null; + + // Initialize an empty string to hold result + String result = ""; + + // Iterate through the range from start to stop (include 'start' -> set equal and exclude 'stop' ->not equal) + for (int i = start; i < stop; i++) { + if (i % 2 == 0) { + result = result + i; + } + } + //return result + return result; } + public static String getSquareNumbers(int start, int stop, int step) { - return null; + // Initialize an empty string to hold result + String result = ""; + + // Iterate through the range from start to stop with the given step + // step skips by 5 so it should be 5,10,15 + for (int i = start; i < stop; i += step) { + result = result + (i * i); + } + //return result + return result; } } diff --git a/java/src/main/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilities.java b/java/src/main/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilities.java index 6e2f6a1..22555b3 100644 --- a/java/src/main/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilities.java +++ b/java/src/main/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilities.java @@ -3,11 +3,27 @@ public class TriangleUtilities { public static String getRow(int numberOfStars) { - return null; + + String result = ""; + int i = 1; + while (i <= numberOfStars) { + result = result + '*'; + i = i + 1; + } + //result = result + '\n'; + return result; + } public static String getTriangle(int numberOfRows) { - return null; + + String result=""; + int i=1; + while (i Date: Mon, 14 Jul 2025 22:53:24 -0400 Subject: [PATCH 2/4] Triangles finished --- .../mastering_loops/TriangleUtilities.java | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/java/src/main/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilities.java b/java/src/main/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilities.java index 22555b3..b990cf5 100644 --- a/java/src/main/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilities.java +++ b/java/src/main/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilities.java @@ -4,7 +4,10 @@ public class TriangleUtilities { public static String getRow(int numberOfStars) { + // Initialize an empty string to hold the row of stars String result = ""; + + // Use a while loop until the number of stars is reached int i = 1; while (i <= numberOfStars) { result = result + '*'; @@ -17,22 +20,31 @@ public static String getRow(int numberOfStars) { public static String getTriangle(int numberOfRows) { + // Initialize an empty string to hold the triangle String result=""; + + // Use a while loop to build the triangle row by row int i=1; while (i Date: Tue, 15 Jul 2025 13:27:37 -0400 Subject: [PATCH 3/4] Fixed number utilities and finished Tables --- .../mastering_loops/NumberUtilities.java | 4 +- .../mastering_loops/TableUtilities.java | 45 +++++++++++++++++-- .../mastering_loops/TriangleUtilities.java | 2 +- .../mastering_loops/NumberUtilitiesTest.java | 4 +- 4 files changed, 47 insertions(+), 8 deletions(-) diff --git a/java/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java b/java/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java index 383230d..a29081b 100644 --- a/java/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java +++ b/java/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java @@ -81,7 +81,7 @@ public static String getEvenNumbers(int start, int stop) { // Iterate through the range from start to stop (include 'start' -> set equal and exclude 'stop' ->not equal) for (int i = start; i < stop; i++) { - if (i % 2 != 0) { + if (i % 2 == 0) { result = result + i; } } @@ -97,7 +97,7 @@ public static String getOddNumbers(int start, int stop) { // Iterate through the range from start to stop (include 'start' -> set equal and exclude 'stop' ->not equal) for (int i = start; i < stop; i++) { - if (i % 2 == 0) { + if (i % 2 != 0) { result = result + i; } } diff --git a/java/src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java b/java/src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java index 7276d17..2859f9e 100644 --- a/java/src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java +++ b/java/src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java @@ -1,15 +1,54 @@ package io.zipcoder.microlabs.mastering_loops; public class TableUtilities { + public static String getMultiplicationTable(int tableSize) { - return null; + + //Create variable table to hold empty string + String table = ""; + + //Create outer loop for ROWS -> R => the amound of rows needed + // Iterate from 1 to tableSize (including tableSize) + for (int r = 1; r <= tableSize; r++) { + + // Create inner loop for COLUMNS -> C => the amount of columns needed + for (int c = 1; c <= tableSize; c++) { + + // Multiply the row and columns to get the total for the table + //(how many times the row and column intersect) + int valueOfTableSize = r * c; + + //If number is less than 10, add 2 spaces before the number + //and a | to the end + if (valueOfTableSize < 10) { + table = table + " " + valueOfTableSize + " |"; + } else if (valueOfTableSize < 100) { + //If number is less than 100, add one space before the number + //add a | to the end + table = table + " " + valueOfTableSize + " |"; + } else { + //If number is greater than 100, no need to add spaces + //add a | to the end + table = table + valueOfTableSize + " |"; + } + } + // Add a newline at the end of each row + table = table + "\n"; + } + //Return the table + return table; } + public static String getSmallMultiplicationTable() { - return null; + + //call the getMultiplicationTable method with 5 to get 5 rows and 5 columns + return getMultiplicationTable(5); } public static String getLargeMultiplicationTable() { - return null; + + //call the getMultiplicationTable method with 10 to get 10 rows and 10 columns + return getMultiplicationTable(10); } } diff --git a/java/src/main/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilities.java b/java/src/main/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilities.java index b990cf5..7413fb5 100644 --- a/java/src/main/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilities.java +++ b/java/src/main/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilities.java @@ -31,7 +31,7 @@ public static String getTriangle(int numberOfRows) { result = result + '\n'; i++; } - // Add the last row + // return the amount of rows return result; } diff --git a/java/src/test/java/io/zipcoder/microlabs/mastering_loops/NumberUtilitiesTest.java b/java/src/test/java/io/zipcoder/microlabs/mastering_loops/NumberUtilitiesTest.java index 380eebf..5783bf3 100644 --- a/java/src/test/java/io/zipcoder/microlabs/mastering_loops/NumberUtilitiesTest.java +++ b/java/src/test/java/io/zipcoder/microlabs/mastering_loops/NumberUtilitiesTest.java @@ -184,7 +184,7 @@ public void testGetRange3C() { @Test public void testGetEvenNumbers() { // : Given - String expected = "5791113151719"; + String expected = "681012141618"; int start = 5; int stop = 20; @@ -198,7 +198,7 @@ public void testGetEvenNumbers() { @Test public void testGetOddNumbers() { // : Given - String expected = "681012141618"; + String expected = "5791113151719"; int start = 5; int stop = 20; int step = 5; From 6d302cb8ddfff660a8239acbf99bd4de1d923025 Mon Sep 17 00:00:00 2001 From: jenn-182 Date: Tue, 15 Jul 2025 21:39:11 -0400 Subject: [PATCH 4/4] Fixed some commenrs --- .../microlabs/mastering_loops/NumberUtilities.java | 2 +- .../microlabs/mastering_loops/TableUtilities.java | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/java/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java b/java/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java index a29081b..89a8693 100644 --- a/java/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java +++ b/java/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java @@ -17,7 +17,7 @@ public static String getExponentiations(int start, int stop, int step, int expon double doubleResult=Math.pow(i,exponent); //convert double to string - result= result + String.valueOf(doubleResult); + result = result + String.valueOf(doubleResult); } //return result return result; diff --git a/java/src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java b/java/src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java index 2859f9e..ab0b1e8 100644 --- a/java/src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java +++ b/java/src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java @@ -8,14 +8,14 @@ public static String getMultiplicationTable(int tableSize) { String table = ""; //Create outer loop for ROWS -> R => the amound of rows needed - // Iterate from 1 to tableSize (including tableSize) + //Iterate from 1 to tableSize (including tableSize) for (int r = 1; r <= tableSize; r++) { - // Create inner loop for COLUMNS -> C => the amount of columns needed + //Create inner loop for COLUMNS -> C => the amount of columns needed for (int c = 1; c <= tableSize; c++) { - // Multiply the row and columns to get the total for the table - //(how many times the row and column intersect) + //Multiply the row and columns to get the total for the table size + //(how many times do the rows and columns intersect?) int valueOfTableSize = r * c; //If number is less than 10, add 2 spaces before the number @@ -32,7 +32,7 @@ public static String getMultiplicationTable(int tableSize) { table = table + valueOfTableSize + " |"; } } - // Add a newline at the end of each row + // Add a newline at the end of each row -> \n table = table + "\n"; } //Return the table