-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInheritance.java
More file actions
130 lines (101 loc) · 3.78 KB
/
Inheritance.java
File metadata and controls
130 lines (101 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
public class Inheritance {
public class Progression {
protected long first;
protected long current;
Progression() {
current = first = 0;
}
protected long firstValue() {
current = first;
return current;
}
protected long nextValue() {
return ++current;
}
public void printProgression(int n) {
System.out.print(firstValue() + " ");
for (int i = 1; i < n; i++) {
System.out.print(nextValue() + " ");
}
System.out.println();
}
}
// ArithProgression class outside Progression but inside Inheritance
public class ArithProgression extends Progression {
protected long increment;
ArithProgression() {
this(1);
}
ArithProgression(long inc) {
increment = inc;
}
protected long nextValue() {
current += increment;
return current;
}
}
// GeomProgression class outside Progression but inside Inheritance
public class GeomProgression extends Progression {
protected long base;
GeomProgression() {
this(2);
}
GeomProgression(long bse) {
base = bse;
first = 1;
current = first;
}
protected long nextValue() {
current *= base;
return current;
}
}
// FibonacciProgression class outside Progression but inside Inheritance
public class FibonacciProgression extends Progression {
long prev;
FibonacciProgression() {
this(0, 1);
}
FibonacciProgression(long value1, long value2) {
first = value1;
prev = value2 - value1;
current = first;
}
protected long nextValue() {
long temp = prev;
prev = current;
current += temp;
return current;
}
}
// TestProgression class outside Progression but inside Inheritance
public static class TestProgression {
public static void main(String[] args) {
Inheritance outer = new Inheritance();
// Testing the Arithmetic progression
System.out.println("Arithmetic progression with default increment:");
ArithProgression arithProg = outer.new ArithProgression();
arithProg.printProgression(10);
// Testing the Geometric progression
System.out.println("Geometric progression with base 2:");
GeomProgression geomProg = outer.new GeomProgression();
geomProg.printProgression(10);
// Testing the Fibonacci progression
System.out.println("Fibonacci progression:");
FibonacciProgression fibProg = outer.new FibonacciProgression();
fibProg.printProgression(10);
// Testing the parameterized constructor of ArithProgression
System.out.println("Arithmetic progression with increment 5:");
Inheritance.ArithProgression arithProgParm = outer.new ArithProgression(5);
arithProgParm.printProgression(10);
// Testing the parameterized constructor of GeomProgression
System.out.println("Geometric progression with base 3:");
Inheritance.GeomProgression geomProgParm = outer.new GeomProgression(3);
geomProgParm.printProgression(10);
// Testing the parameterized constructor of FibonacciProgression
System.out.println("Fibonacci progression starting with 4 and 6:");
Inheritance.FibonacciProgression fibProgParm = outer.new FibonacciProgression(4, 6);
fibProgParm.printProgression(10);
}
}
}