adding a test case
[IRC.git] / Robust / src / Tests / AbstractTest.java
1 //: interfaces/music4/Music4.java
2 // Abstract classes and methods.
3
4 abstract class Instrument {
5   public Instrument(){}
6   private int i; // Storage allocated for each
7   public abstract void play(int n);
8   public String what() { return "Instrument"; }
9   public abstract void adjust();
10 }
11
12 class Wind extends Instrument {
13   public Wind(){}
14   public void play(int n) {
15     System.out.println("Wind.play() " + n);
16   }
17   public String what() { return "Wind"; }
18   public void adjust() {}
19 }
20
21 class Percussion extends Instrument {
22   public Percussion(){}
23   public void play(int n) {
24     System.out.println("Percussion.play() " + n);
25   }
26   public String what() { return "Percussion"; }
27   public void adjust() {}
28 }
29
30 class Stringed extends Instrument {
31   public Stringed(){}
32   public void play(int n) {
33     System.out.println("Stringed.play() " + n);
34   }
35   public String what() { return "Stringed"; }
36   public void adjust() {}
37 }
38
39 class Brass extends Wind {
40   public Brass(){}
41   public void play(int n) {
42     System.out.println("Brass.play() " + n);
43   }
44   public void adjust() { System.out.println("Brass.adjust()"); }
45 }
46
47 class Woodwind extends Wind {
48   public Woodwind(){}
49   public void play(int n) {
50     System.out.println("Woodwind.play() " + n);
51   }
52   public String what() { return "Woodwind"; }
53 }
54
55
56 public class AbstractTest {
57   
58   public AbstractTest() {}
59   
60   // Doesn’t care about type, so new types
61   // added to the system still work right:
62   static void tune(Instrument i) {
63     // ...
64     i.play(9);
65   }
66   static void tuneAll(Instrument[] e) {
67     for(int k = 0; k < e.length; k++) {
68       Instrument i = e[k];
69       tune(i);
70     }
71   }
72   public static void main(String[] args) {
73     // Upcasting during addition to the array:
74     Instrument[] orchestra = new Instrument[5];
75     orchestra[0] = new Wind();
76     orchestra[1] = new Percussion();
77     orchestra[2] = new Stringed();
78     orchestra[3] = new Brass();
79     orchestra[4] = new Woodwind();
80     tuneAll(orchestra);
81   }
82 } /* Output:
83 Wind.play() MIDDLE_C
84 Percussion.play() MIDDLE_C
85 Stringed.play() MIDDLE_C
86 Brass.play() MIDDLE_C
87 Woodwind.play() MIDDLE_C
88 *///:~