Spaces:
Running
Running
SubClass1 subClassVar1 = (SubClass1) superClassVar1;
Browse files
Week 6: Methods of OO Programming/11B. Calculate the Total Load of Trucks
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
In the program, a class Car and its subclass Truck are defined.
|
| 2 |
+
|
| 3 |
+
Write a method
|
| 4 |
+
public static int totalLoad(ArrayList<Car> cars)
|
| 5 |
+
|
| 6 |
+
that receives a list of Truck objects as a parameter.
|
| 7 |
+
The method calculates and returns the total load of the trucks.
|
| 8 |
+
Note that the list type is Car, not Truck!
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
import java.util.Random;
|
| 13 |
+
import java.util.ArrayList;
|
| 14 |
+
|
| 15 |
+
public class Test{
|
| 16 |
+
public static void main(String[] args){
|
| 17 |
+
final Random r = new Random();
|
| 18 |
+
|
| 19 |
+
String[] brands = "Sisu Scania Volvo Ford Fiat Citroen".split(" ");
|
| 20 |
+
|
| 21 |
+
for(int test=1; test<=3; test++) {
|
| 22 |
+
|
| 23 |
+
System.out.println("Test " + test);
|
| 24 |
+
|
| 25 |
+
ArrayList<Car> cars = new ArrayList<>();
|
| 26 |
+
|
| 27 |
+
final int size = r.nextInt(4) + 3;
|
| 28 |
+
for (int i=0; i<size; i++) {
|
| 29 |
+
cars.add(new Truck(brands[r.nextInt(brands.length)], r.nextInt(20) + 5));
|
| 30 |
+
}
|
| 31 |
+
System.out.println("Cars:");
|
| 32 |
+
cars.stream().forEach(a -> System.out.println("" + a));
|
| 33 |
+
|
| 34 |
+
System.out.println("Total load: " + totalLoad(cars));
|
| 35 |
+
System.out.println("");
|
| 36 |
+
|
| 37 |
+
}
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
//add
|
| 43 |
+
// round 1 - NO
|
| 44 |
+
// public static int totalLoad(ArrayList<Car> cars) {
|
| 45 |
+
// int totalLoad = 0;
|
| 46 |
+
// for (Truck truck : cars) {
|
| 47 |
+
// totalLoad += truck.getLoad();
|
| 48 |
+
// }
|
| 49 |
+
// return totalLoad;
|
| 50 |
+
// }
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
// round 2 - YES
|
| 54 |
+
public static int totalLoad(ArrayList<Car> cars) {
|
| 55 |
+
int totalLoad = 0;
|
| 56 |
+
for (Car car : cars) {
|
| 57 |
+
// explicit type casting
|
| 58 |
+
Truck truck = (Truck) car;
|
| 59 |
+
totalLoad += truck.getLoad();
|
| 60 |
+
}
|
| 61 |
+
return totalLoad;
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
class Car {
|
| 74 |
+
protected String brand;
|
| 75 |
+
|
| 76 |
+
public Car(String brand) {
|
| 77 |
+
this.brand = brand;
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
public String getBrand() {
|
| 81 |
+
return brand;
|
| 82 |
+
}
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
class Truck extends Car {
|
| 86 |
+
private int load;
|
| 87 |
+
|
| 88 |
+
public Truck(String brand, int load) {
|
| 89 |
+
super(brand);
|
| 90 |
+
this.load = load;
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
public int getLoad() {
|
| 94 |
+
return load;
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
public String toString() {
|
| 98 |
+
return brand + ", load: " + load;
|
| 99 |
+
}
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
Test 1
|
| 113 |
+
Cars:
|
| 114 |
+
Sisu, load: 5
|
| 115 |
+
Volvo, load: 24
|
| 116 |
+
Sisu, load: 23
|
| 117 |
+
Total load: 52
|
| 118 |
+
|
| 119 |
+
Test 2
|
| 120 |
+
Cars:
|
| 121 |
+
Fiat, load: 9
|
| 122 |
+
Fiat, load: 10
|
| 123 |
+
Scania, load: 13
|
| 124 |
+
Ford, load: 16
|
| 125 |
+
Total load: 48
|
| 126 |
+
|
| 127 |
+
Test 3
|
| 128 |
+
Cars:
|
| 129 |
+
Volvo, load: 18
|
| 130 |
+
Ford, load: 22
|
| 131 |
+
Scania, load: 20
|
| 132 |
+
Sisu, load: 20
|
| 133 |
+
Volvo, load: 7
|
| 134 |
+
Sisu, load: 21
|
| 135 |
+
Total load: 108
|
| 136 |
+
|
| 137 |
+
|