This Programming Project deals a two well-known arithmetic calculations that can
ID: 3859464 • Letter: T
Question
This Programming Project deals a two well-known arithmetic calculations that can be implemented recursively.
The project will have 3 class files:
FactorialClass.java
FibinnaciClass.java
RecursionMainClass.java
The FactorialClass.java will calculate a factorial for a long type argument to a FactorialClass class method.
The FibinnaciClass.java will calculate the Fibonacci sum for an int type argument to a FibinnaciClass class method.
Both classes should display the results in a well formatted manner. See the sample output below for required formatting details, use commas and right justification in the number results for both calculations.
Each of the calculation classes should detect overflow using the Exception class try-catch technique described in the book and slides (result <= 0). The overflow exception must cause the parameter that caused the overflow and the overflow value to be displayed.
In a main program, write a loop for each calculation that will use inputs from 1 to a number that will cause overflow. The loop for each calculation should display each result up until the overflow happens.
Assignment This Programming Project deals a two well-known arithmetic calculations that can be implemented recursively The project will have 3 class files FactorialClass.java FibinnaciClass.java RecursionMainClass.java The FactorialClass.java will calculate a factorial for a long type argument to a FactorialClass class method. The FibinnaciClass.java will calculate the Fibonacci sum for an int type argument to a FibinnaciClass class method. Both classes should display the results in a well formatted manner. See the sample output below for required formatting details, use commas and right justification in the number results for both calculations. Each of the calculation classes should detect overflow using the Exception class try-catch technique described in the book and slides (resultExplanation / Answer
FactorialClass.java
public class FactorialClass{
public static long findFactorial(long x){
if(x==0||x==1) return 1;
return x*findFactorial(x-1);
}
}
FibonnaciClass.java
public class FibonnaciClass{
public static int findXthFibonnaciNumber(int x){
if(x==0) return 0;
else if(x==1) return 1;
else return findXthFibonnaciNumber(x-1)+findXthFibonnaciNumber(x-2);
}
}
RecursionMainClass.java
public class RecursionMainClass{
public static void main(String a[]){
//Factorial numbers list
long num1 = 0;
while(true){
long result=0;
try{
result = FactorialClass.findFactorial(num1);
if(result<=0)
throw new Exception();
System.out.println(num1+" Factorial = "+String.format("%, d",result));
//System.out.println(FibonnaciClass.findXthFibonnaciNumber(3));
}
catch(Exception e){
System.out.println("java.lang.Exception:");
System.out.println(num1+" Factorial = "+String.format("%, d",result)+" - Overflow");
break;
}
num1++;
}
//Fibonnaci numbers list
int num2 = 0;
while(true){
int result=-1;
try{
result = FibonnaciClass.findXthFibonnaciNumber(num2);
if(result<0)
throw new Exception();
System.out.println(num2+" Fibonnaci = "+String.format("%, d",result));
//System.out.println(FibonnaciClass.findXthFibonnaciNumber(3));
}
catch(Exception e){
System.out.println("java.lang.Exception:");
System.out.println(num2+" Fibonnaci = "+String.format("%, d",result)+" - Overflow");
break;
}
num2++;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.