File size: 2,748 Bytes
eeaf44a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
The program defines the class 'Director'. 
Write the class 'SpecialDirector', which inherits the 'Director' class.

The class must have a constructor that takes as parameters, 
in this order, name, hours, overtime hours, and as a new parameter, special hours. 
This parameter is also an integer.

In the inheriting class, overwrite the method 'totalHours' so that it also counts special hours in the total number of hours.



import java.util.Random;

public class Test{
    public static void main(String[] args){
        final Random r = new Random();
        
        
        System.out.println("Testing class SpecialDirector...");
        String[] names = {"Sandra Special", "Sean Spessu", "Mike Monroe"};
      
        
        for (String name : names) {
            int hours = r.nextInt(15);
            int overtimeHours = r.nextInt(15);
            int specialHours = r.nextInt(15);
          
            SpecialDirector director = new SpecialDirector(name, hours, 
                    overtimeHours, specialHours);
        
            System.out.println("Object created using parameters...");
            System.out.println("Hours: " + hours);
            System.out.println("Overtime hours: " + overtimeHours);
            System.out.println("Special hours: " + specialHours);
            
            System.out.println("Method totalHours returns " + director.totalHours());
            
            System.out.println("");
        
        }
    }
}

class Director {
    protected String name;
    protected int hours;
    protected int overtime;
    
    public Director(String name, int hours, int overtime) {
        this.name = name;
        this.hours = hours;
        this.overtime = overtime;
    }
    
    public int totalHours() {
        return hours + overtime;
    }
}



//ADD
class SpecialDirector extends Director {
    private int specialHours;
    
    //constructor
    public SpecialDirector(String name, int hours, int overtime, int specialHours) {
        // initialise params found in superclass
        super(name, hours, overtime);
        // initialise param only found in subclass
        this.specialHours = specialHours;
    }

    // SAME METHOD NAME
    // SAME PARAMS/NAMES n num_params - NO PARAMS
    // SAME PARAM ORDER, DTYPE
    public int totalHours() {
        return this.hours + this.overtime + this.specialHours;
    }
}





Testing class SpecialDirector...
Object created using parameters...
Hours: 2
Overtime hours: 2
Special hours: 5
Method totalHours returns 9

Object created using parameters...
Hours: 11
Overtime hours: 0
Special hours: 0
Method totalHours returns 11

Object created using parameters...
Hours: 5
Overtime hours: 6
Special hours: 0
Method totalHours returns 11