File size: 2,486 Bytes
f5d3ba7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
In the program, a class Car and its subclass Truck are defined.

Write a method
public static int totalLoad(ArrayList<Car> cars)

that receives a list of Truck objects as a parameter. 
The method calculates and returns the total load of the trucks.
Note that the list type is Car, not Truck!



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

public class Test{
    public static void main(String[] args){
        final Random r = new Random();
        
        String[] brands = "Sisu Scania Volvo Ford Fiat Citroen".split(" ");
        
        for(int test=1; test<=3; test++) {
            
            System.out.println("Test " + test);
        
            ArrayList<Car> cars = new ArrayList<>();
            
            final int size = r.nextInt(4) + 3;
            for (int i=0; i<size; i++) {
                cars.add(new Truck(brands[r.nextInt(brands.length)], r.nextInt(20) + 5));
            }
            System.out.println("Cars:");
            cars.stream().forEach(a -> System.out.println("" + a));
            
            System.out.println("Total load: " + totalLoad(cars));
            System.out.println("");
    
        }
    }



    //add
    // round 1 - NO
    // public static int totalLoad(ArrayList<Car> cars) {
    //     int totalLoad = 0;
    //     for (Truck truck : cars) {
    //         totalLoad += truck.getLoad();
    //     }
    //     return totalLoad;
    // }


    // round 2 - YES
    public static int totalLoad(ArrayList<Car> cars) {
        int totalLoad = 0;
        for (Car car : cars) {
            // explicit type casting
            Truck truck = (Truck) car;
            totalLoad += truck.getLoad();
        }
        return totalLoad;
    }




}





class Car {
    protected String brand;
    
    public Car(String brand) {
        this.brand = brand;
    }

    public String getBrand() {
        return brand;
    }
}

class Truck extends Car {
    private int load;

    public Truck(String brand, int load) {
        super(brand);
        this.load = load;
    }
    
    public int getLoad() {
        return load;
    }
    
    public String toString() {
        return brand + ", load: " + load;
    }
}











Test 1
Cars:
Sisu, load: 5
Volvo, load: 24
Sisu, load: 23
Total load: 52

Test 2
Cars:
Fiat, load: 9
Fiat, load: 10
Scania, load: 13
Ford, load: 16
Total load: 48

Test 3
Cars:
Volvo, load: 18
Ford, load: 22
Scania, load: 20
Sisu, load: 20
Volvo, load: 7
Sisu, load: 21
Total load: 108