-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
116 lines (90 loc) · 2.61 KB
/
Copy pathMain.java
File metadata and controls
116 lines (90 loc) · 2.61 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
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
class Main {
static abstract class KitchenAppliance {
protected double weight;
protected String location;
protected KitchenAppliance(double weight) {
this.weight = weight;
this.location = "table";
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public abstract void turnOn();
public abstract void wash();
}
static class Microwave extends KitchenAppliance {
public Microwave() {
super(5000);
}
@Override
public void turnOn() {
System.out.println("turning on microwave...");
}
@Override
public void wash() {
throw new UnsupportedOperationException("cannot wash a microwave");
}
}
static abstract class Tableware extends KitchenAppliance {
protected Tableware(double weight) {
super(weight);
}
@Override
public void turnOn() {
throw new UnsupportedOperationException("cannot turn on a tableware");
}
}
static class Fork extends Tableware {
public Fork() {
super(150);
}
@Override
public void wash() {
System.out.println("washing a fork...");
}
}
static class Dish extends Tableware {
public Dish() {
super(300);
}
@Override
public void wash() {
System.out.println("washing a dish...");
}
}
public static void main(String[] args) {
System.out.println("hello world");
System.out.println();
KitchenAppliance appliance;
appliance = new Microwave();
System.out.println("Microwave.getWeight() = " + appliance.getWeight());
System.out.println("Microwave.getLocation() = " + appliance.getLocation());
System.out.print("Microwave.turnOn() ");
appliance.turnOn();
System.out.println();
appliance = new Fork();
System.out.println("Fork.getWeight() = " + appliance.getWeight());
System.out.println("Fork.getLocation() = " + appliance.getLocation());
System.out.print("Fork.wash() ");
appliance.wash();
System.out.println();
appliance = new Dish();
System.out.println("Dish.getWeight() = " + appliance.getWeight());
System.out.println("Dish.getLocation() = " + appliance.getLocation());
System.out.print("Dish.wash() ");
appliance.wash();
appliance.setLocation("fridge");
System.out.println();
System.out.println("Dish.setLocation('fridge')");
System.out.println("Dish.getLocation() = " + appliance.getLocation());
}
}