Unit 10 HW/Vocab Integration
Recursion HW
- Creating a class
- The main method
- Standard methods such as toString(), equals(), and hashCode() Recursion is a programming technique in which a method calls itself to solve a problem. A recursive method must have a base case, which is a condition that stops the recursion, and a recursive case, which is a condition that makes the method call itself.
For example, the following factorial() method calculates the factorial of a given number using recursion:
public class Factorial {
public static void main(String[] args) {
int n = 5;
int factorial = factorial(n);
System.out.println("The factorial of " + n + " is " + factorial);
}
public static int factorial(int n) {
if (n == 0) {
// Base case: the factorial of 0 is 1
return 1;
} else {
// Recursive case: the factorial of n is n * the factorial of n - 1
return n * factorial(n - 1);
}
}
}
In this example, the factorial() method has a base case for the n == 0 condition, where it returns 1, and a recursive case for the n > 0 condition, where it calls itself with n - 1 as an argument. This allows the factorial() method to calculate the factorial of a given number by calling itself repeatedly with smaller and smaller numbers until it reaches the base case.
Casting, wrapper classes, concatenation, the Math class, compound boolean expressions, truth tables, De Morgan's law, comparing numbers, comparing strings, comparing objects, loops, constructor, accessor and mutator methods, static variables and methods, the this keyword, tester methods, inheritance, subclass constructor, method overloading, method overriding, abstract classes and methods, late binding of objects, polymorphism, and Big O notation are not directly related to recursion in Java.
//ex
public class RecursiveFunctions {
public static void main(String[] args) {
int n = 12;
int result = half(n);
System.out.println("Half of " + n + " is " + result);
}
public static int half(int n) {
if (n == 0) {
// Base case: the half of 0 is 0
return 0;
} else {
// Recursive case: the half of n is the half of n / 2
return half(n / 2);
}
}
}
In this program, the RecursiveFunctions class contains the main method, which is the entry point of the program. The main method calls the half() method with the number 12 as an argument and prints out the result.
The half() method is a recursive method that calculates the half of a given number. It has a base case for the n == 0 condition, where it returns 0, and a recursive case for the n > 0 condition, where it calls itself with n / 2 as an argument. This allows the half() method to calculate the half of a given number by calling itself repeatedly with smaller and smaller numbers until it reaches the base case.
The half() method is a standard method that is defined within the RecursiveFunctions class. It has a return type of int, which means it returns an integer value. The method uses
//2 base cases
public class RecursiveFunctions {
public static void main(String[] args) {
int n = 12;
int result = nextEven(n);
System.out.println("The next even number after " + n + " is " + result);
}
public static int nextEven(int n) {
if (n == 1 || n == 2) {
// Base case 1: the next even number after 1 is 2
// Base case 2: the next even number after 2 is 4
return n + 1;
} else {
// Recursive case: the next even number after n is the next even number after n - 2
return nextEven(n - 2);
}
}
}
In this program, the RecursiveFunctions class contains the main method, which is the entry point of the program. The main method calls the nextEven() method with the number 12 as an argument and prints out the result.
The nextEven() method is a recursive method that calculates the next even number after a given number. It has two base cases: one for the n == 1 condition, where it returns 2, and one for the n == 2 condition, where it returns 4. It also has a recursive case for the n > 2 condition, where it calls itself with n - 2 as an argument. This allows the nextEven() method to calculate the next even number after a given number by calling itself repeatedly with smaller and smaller numbers until it reaches one of the base cases.
The nextEven() method is a standard method that is defined within the RecursiveFunctions class. It has a return type of int, which means it returns an integer value. The method uses recursion to solve the problem of finding the next even number after a given number.
To use the nextEven() method in the main method, we simply call it with the desired number as an argument and store the result in a variable. We can then use the System.out.println() method to print out the result to the console.
//standard methods implemented (hashcode, etc.)
public class RecursiveFunctions {
public static void main(String[] args) {
int n = 12;
int result = nextEven(n);
System.out.println("The next even number after " + n + " is " + result);
// Use the toString() method to convert the result to a string
String resultString = Integer.toString(result);
System.out.println("The string representation of the result is " + resultString);
// Use the equals() method to compare the result to another integer
int otherNumber = 14;
boolean isEqual = (result == otherNumber);
System.out.println("The result is equal to " + otherNumber + ": " + isEqual);
// Use the hashCode() method to get the hash code of the result
Integer resultInteger = new Integer(result);
int resultHashCode = resultInteger.hashCode();
System.out.println("The hash code of the result is " + resultHashCode);
}
public static int nextEven(int n) {
if (n == 1 || n == 2) {
// Base case 1: the next even number after 1 is 2
// Base case 2: the next even number after 2 is 4
return n + 1;
} else {
// Recursive case: the next even number after n is the next even number after n - 2
return nextEven(n - 2);
}
}
}