File size: 4,038 Bytes
16c8976
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
In the program, a class named 'Ship' is defined. 
The class 'PassengerShip' inherits from the 'Ship' class. 
Additionally, the class 'LuxuryCruiser' inherits from the 'PassengerShip' class.

A ship has a crew. 
A passenger ship also has passengers. 
A luxury cruiser has, in addition to these, servants as well.



Familiarize yourself with the getter methods offered by these classes, and then write the method:
public static int peopleOnShips(ArrayList<Ship> ships)

This method receives a list of ships of different types as a parameter, 
and calculates and returns the total number of all people on all the ships. 
This number includes the crew, passengers, and servants.







import java.util.Random;
import java.util.ArrayList;

public class Test{
    public static void main(String[] args){
        final Random r = new Random();
        
        ArrayList<Ship> ships = new ArrayList<Ship>();
        for(int i = 0; i < 20; i++){
            int shipType = r.nextInt(3);
            switch(shipType){
                case 0:
                    ships.add(new Ship(r.nextInt(40) + 10));
                    break;
                case 1:
                    ships.add(new PassengerShip(r.nextInt(40) + 10, r.nextInt(40) + 500));
                    break;
                case 2:
                    ships.add(new LuxuryCruiser(r.nextInt(40) + 10, r.nextInt(40) + 500, r.nextInt(40) + 1000));
                    break;
            }
        }
        
        System.out.println("" + ships);
        
        System.out.println("People on ships: " + peopleOnShips(ships));
        
    }
    
    //ADD
    // LuxuryCruiser extends PassengerShip
    // PassengerShip extends Ship
    // Ship
    public static int peopleOnShips(ArrayList<Ship> ships) {
        int people = 0;
        for (Ship ship : ships) {
            people += ship.getCrew();
            if (ship instanceof PassengerShip) {
                // explicit casting as subclass, then call method
                people += ((PassengerShip) ship).getPassengers();
            }
            // cannot use else if, because 'LuxuryCruiser' is also an instance of 'PassengerShip'
            // and we need to extract count of servants from 'LuxuryCruiser'
            // else if (ship instanceof LuxuryCruiser) {
            if (ship instanceof LuxuryCruiser) {
                // explicit casting as subclass, then call method
                people += ((LuxuryCruiser) ship).getServants();
            }
        }
        return people;
    }







}





class Ship{
    
    private int crew;
    
    public Ship(int crew){
        this.crew = crew;
    }
    
    public int getCrew(){
        return crew;
    }
    
    @Override
    public String toString(){
        return "Ship (" + crew + ")";
    }
    
}





class PassengerShip extends Ship{
    
    private int passengers;
    
    public PassengerShip(int crew, int passengers){
        super(crew);
        this.passengers = passengers;
    }

    public int getPassengers(){
        return this.passengers;
    }
    
    @Override
    public String toString(){
        return "Passenger Ship (" + (getCrew() + passengers) + ")";
    }
}





class LuxuryCruiser extends PassengerShip{
    
    private int servants;
    
    public LuxuryCruiser(int crew, int passengers, int servants){
        super(crew, passengers);
        this.servants = servants;
    }

    
    public int getServants(){
        return this.servants;
    }
    
    @Override
    public String toString(){
        return "Luxury Cruiser (" + (getCrew() + getPassengers() + servants) + ")";
    }
}











[Luxury Cruiser (1570), Ship (44), Passenger Ship (563), Ship (44), Passenger Ship (579), Passenger Ship (528), Luxury Cruiser (1597), Ship (20), Passenger Ship (550), Passenger Ship (557), Luxury Cruiser (1577), Ship (41), Luxury Cruiser (1538), Passenger Ship (538), Passenger Ship (554), Luxury Cruiser (1582), Luxury Cruiser (1560), Luxury Cruiser (1553), Ship (32), Ship (21)]
People on ships: 15048