Assignment This Programming Project deals two well-known arithmetic calculations
ID: 3852860 • Letter: A
Question
Assignment
This Programming Project deals 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.
Example Output:
0 Factorial = 1
1 Factorial = 1
2 Factorial = 2
3 Factorial = 6
4 Factorial = 24
5 Factorial = 120
6 Factorial = 720
7 Factorial = 5,040
8 Factorial = 40,320
9 Factorial = 362,880
10 Factorial = 3,628,800
11 Factorial = 39,916,800
12 Factorial = 479,001,600
13 Factorial = 6,227,020,800
14 Factorial = 87,178,291,200
15 Factorial = 1,307,674,368,000
16 Factorial = 20,922,789,888,000
17 Factorial = 355,687,428,096,000
18 Factorial = 6,402,373,705,728,000
19 Factorial = 121,645,100,408,832,000
20 Factorial = 2,432,902,008,176,640,000
java.lang.Exception:
21 Factorial = -4,249,290,049,419,214,848 - Overflow
0 Fibonnaci = 0
1 Fibonnaci = 1
2 Fibonnaci = 1
3 Fibonnaci = 2
4 Fibonnaci = 3
5 Fibonnaci = 5
6 Fibonnaci = 8
7 Fibonnaci = 13
8 Fibonnaci = 21
9 Fibonnaci = 34
10 Fibonnaci = 55
11 Fibonnaci = 89
12 Fibonnaci = 144
13 Fibonnaci = 233
14 Fibonnaci = 377
15 Fibonnaci = 610
16 Fibonnaci = 987
17 Fibonnaci = 1,597
18 Fibonnaci = 2,584
19 Fibonnaci = 4,181
20 Fibonnaci = 6,765
21 Fibonnaci = 10,946
22 Fibonnaci = 17,711
23 Fibonnaci = 28,657
24 Fibonnaci = 46,368
25 Fibonnaci = 75,025
26 Fibonnaci = 121,393
27 Fibonnaci = 196,418
28 Fibonnaci = 317,811
29 Fibonnaci = 514,229
30 Fibonnaci = 832,040
31 Fibonnaci = 1,346,269
32 Fibonnaci = 2,178,309
33 Fibonnaci = 3,524,578
34 Fibonnaci = 5,702,887
35 Fibonnaci = 9,227,465
36 Fibonnaci = 14,930,352
37 Fibonnaci = 24,157,817
38 Fibonnaci = 39,088,169
39 Fibonnaci = 63,245,986
40 Fibonnaci = 102,334,155
41 Fibonnaci = 165,580,141
42 Fibonnaci = 267,914,296
43 Fibonnaci = 433,494,437
44 Fibonnaci = 701,408,733
Explanation / Answer
If did not implement the classes the way you want, please comment me. Always happy to help. Thankyou
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.