From 8528f0dfbfb46a4479c93790a3c8ced0bc9a29e0 Mon Sep 17 00:00:00 2001 From: Sandy Setiawan Date: Sat, 8 Feb 2020 10:50:14 -0500 Subject: [PATCH] NumbersTrianglesTables Sandy Setiawan --- NumberUtilities.java | 48 ++++++++++++++++++++++++++++++++---------- TableUtilities.java | 34 ++++++++++++++++++++++++++---- TriangleUtilities.java | 30 ++++++++++++++++++++------ package.bluej | 22 +++++++++---------- 4 files changed, 102 insertions(+), 32 deletions(-) diff --git a/NumberUtilities.java b/NumberUtilities.java index 1d5ba50..7f9cf53 100644 --- a/NumberUtilities.java +++ b/NumberUtilities.java @@ -4,29 +4,55 @@ public class NumberUtilities { public static String getRange(int stop) { - return null; + String result = ""; + for (int i = 0; i < stop; i++) { + result += i; + } + return result; } public static String getRange(int start, int stop) { - return null; + String result2 = ""; + for (int i = start; i < stop; i++) { + result2 += i; + } + return result2; } public static String getRange(int start, int stop, int step) { - return null; + String result3 = ""; + for (int i = start; i < stop; i = i + step) { + result3 += i; + } + return result3; } public static String getEvenNumbers(int start, int stop) { - return null; + String result4 = ""; + for (int i = start; i < stop; i++) { + if(i % 2 == 0) { + result4 += i; + } } - - + return result4; + } + public static String getOddNumbers(int start, int stop) { - return null; + String result5 = ""; + for (int i = start; i < stop; i++) { + if(i % 2 == 1) { + result5 +=i; + } } - - - public static String getExponentiations(int start, int stop, int exponent) { - return null; +return result5; +} +public static String getExponentiations(int start, int stop, int exponent) { + String result6 = ""; + for (int i = start; i <= stop; i++) { + + result6 +=(int)Math.pow(i, exponent); } + return result6; +} } diff --git a/TableUtilities.java b/TableUtilities.java index 03bf004..3efa8ea 100644 --- a/TableUtilities.java +++ b/TableUtilities.java @@ -2,14 +2,40 @@ public class TableUtilities { public static String getSmallMultiplicationTable() { - return null; + int x = 5; + String result1 = getMultiplicationTable(x); + return result1; } public static String getLargeMultiplicationTable() { - return null; + int y = 10; + String result2 = getMultiplicationTable(y); + return result2; } public static String getMultiplicationTable(int tableSize) { - return null; + String result = ""; + for (int i=1; i <= tableSize; i++) { + for (int j=1; j <= tableSize; j++) { + int product = i * j; + if (product <10) + { + result += " " + product + " |"; + } + else if ( product < 100 && product > 9) + { + result += " " + product + " |"; + } + else + { + result += "" + product + " |"; + } + } + result += "\n"; + } + + return result; } -} + + } + diff --git a/TriangleUtilities.java b/TriangleUtilities.java index 8755fd0..ef8b340 100644 --- a/TriangleUtilities.java +++ b/TriangleUtilities.java @@ -3,19 +3,37 @@ public class TriangleUtilities { public static String getRow(int numberOfStars) { - return null; + String result = ""; + for(int i = 1; i <= numberOfStars; i++) { + result += "*"; } + return result; +} - public static String getTriangle(int numberOfRows) { - return null; +public static String getTriangle(int numberOfRows) { + String result2 = ""; + for(int i = 1; i <= numberOfRows; i++) + { + result2 += getRow(i) + "\n" ; } - + return result2; +} public static String getSmallTriangle() { - return null; + String result3 =""; + for(int i = 1; i <= 4; i++) + { + result3 += getRow(i) + "\n"; + } + return result3; } public static String getLargeTriangle() { - return null; + String result4 =""; + for(int i = 1; i <= 9; i++) + { + result4 += getRow(i) + "\n"; + } + return result4; } } diff --git a/package.bluej b/package.bluej index 028bdd0..2bd6ada 100644 --- a/package.bluej +++ b/package.bluej @@ -10,17 +10,17 @@ dependency3.to=TableUtilities dependency3.type=UsesDependency editor.fx.0.height=722 editor.fx.0.width=800 -editor.fx.0.x=537 -editor.fx.0.y=28 -objectbench.height=164 +editor.fx.0.x=523 +editor.fx.0.y=66 +objectbench.height=179 objectbench.width=484 package.divider.horizontal=0.6 -package.divider.vertical=0.7560627674750356 -package.editor.height=523 -package.editor.width=382 -package.editor.x=20 -package.editor.y=57 -package.frame.height=759 +package.divider.vertical=0.7223880597014926 +package.editor.height=477 +package.editor.width=390 +package.editor.x=100 +package.editor.y=24 +package.frame.height=728 package.frame.width=508 package.numDependencies=3 package.numTargets=6 @@ -61,14 +61,14 @@ target4.showInterface=false target4.type=ClassTarget target4.width=120 target4.x=80 -target4.y=70 +target4.y=50 target5.height=50 target5.name=NumberUtilitiesTest target5.showInterface=false target5.type=UnitTestTargetJunit4 target5.width=120 target5.x=110 -target5.y=40 +target5.y=20 target6.association=TriangleUtilitiesTest target6.height=50 target6.name=TriangleUtilities