From c1bd66b478e46e5192b7be99bfb4313ceb243558 Mon Sep 17 00:00:00 2001 From: Katrice Williams-Dredden Date: Thu, 8 Feb 2018 18:31:03 -0500 Subject: [PATCH 1/2] msg --- .../mastering_loops/NumberUtilities.java | 45 +++++++++++++++++-- .../mastering_loops/NumberUtilitiesTest.java | 4 +- 2 files changed, 44 insertions(+), 5 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..5d47fdf 100644 --- a/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java +++ b/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java @@ -3,21 +3,60 @@ public class NumberUtilities { public static String getEvenNumbers(int start, int stop) { - return null; + + String evenvalue = ""; + + for (int i = start; i Date: Fri, 9 Feb 2018 16:47:08 -0500 Subject: [PATCH 2/2] msg --- .../mastering_loops/NumberUtilities.java | 17 ++++-- .../mastering_loops/TableUtilities.java | 60 +++++++++++++++++-- .../mastering_loops/TriangleUtilities.java | 49 +++++++++++++-- 3 files changed, 111 insertions(+), 15 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 5d47fdf..544f175 100644 --- a/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java +++ b/src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java @@ -50,18 +50,25 @@ public static String getSquareNumbers(int start, int stop, int step) { public static String getRange(int start, int stop, int step) { + String holder = ""; + for(int i = start; i < stop; i += step) { + holder += i; + } + return holder; + } + public static String getExponentiations(int start, int stop, int step, int exponent) { + String exp_num = ""; - return null; + for (int i = start; i < stop; i += step){ + exp_num += "" +(int)Math.pow(i, exponent); + } + return exp_num; } - - public static String getExponentiations(int start, int stop, int step, int exponent) { - return null; - } } 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..e4a1198 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,67 @@ package io.zipcoder.microlabs.mastering_loops; + public class TableUtilities { public static String getSmallMultiplicationTable() { - return null; + + //so I don't have to concatenate every string, I named it times + //my first for loop, created to look ht(height), the dimension is defined in the middle + //my 2nd for loop, created to look width(width), the dimension is defined in the middle + /*I actually got this portion from student help but I know what it means. The append + goes along with the string builder and times is just the title. String format will create a + formatted string without having to printf it. The %3 represents the field width of, "d" is decimal + integer, space, pipe, by the value of 1. + */ + //and then we are returning times to a string format and it passed. + + //you have to append outside the inside loop because that loop PRINTS A ROW! + + StringBuilder times = new StringBuilder(); + + for (int ht=1; ht<6; ht++){ + for (int wd=1; wd<6; wd++){ + times.append(String.format("%3d |", wd*ht)); + } + times.append("\n"); + } + return times.toString(); } + + + + public static String getLargeMultiplicationTable() { - return null; + StringBuilder timest = new StringBuilder(); + + for (int h=1; h<11; h++){ + for (int w=1; w<11; w++){ + timest.append(String.format("%3d |", w*h)); + } + timest.append("\n"); + } + return timest.toString(); } + + public static String getMultiplicationTable(int tableSize) { - return null; + StringBuilder multit = new StringBuilder(); + + for (int n=1; n<21; n++){ + for (int w=1; w<21; w++){ + multit.append(String.format("%3d |", w*n)); + } + multit.append("\n"); + } + return multit.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..a410c46 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,56 @@ 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; + + //The aim of using StringBuilder, i.e reducing memory. Is it achieved? + //adding numberOfStars to StringBuilder allows for the length to be known up front + StringBuilder stars = new StringBuilder(numberOfStars); + //now we want to add * to the stars StringBuilder for the length of numberOfStars + stars.append("*"); + //now we are returning our stars back to a string and it passes!! + return stars.toString(); + } public static String getSmallTriangle() { - return null; + + StringBuilder shinyStar = new StringBuilder(); + + int h, w; + for (h = 1 ; h <= 4 ; h++) { + for (w = 1 ; w <= h ; w++) { + shinyStar.append("*"); + } + shinyStar.append("\n"); + } + + return shinyStar.toString(); } + + + public static String getLargeTriangle() { - return null; + + StringBuilder bigStar = new StringBuilder(); + + int ht, wd; + for (ht = 1 ; ht <= 10 ; ht++) { + for (wd = 1 ; wd <= ht ; wd++) { + bigStar.append("*"); + } + bigStar.append("\n"); + } + + return bigStar.toString(); + + + + + + } }