KaiquanMah commited on
Commit
eeaf44a
·
verified ·
1 Parent(s): f94ce27

Create 08. Write class SpecialDirector

Browse files
Week 5: Class hierarchies/08. Write class SpecialDirector ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ The program defines the class 'Director'.
2
+ Write the class 'SpecialDirector', which inherits the 'Director' class.
3
+
4
+ The class must have a constructor that takes as parameters,
5
+ in this order, name, hours, overtime hours, and as a new parameter, special hours.
6
+ This parameter is also an integer.
7
+
8
+ In the inheriting class, overwrite the method 'totalHours' so that it also counts special hours in the total number of hours.
9
+
10
+
11
+
12
+ import java.util.Random;
13
+
14
+ public class Test{
15
+ public static void main(String[] args){
16
+ final Random r = new Random();
17
+
18
+
19
+ System.out.println("Testing class SpecialDirector...");
20
+ String[] names = {"Sandra Special", "Sean Spessu", "Mike Monroe"};
21
+
22
+
23
+ for (String name : names) {
24
+ int hours = r.nextInt(15);
25
+ int overtimeHours = r.nextInt(15);
26
+ int specialHours = r.nextInt(15);
27
+
28
+ SpecialDirector director = new SpecialDirector(name, hours,
29
+ overtimeHours, specialHours);
30
+
31
+ System.out.println("Object created using parameters...");
32
+ System.out.println("Hours: " + hours);
33
+ System.out.println("Overtime hours: " + overtimeHours);
34
+ System.out.println("Special hours: " + specialHours);
35
+
36
+ System.out.println("Method totalHours returns " + director.totalHours());
37
+
38
+ System.out.println("");
39
+
40
+ }
41
+ }
42
+ }
43
+
44
+ class Director {
45
+ protected String name;
46
+ protected int hours;
47
+ protected int overtime;
48
+
49
+ public Director(String name, int hours, int overtime) {
50
+ this.name = name;
51
+ this.hours = hours;
52
+ this.overtime = overtime;
53
+ }
54
+
55
+ public int totalHours() {
56
+ return hours + overtime;
57
+ }
58
+ }
59
+
60
+
61
+
62
+ //ADD
63
+ class SpecialDirector extends Director {
64
+ private int specialHours;
65
+
66
+ //constructor
67
+ public SpecialDirector(String name, int hours, int overtime, int specialHours) {
68
+ // initialise params found in superclass
69
+ super(name, hours, overtime);
70
+ // initialise param only found in subclass
71
+ this.specialHours = specialHours;
72
+ }
73
+
74
+ // SAME METHOD NAME
75
+ // SAME PARAMS/NAMES n num_params - NO PARAMS
76
+ // SAME PARAM ORDER, DTYPE
77
+ public int totalHours() {
78
+ return this.hours + this.overtime + this.specialHours;
79
+ }
80
+ }
81
+
82
+
83
+
84
+
85
+
86
+ Testing class SpecialDirector...
87
+ Object created using parameters...
88
+ Hours: 2
89
+ Overtime hours: 2
90
+ Special hours: 5
91
+ Method totalHours returns 9
92
+
93
+ Object created using parameters...
94
+ Hours: 11
95
+ Overtime hours: 0
96
+ Special hours: 0
97
+ Method totalHours returns 11
98
+
99
+ Object created using parameters...
100
+ Hours: 5
101
+ Overtime hours: 6
102
+ Special hours: 0
103
+ Method totalHours returns 11
104
+
105
+