Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program that has 3 Methods: The first method will be called CallFib

ID: 3730846 • Letter: W

Question

Write a Java program that has 3 Methods:

The first method will be called CallFibOrFac

It will receive a single string as a parameter.

If the string is "Fib" it will call the second method called Fibonacci

If the string is "Fac" it will call the third method called Factorial.

The Second method prints the first 10 numbers in the Fibonacci sequence.

The Second method has a single integer as it's parameter, here we're going to pass it the value 10.

The Third method prints the value of 10 factorial.

The third method has a single integer as it's parameter, which tell it what number to compute the factorial of.

We want to make the second and third functions work for cases other than just the first 10 numbers so add a parameter to the Febonacci function and the Factorial function which will tell it how far to go along the Fibonacci sequence or the Factorial.

Explanation / Answer

CalFibFac.java

import java.util.Scanner;

public class CalFibFac {

public static void main(String[] args) {
//Declaring variables
String str;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);

//Getting the input entered by the user
System.out.print("Enter the input :");
str = sc.next();

//calling the method
CallFibOrFac(str);

}

//THis method will call either fibonacci or factorial method based on input str
private static void CallFibOrFac(String str) {
if (str.equalsIgnoreCase("Fib"))
Fibonacci(10);
else if (str.equalsIgnoreCase("Fac"))
Factorial(10);
}

//This method will calculate the factorial of n
private static void Factorial(int n) {
int res = 1;
for (int i = 1; i <= n; i++) {
res *= i;
}

System.out.println("Factorial of " + n + " is " + res);

}

//This method will display the fibonacci numbers based on n
private static void Fibonacci(int n) {
int first = 0, second = 1, third;
System.out.print("Displaying the fibonacci Series :");
System.out.print(first + " " + second + " ");
for (int i = 0; i < n - 2; i++) {
third = first + second;
first = second;
second = third;
System.out.print(third + " ");
}


}

}

___________________

Output#1:

Enter the input :Fib
Displaying the fibonacci Series :0 1 1 2 3 5 8 13 21 34

__________________

Output#2:

Enter the input :Fac
Factorial of 10 is 3628800

_________________Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote