Java (don\'t copy paste from another person plz) and don\'t paste picture on her
ID: 3837156 • Letter: J
Question
Java
(don't copy paste from another person plz) and don't paste picture on here.
Create a program that will do one of two functions using a menu, like so:
1. Do Catalan numbers
2. Do Fibonacci numbers (recursive)
0. Quit
Enter selection: 1
Enter Catalan number to calculate: 3
Catalan number at 3 is 5
1. Do Catalan numbers
2. Do Fibonacci numbers (recursive)
0. Quit
Enter selection: 2
Enter Fibonacci number to calculate: 6
Fibonacci number 5 is 8
Create a function of catalan that will take a parameter and return the Catalan number. Also create a function of Fibonacci that will take a parameter, calculate using recursion the Fibonacci number and return the number.
Main will have a do/while loop that will print the menu, enter the selection, and call the functions. Have it quit on 0.
Explanation / Answer
MenuCatalanFibonacci.java
import java.util.Scanner;
public class MenuCatalanFibonacci {
public static void main(String[] args) {
int choice, number;
long catalanNum, finbonacciNum;
// Scanner object is used to get the inputs entered by the user
Scanner sc = new Scanner(System.in);
do {
//Displaying the menu
System.out.println(" :: Menu ::");
System.out.println("1. Do Catalan numbers");
System.out.println("2. Do Fibonacci numbers (recursive)");
System.out.println("0. Quit");
//getting choice entered by the user
System.out.print(" Enter selection:");
choice = sc.nextInt();
//based on the user choice the corresponding case will be executed.
switch (choice) {
case 1: {
//getting the number entered by the user
System.out.print("Enter Catalan number to calculate: ");
number = sc.nextInt();
//calling the function which calculate catalan number
catalanNum = calCatalanNum(number);
//displaying the result
System.out.println("Catalan number at " + number + " is "+ catalanNum);
break;
}
case 2: {
//getting the number entered by the user
System.out.print("Enter Fibonacci number to calculate:");
number = sc.nextInt();
//calling the function which calculate nth fibonacci number
finbonacciNum = fibonacci(number);
//displaying the nth fibonacci number
System.out.print("Fibonacci number " + number + " is "+ finbonacciNum);
break;
}
case 0: {
System.out.println("** Program Exit **");
break;
}
default: {
System.out.println("Invalid Input.");
break;
}
}
} while (choice != 0);
}
/*
* This fibonacci() method will calculate the fibonocci numbers recursively
* and return to the caller Params: number of type integer Return :
* fibonocci number of type integer
*/
private static int fibonacci(int index) {
if (index == 0) {
return 0;
} else if (index == 1) {
return 1;
} else {
return fibonacci(index - 1) + fibonacci(index - 2);
}
}
/* This method will find the catalan number recursively
* @param : number of type integer
* @return : Catalan number of type Long
*/
private static long calCatalanNum(int number) {
long catalanNum = 0;
if (number <= 1) {
return 1;
}
for (int i = 0; i < number; i++) {
catalanNum += calCatalanNum(i) * calCatalanNum(number - i - 1);
}
return catalanNum;
}
}
_____________________
Output:
:: Menu ::
1. Do Catalan numbers
2. Do Fibonacci numbers (recursive)
0. Quit
Enter selection:1
Enter Catalan number to calculate: 3
Catalan number at 3 is 5
:: Menu ::
1. Do Catalan numbers
2. Do Fibonacci numbers (recursive)
0. Quit
Enter selection:2
Enter Fibonacci number to calculate:6
Fibonacci number 6 is 8
:: Menu ::
1. Do Catalan numbers
2. Do Fibonacci numbers (recursive)
0. Quit
Enter selection:0
** Program Exit **
______________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.