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..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 @@ -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/TableUtilities.java b/java/src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java index 7276d17..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 @@ -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 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 + //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 -> \n + 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 6e2f6a1..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 @@ -3,20 +3,48 @@ public class TriangleUtilities { public static String getRow(int numberOfStars) { - return null; + + // 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 + '*'; + i = i + 1; + } + //result = result + '\n'; + return result; + } public static String getTriangle(int numberOfRows) { - return null; + + // 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