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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
In the program, a class named 'Ship' is defined.
|
| 2 |
+
The class 'PassengerShip' inherits from the 'Ship' class.
|
| 3 |
+
Additionally, the class 'LuxuryCruiser' inherits from the 'PassengerShip' class.
|
| 4 |
+
|
| 5 |
+
A ship has a crew.
|
| 6 |
+
A passenger ship also has passengers.
|
| 7 |
+
A luxury cruiser has, in addition to these, servants as well.
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
Familiarize yourself with the getter methods offered by these classes, and then write the method:
|
| 12 |
+
public static int peopleOnShips(ArrayList<Ship> ships)
|
| 13 |
+
|
| 14 |
+
This method receives a list of ships of different types as a parameter,
|
| 15 |
+
and calculates and returns the total number of all people on all the ships.
|
| 16 |
+
This number includes the crew, passengers, and servants.
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
import java.util.Random;
|
| 25 |
+
import java.util.ArrayList;
|
| 26 |
+
|
| 27 |
+
public class Test{
|
| 28 |
+
public static void main(String[] args){
|
| 29 |
+
final Random r = new Random();
|
| 30 |
+
|
| 31 |
+
ArrayList<Ship> ships = new ArrayList<Ship>();
|
| 32 |
+
for(int i = 0; i < 20; i++){
|
| 33 |
+
int shipType = r.nextInt(3);
|
| 34 |
+
switch(shipType){
|
| 35 |
+
case 0:
|
| 36 |
+
ships.add(new Ship(r.nextInt(40) + 10));
|
| 37 |
+
break;
|
| 38 |
+
case 1:
|
| 39 |
+
ships.add(new PassengerShip(r.nextInt(40) + 10, r.nextInt(40) + 500));
|
| 40 |
+
break;
|
| 41 |
+
case 2:
|
| 42 |
+
ships.add(new LuxuryCruiser(r.nextInt(40) + 10, r.nextInt(40) + 500, r.nextInt(40) + 1000));
|
| 43 |
+
break;
|
| 44 |
+
}
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
System.out.println("" + ships);
|
| 48 |
+
|
| 49 |
+
System.out.println("People on ships: " + peopleOnShips(ships));
|
| 50 |
+
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
//ADD
|
| 54 |
+
// LuxuryCruiser extends PassengerShip
|
| 55 |
+
// PassengerShip extends Ship
|
| 56 |
+
// Ship
|
| 57 |
+
public static int peopleOnShips(ArrayList<Ship> ships) {
|
| 58 |
+
int people = 0;
|
| 59 |
+
for (Ship ship : ships) {
|
| 60 |
+
people += ship.getCrew();
|
| 61 |
+
if (ship instanceof PassengerShip) {
|
| 62 |
+
// explicit casting as subclass, then call method
|
| 63 |
+
people += ((PassengerShip) ship).getPassengers();
|
| 64 |
+
}
|
| 65 |
+
// cannot use else if, because 'LuxuryCruiser' is also an instance of 'PassengerShip'
|
| 66 |
+
// and we need to extract count of servants from 'LuxuryCruiser'
|
| 67 |
+
// else if (ship instanceof LuxuryCruiser) {
|
| 68 |
+
if (ship instanceof LuxuryCruiser) {
|
| 69 |
+
// explicit casting as subclass, then call method
|
| 70 |
+
people += ((LuxuryCruiser) ship).getServants();
|
| 71 |
+
}
|
| 72 |
+
}
|
| 73 |
+
return people;
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
class Ship{
|
| 89 |
+
|
| 90 |
+
private int crew;
|
| 91 |
+
|
| 92 |
+
public Ship(int crew){
|
| 93 |
+
this.crew = crew;
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
public int getCrew(){
|
| 97 |
+
return crew;
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
@Override
|
| 101 |
+
public String toString(){
|
| 102 |
+
return "Ship (" + crew + ")";
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
class PassengerShip extends Ship{
|
| 112 |
+
|
| 113 |
+
private int passengers;
|
| 114 |
+
|
| 115 |
+
public PassengerShip(int crew, int passengers){
|
| 116 |
+
super(crew);
|
| 117 |
+
this.passengers = passengers;
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
public int getPassengers(){
|
| 121 |
+
return this.passengers;
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
@Override
|
| 125 |
+
public String toString(){
|
| 126 |
+
return "Passenger Ship (" + (getCrew() + passengers) + ")";
|
| 127 |
+
}
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
class LuxuryCruiser extends PassengerShip{
|
| 135 |
+
|
| 136 |
+
private int servants;
|
| 137 |
+
|
| 138 |
+
public LuxuryCruiser(int crew, int passengers, int servants){
|
| 139 |
+
super(crew, passengers);
|
| 140 |
+
this.servants = servants;
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
public int getServants(){
|
| 145 |
+
return this.servants;
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
@Override
|
| 149 |
+
public String toString(){
|
| 150 |
+
return "Luxury Cruiser (" + (getCrew() + getPassengers() + servants) + ")";
|
| 151 |
+
}
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
[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)]
|
| 165 |
+
People on ships: 15048
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
|