-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise26.java
More file actions
104 lines (91 loc) · 2.95 KB
/
Copy pathExercise26.java
File metadata and controls
104 lines (91 loc) · 2.95 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
import java.util.*;
/**
* @author viktorvoltz
*/
class RandomInstrumentGenerator {
private Random rand = new Random();
public Instrument next() {
switch(rand.nextInt(7)) {
default:
case 0: return new Wind();
case 1: return new Percussion();
case 2: return new Stringed();
case 3: return new Keyboard();
case 4: return new Brass();
case 5: return new Woodwind();
case 6: return new Piano();
}
}
}
enum Note{
MIDDLE_C,
C_SHARP,
B_FLAT
}
class Instrument {
void play(Note n) { System.out.println("Instrument.play() " + n); }
public String toString() { return "Instrument"; }
void adjust() { System.out.println("Adjusting Instrument"); }
}
class Wind extends Instrument {
void play(Note n) { System.out.println("Wind.play() " + n); }
public String toString() { return "Wind"; }
void adjust() { System.out.println("Adjusting Wind"); }
void clearSpitValve() { System.out.println("Wind clearing spit valve"); }
}
class Percussion extends Instrument {
void play(Note n) { System.out.println("Percussion.play() " + n); }
public String toString() { return "Percussion"; }
void adjust() { System.out.println("Adjusting Percussion"); }
}
class Stringed extends Instrument {
void play(Note n) { System.out.println("Stringed.play() " + n); }
public String toString() { return "Stringed"; }
void adjust() { System.out.println("Adjusting Stringed"); }
}
class Keyboard extends Instrument {
void play(Note n) { System.out.println("Keyboard.play() " + n); }
public String toString() { return "Keyboard"; }
void adjust() { System.out.println("Adjusting Keyboard"); }
}
class Brass extends Wind {
void play(Note n) { System.out.println("Brass.play() " + n); }
public String toString() { return "Brass"; }
void adjust() { System.out.println("Adjusting Brass"); }
void clearSpitValve() { System.out.println("Brass clearing spit valve"); }
}
class Woodwind extends Wind {
void play(Note n) { System.out.println("Woodwind.play() " + n); }
public String toString() { return "Woodwind"; }
void clearSpitValve() { System.out.println("Woodwind clearing spit valve"); }
}
class Piano extends Keyboard {
void play(Note n) { System.out.println("Piano.play() " + n); }
public String toString() { return "Piano"; }
}
class Music26 {
// Doesn't care about type, so new types
// added to the system still work right:
public static void tune(Instrument i) {
//...
i.play(Note.MIDDLE_C);
}
public static void tuneAll(Instrument[] e) {
for(Instrument i : e)
tune(i);
}
private static RandomInstrumentGenerator gen = new RandomInstrumentGenerator();
public static void main(String[] args) {
// Upcasting during addition to the array:
Instrument[] orchestra = new Instrument[20];
// fill up orchestra array wth instruments:
for(int i = 0; i < orchestra.length; i++)
orchestra[i] = gen.next();
for(Instrument i : orchestra) {
if(i instanceof Wind) // get RTTI
((Wind)i).clearSpitValve();
i.adjust();
}
tuneAll(orchestra);
}
}