AndreyElagin
9/8/2016 - 7:57 PM

music.java

enum Note {
    MIDDLE_C, C_SHARP, B_FLAT; // Etc.
}

abstract class Plays extends Print {
    abstract void play(Note n);

    abstract String what();

    abstract void adjust();
}

class Instrument extends Plays {
    private String instrument;

    Instrument(String instrument) {
        System.out.println(instrument);
        this.instrument = instrument;
    }

    void play(Note n) {
        print(instrument + ".play() " + n);
    }

    String what() {
        return instrument;
    }

    void adjust() {
        print("Adjusting " + instrument);
    }

    public String toString() {
        return this.what();
    }
}

class Wind extends Instrument {
    private String instrument;

    Wind(String instrument) {
        super(instrument);
        this.instrument = instrument;
    }
}

class Percussion extends Instrument {
    private String instrument;

    Percussion(String instrument) {
        super(instrument);
        this.instrument = instrument;
    }
}

class Stringed extends Instrument {
    private String instrument;

    Stringed(String instrument) {
        super(instrument);
        this.instrument = instrument;
    }

    public void sweep() {
        System.out.println(instrument + "brrrrrrrrrreeeeee");
    }
}

class Music3 {
    public static void tune(Instrument i) {
        // ...
        i.play(Note.MIDDLE_C);
    }

    public static void tuneAll(Instrument[] e) {
        for (Instrument i : e) {
            System.out.println(i.what());
            tune(i);
        }
    }

    private Random rand = new Random(6);

    public Instrument generator() {
        switch (this.rand.nextInt(5)) {
            default:
            case 0:
                return new Wind("Wind");
            case 1:
                return new Percussion("Percussion");
            case 2:
                return new Stringed("Stringed");
        }

    }

    public static void main(String[] args) {
        Music3 music3 = new Music3();
        Instrument[] orchestra = new Instrument[6];
        for (int i = 0; i < 6; i++) {
            orchestra[i] = music3.generator();
        }
        tuneAll(orchestra);
    }
}