Spaces:
Running
Running
| 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 | |