Due Date: April 22, 2024
Information: Factorials are a common example to illustrate recursion because the factorial of a number (n!) is defined as the product of all positive integers less than or equal to n. This definition naturally lends itself to a recursive approach.
Your files will be:
PX_Factorial_lastname.java (Actual Java program)
PX_Factorial_lastname.png (Screen shot of the program in the Eclipse IDE)
PX_Factorial_lastname.mp4 (Video)
(Video should include an explanation of the program
and showing it running successfully)
Here's what the program will do:
Define a method factorial that calculates the factorial of a number recursively.
Include a main method to take input from the user and display the factorial of the provided number.
You will need to update the file names.
I also have a couple of syntax errors.
Here's the Java code for this factorial calculator:
import java.util.Scanner;
public class FactorialCalculator {
// Recursive method to calculate factorial
public static long factorial(int n) {
if (n == 0) { // Base case: factorial of 0 is 1
return 1;
} else {{
return n * factorial(n - 1); // Recursive case
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a non-negative integer: ");
int number = scanner.nextInt();
if (number < 0) { {
System.out.println("Factorial is not defined for negative numbers.");
} else {
long result = factorial(number);
System.out.println("Factorial of " + number + " is " + result);
}
scanner.close();
}
}
How It Works:
factorial method: This method takes an integer n as an input. If n is 0, it returns 1 (since 0! = 1). Otherwise, it calls itself with n-1, multiplying the result by n to compute the factorial recursively.
main method: It prompts the user to enter a non-negative integer. After receiving input, it checks if the number is negative. If not, it calls the factorial method and prints the result.
Running the Program:
To run this program, you need to compile it first and then execute the compiled class:
Save the file as PX_Factorial_lastname.java.
Enter a non-negative integer when prompted to see its factorial.
This program provides a clear example of how recursion can be applied in Java to solve problems that can be broken down into simpler, self-similar tasks.
KP Java Programming – Course Outline
Section 1 - Getting Started
Java Basics
Data Types
Arrays and References
Operators and Constructs
Java Objects
Dynamic Memory Allocation
Java Methods
Java Strings
Section 2 - Classes and Objects
Class Design
Fields and Access Control
Constructors
Method Overloading
Static Methods
Inheritance
Method Overriding
Using
final
and
super
Abstract Classes and Methods
Dynamic Binding
Polymorphism
Section 3 - Working with Classes
Using
instanceof
Interfaces
Exception Handling
Exception Objects
throw points, throws clause
try, catch, finally
Section 4 - User Interfaces
Window Applications
Layout Managers
Event Handlers and Listeners
Anonymous Classes and Lambdas
Java Swing APIs
Basic GUI Controls
Menus and MenuBars
Section 5 - Generics and Collections
Why Use Generics?
Generic Classes and Interfaces
Generic Iterators
Collections
ArrayList, LinkedList, HashMap
Section 6 - Threads
Thread States
Extending the
Thread
class
Timer Thread
Implementing the
Runnable
interface
Section 7 - File I/O
Input and Output Streams
Binary and Text Files
Files and Directory Methods
Appendix
Java Language
JavaFX