diff --git a/src/main/java/chapter04/multiplication/Multiplication.java b/src/main/java/chapter04/multiplication/Multiplication.java deleted file mode 100644 index 60636ea..0000000 --- a/src/main/java/chapter04/multiplication/Multiplication.java +++ /dev/null @@ -1,21 +0,0 @@ -package chapter04.multiplication; - -// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. -public class Multiplication -{ - public static void main(String [] args) - { - System.out.println(product(10, 5)); - } - - public static int product(int a, int b) - { - int result = 0; - for(int counter = 0; counter < b; counter++) - { - result = result + a; - } - return result; - } -} -// Code written by Ethan K as part of the Tenzca-AP-Computer-A-Java-Methods-Program repository on GitHub. diff --git a/src/main/java/chapter04/multiplication/Multiplier.java b/src/main/java/chapter04/multiplication/Multiplier.java new file mode 100644 index 0000000..10ac240 --- /dev/null +++ b/src/main/java/chapter04/multiplication/Multiplier.java @@ -0,0 +1,30 @@ +// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. +package chapter04.multiplication; + +/** + * A class that provides an iterative addition implementation (i.e., multiplication). + */ +public class Multiplier { + /** + * Multiplies two numbers by iterative addition. + *

+ * This method of multiplication restricts b to positive integers, since adding a number n times, + * where n is not a natural number, does not make sense. Theoretically, a could be any real number, + * however for simplicity's sake, a is made to be an integer. + * + * @param a Any integer. + * @param b Any positive integer. + * @return The product of a and b. If b is negative, returns {@code 0}. + */ + public static int product(int a, int b) { + int result = 0; + for (int counter = 0; counter < b; counter++) { + result = result + a; // Does not run if "counter < b" is impossible + } + return result; + } + + public static void main(String[] args) { + System.out.println(product(10, 5)); + } +} \ No newline at end of file diff --git a/src/main/java/chapter04/multiplicationrecursive/Multiplication.java b/src/main/java/chapter04/multiplicationrecursive/Multiplication.java deleted file mode 100644 index 8aacdae..0000000 --- a/src/main/java/chapter04/multiplicationrecursive/Multiplication.java +++ /dev/null @@ -1,20 +0,0 @@ -package chapter04.multiplicationrecursive; - -// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. -public class Multiplication { - - public static void main(String[] args) - { - System.out.println(product(10, 5)); - - } - - public static int product(int a, int b) - { - if ((a == 0) || (b == 0)) - return 0; - else - return (a + product(a, b - 1)); - - } -} \ No newline at end of file diff --git a/src/main/java/chapter04/multiplicationrecursive/RecursiveMultiplier.java b/src/main/java/chapter04/multiplicationrecursive/RecursiveMultiplier.java new file mode 100644 index 0000000..752dfe7 --- /dev/null +++ b/src/main/java/chapter04/multiplicationrecursive/RecursiveMultiplier.java @@ -0,0 +1,34 @@ +// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. +package chapter04.multiplicationrecursive; + +/** + * A class that provides an iterative addition implementation by recursion. + */ +public class RecursiveMultiplier { + /** + * Multiplies two numbers by iterative addition using recursion. + *

