forked from Fuseken/code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcars.java
More file actions
58 lines (48 loc) · 1.13 KB
/
cars.java
File metadata and controls
58 lines (48 loc) · 1.13 KB
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
import java.util.*;
/**
* component
*/
interface CarParts {
public double getWeight();
}
public class Assembly implemenets Part {
/**
* all components
*/
private ArrayList<CarParts> carComponents = new ArrayList<CarParts>();
/**
* getWeight for whole car
*/
public double getWeight() {
double totalWeight = 0;
for( CarParts part : carComponents ) {
totalWeight += part.getWeight();
}
return totalWeight;
}
}
public class Part implements CarParts {
/**
* component: weight
*/
private double weight;
/**
* weight of this part
*/
public double getWeight() {
return weight;
}
}
public class Program {
public static void main(String[] args) {
Part chassis = new CarParts();
Part engine = new CarParts();
Part body = new CarParts();
Assembly porsche = new Assembly();
porsche.add(chassis);
porsche.add(engine);
porsche.add(body);
double porscheWeight = porsche.getWeight;
System.out.println("porsche weights " + porscheWeight);
}
}