Lab 05 Assignment This Programming Project deals a two well-known arithmetic cal
ID: 3858967 • Letter: L
Question
Lab 05 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
Solution :
code :
package recursionmainclass;
public class FactorialClass {
static int printFactorials(){
int i =1;
long fact=1;
try{
while(true){
System.out.println(" "+i+"! " + fact );
fact = fact * i;
i++;
if(fact <= 0){
throw new Exception();
}
}
}catch(Exception e){
System.out.println(" Overflow occured for number :"+ i);
}
return i;
}
}
package recursionmainclass;
public class FibonacciClass {
static int printFebonacci(){
long a =0, b=1;
long fibo=a+b;
int i = 2;
System.out.println(" 1st term : 0");
System.out.println(" 2nd term : 1");
try{
while(true){
System.out.println(" "+i+"- term : " + fibo );
a = b;
b = fibo;
fibo = a + b;
i++;
if(fibo <= 0){
throw new Exception();
}
}
}catch(Exception e){
System.out.println(" Overflow occured for number :"+ i);
}
return i;
}
}
package recursionmainclass;
public class RecursionMainClass {
public static void main(String[] args) {
FactorialClass.printFactorials();
FibonacciClass.printFebonacci();
}
}
Output :
1! 1
2! 1
3! 2
4! 6
5! 24
6! 120
7! 720
8! 5040
9! 40320
10! 362880
11! 3628800
12! 39916800
13! 479001600
14! 6227020800
15! 87178291200
16! 1307674368000
17! 20922789888000
18! 355687428096000
19! 6402373705728000
20! 121645100408832000
21! 2432902008176640000
Overflow occured for number :22
1st term : 0
2nd term : 1
2- term : 1
3- term : 2
4- term : 3
5- term : 5
6- term : 8
7- term : 13
8- term : 21
9- term : 34
10- term : 55
11- term : 89
12- term : 144
13- term : 233
14- term : 377
15- term : 610
16- term : 987
17- term : 1597
18- term : 2584
19- term : 4181
20- term : 6765
21- term : 10946
22- term : 17711
23- term : 28657
24- term : 46368
25- term : 75025
26- term : 121393
27- term : 196418
28- term : 317811
29- term : 514229
30- term : 832040
31- term : 1346269
32- term : 2178309
33- term : 3524578
34- term : 5702887
35- term : 9227465
36- term : 14930352
37- term : 24157817
38- term : 39088169
39- term : 63245986
40- term : 102334155
41- term : 165580141
42- term : 267914296
43- term : 433494437
44- term : 701408733
45- term : 1134903170
46- term : 1836311903
47- term : 2971215073
48- term : 4807526976
49- term : 7778742049
50- term : 12586269025
51- term : 20365011074
52- term : 32951280099
53- term : 53316291173
54- term : 86267571272
55- term : 139583862445
56- term : 225851433717
57- term : 365435296162
58- term : 591286729879
59- term : 956722026041
60- term : 1548008755920
61- term : 2504730781961
62- term : 4052739537881
63- term : 6557470319842
64- term : 10610209857723
65- term : 17167680177565
66- term : 27777890035288
67- term : 44945570212853
68- term : 72723460248141
69- term : 117669030460994
70- term : 190392490709135
71- term : 308061521170129
72- term : 498454011879264
73- term : 806515533049393
74- term : 1304969544928657
75- term : 2111485077978050
76- term : 3416454622906707
77- term : 5527939700884757
78- term : 8944394323791464
79- term : 14472334024676221
80- term : 23416728348467685
81- term : 37889062373143906
82- term : 61305790721611591
83- term : 99194853094755497
84- term : 160500643816367088
85- term : 259695496911122585
86- term : 420196140727489673
87- term : 679891637638612258
88- term : 1100087778366101931
89- term : 1779979416004714189
90- term : 2880067194370816120
91- term : 4660046610375530309
92- term : 7540113804746346429
Overflow occured for number :93
BUILD SUCCESSFUL (total time: 0 seconds)
If you have any doubt then you can ask in comment section.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.