+ * This method of multiplication restricts b to positive integers. Again, a could be any real + * number, but is an integer for simplicity. + * + * @param a Any integer. + * @param b Any positive integer. + * @return The product of a and b. + * @throws StackOverflowError If a negative integer is passed to b. Because of the way this is implemented, + * passing a negative number to b runs the function and never reaches the base case (i.e., never satisfies + * the conditions needed to end recursion). Thus, the call stack's maximum allowed depth is reached with infinite + * recursion and the fatal {@link StackOverflowError} is thrown. This is a reason recursion should be used + * cautiously. + */ + public static int product(int a, int b) { + if ((a == 0) || (b == 0)) { // StackOverflowError could be avoided if "b == 0" is changed to "b <= 0" + return 0; + } else { + return (a + product(a, b - 1)); + } + } + + public static void main(String[] args) { + System.out.println(product(10, 5)); + } +} \ No newline at end of file diff --git a/src/main/java/chapter04/printtriangle/PrintStar.java b/src/main/java/chapter04/printtriangle/PrintStar.java deleted file mode 100644 index d037c5f..0000000 --- a/src/main/java/chapter04/printtriangle/PrintStar.java +++ /dev/null @@ -1,27 +0,0 @@ -package chapter04.printtriangle;// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. - -public class PrintStar { - - public static void main(String[] args) - { - printTriangle(5); - } - - public static void printStars (int starNum) - { - for (int counter = 0; counter <= starNum; counter++) - { - System.out.print("*"); - } - } - - public static void printTriangle(int lineNum) - { - for(int counter = 0; counter < lineNum; counter++) - { - printStars(counter); - System.out.println(); - } - } - -} \ No newline at end of file diff --git a/src/main/java/chapter04/printtriangle/TrianglePrinter.java b/src/main/java/chapter04/printtriangle/TrianglePrinter.java new file mode 100644 index 0000000..232ff14 --- /dev/null +++ b/src/main/java/chapter04/printtriangle/TrianglePrinter.java @@ -0,0 +1,34 @@ +// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. +package chapter04.printtriangle; + +/** + * A class that provides methods to print stars and triangles. + */ +public class TrianglePrinter { + /** + * Prints a given number of stars on the same line. + * + * @param stars The number of stars. + */ + public static void printStars(int stars) { + for (int counter = 0; counter <= stars; counter++) { + System.out.print("*"); + } + } + + /** + * Prints a right triangle made of stars. + * + * @param lineNum The number of lines high the triangle is to be printed. + */ + public static void printTriangle(int lineNum) { + for (int counter = 0; counter < lineNum; counter++) { + printStars(counter); + System.out.println(); + } + } + + public static void main(String[] args) { + printTriangle(5); + } +} \ No newline at end of file diff --git a/src/main/java/chapter04/printtriangleinverse/InvertedTrianglePrinter.java b/src/main/java/chapter04/printtriangleinverse/InvertedTrianglePrinter.java new file mode 100644 index 0000000..c6a1992 --- /dev/null +++ b/src/main/java/chapter04/printtriangleinverse/InvertedTrianglePrinter.java @@ -0,0 +1,29 @@ +// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. +package chapter04.printtriangleinverse; + +/** + * A class that provides a method to print an inverted triangle recursively. + */ +public class InvertedTrianglePrinter { + /** + * Prints an inverted right triangle made out of stars using recursion. + * + * @param stars The number of stars to be printed on the first line. + * @param display The current string. For a standard triangle, a string of {@code ""} should be passed, but + * additional constant width can be added by passing in a string of asterisks. + */ + public static void printTriangle(int stars, String display) { + if (stars <= 0) { + return; + } + + display = display + "*"; + printTriangle(stars - 1, display); + System.out.println(display); + } + + public static void main(String[] args) { + String p = ""; + printTriangle(5, p); + } +} diff --git a/src/main/java/chapter04/printtriangleinverse/PrintTriangle.java b/src/main/java/chapter04/printtriangleinverse/PrintTriangle.java deleted file mode 100644 index 4b7eb72..0000000 --- a/src/main/java/chapter04/printtriangleinverse/PrintTriangle.java +++ /dev/null @@ -1,21 +0,0 @@ -package chapter04.printtriangleinverse; - -// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. -public class PrintTriangle -{ - public static void main(String [] args){ - String p = ""; - printTriangle(5, p); - } - - public static String printTriangle(int starNum, String display) - { - if(starNum <= 0) return ""; - - display = display + "*"; - printTriangle(starNum-1, display); - System.out.println(display); - - return display; - } -} diff --git a/src/main/java/chapter04/printtrianglerecursive/PrintTriangle.java b/src/main/java/chapter04/printtrianglerecursive/PrintTriangle.java deleted file mode 100644 index e0ce7ac..0000000 --- a/src/main/java/chapter04/printtrianglerecursive/PrintTriangle.java +++ /dev/null @@ -1,28 +0,0 @@ -package chapter04.printtrianglerecursive; - -// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. -public class PrintTriangle -{ - - public static void main(String[] args) - { - printTriangle(5); - } - - public static String printTriangle (int starNum) - { - String display; - if(starNum <= 0) - return ""; - - display = printTriangle(starNum - 1); - display = display + "*"; - System.out.println(display); - - return display; - } - /*Gets numbers of stars, then calls it recursively with (num of stars - 1), then prints a - * line with n stars. It does nothing of num of stars = 0*/ - -} -// Code written by Ethan K as part of the Tenzca-AP-Computer-A-Java-Methods-Program repository on GitHub. diff --git a/src/main/java/chapter04/printtrianglerecursive/RecursiveTrianglePrinter.java b/src/main/java/chapter04/printtrianglerecursive/RecursiveTrianglePrinter.java new file mode 100644 index 0000000..b0d0fc7 --- /dev/null +++ b/src/main/java/chapter04/printtrianglerecursive/RecursiveTrianglePrinter.java @@ -0,0 +1,29 @@ +// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. +package chapter04.printtrianglerecursive; + +/** + * A class that provides a method to print a triangle recursively. + */ +public class RecursiveTrianglePrinter { + /** + * Prints a right triangle made out of stars using recursion. + * + * @param stars The number of stars to be printed on the first line. + * @return The current string. This is not a useful value, it's only used for passing values back from recursion. + */ + public static String printTriangle(int stars) { + if (stars <= 0) { + return ""; + } + + String display = printTriangle(stars - 1); + display = display + "*"; + System.out.println(display); + + return display; + } + + public static void main(String[] args) { + printTriangle(5); + } +} \ No newline at end of file