Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions src/main/java/chapter04/multiplication/Multiplication.java

This file was deleted.

30 changes: 30 additions & 0 deletions src/main/java/chapter04/multiplication/Multiplier.java
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* This method of multiplication restricts <i>b</i> to positive integers, since adding a number <i>n</i> times,
* where <i>n</i> is not a natural number, does not make sense. Theoretically, <i>a</i> could be any real number,
* however for simplicity's sake, <i>a</i> is made to be an integer.
*
* @param a Any integer.
* @param b Any positive integer.
* @return The product of <i>a</i> and <i>b</i>. If <i>b</i> 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));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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.
* <p>
* This method of multiplication restricts <i>b</i> to positive integers. Again, <i>a</i> could be any real
* number, but is an integer for simplicity.
*
* @param a Any integer.
* @param b Any positive integer.
* @return The product of <i>a</i> and <i>b</i>.
* @throws StackOverflowError If a negative integer is passed to <i>b</i>. Because of the way this is implemented,
* passing a negative number to <i>b</i> 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));
}
}
27 changes: 0 additions & 27 deletions src/main/java/chapter04/printtriangle/PrintStar.java

This file was deleted.

34 changes: 34 additions & 0 deletions src/main/java/chapter04/printtriangle/TrianglePrinter.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
21 changes: 0 additions & 21 deletions src/main/java/chapter04/printtriangleinverse/PrintTriangle.java

This file was deleted.

28 changes: 0 additions & 28 deletions src/main/java/chapter04/printtrianglerecursive/PrintTriangle.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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);
}
}