0

My goal is to print out that all the instruments are playing by creating the testInstrument.java file. For some reason I am getting an error for System.out.println(all[i].play());

testInstrument.java

package my_instruments;

public class testInstrument {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Guitar g = new Guitar();
        Flute f = new Flute();
        Piano p = new Piano();



        Instrument[] all = new Instrument[3];

        all[0] = g;
        all[1] = f;
        all[2] = p;

        for (int i=0; i<3; i++) {
            System.out.println(all[i].play());
        }


    }

}

Instrument.java

package my_instruments;

public class Instrument {

public Instrument() {

}

public void play() {
    System.out.println("Playing instrument");
}
}

Piano.java

package my_instruments;

public class Piano extends Instrument{
public Piano() {
    super();
}

public void play() {
    System.out.println("Playing piano");
}
}
8
  • 1
    What's the error? Commented Feb 19, 2018 at 17:22
  • Does play() method exist in Instrument class? Does it return anything? Please post the contents of those classes too. Commented Feb 19, 2018 at 17:24
  • Yes the .play() method exists in the Instrument class as well as the Piano, Flute, and Guitar class. Commented Feb 19, 2018 at 17:27
  • What is the relationship between an Instrument and Guitar, Piano and Flute? It may seem obvious and apparent to you but without actual source code to go off of, we're left in the dark here. Commented Feb 19, 2018 at 17:27
  • The error i get is "The method println(boolean) in the type PrintStream is not applicable for the arguments (void)" Commented Feb 19, 2018 at 17:27

2 Answers 2

2

Try this:

for (int i=0; i<3; i++) {
       all[i].play();
    }

The play method is already doing the printing and is not returning anything to print.

Sign up to request clarification or add additional context in comments.

Comments

1

your play method() is doing the printing already System.out.println(); try removing the print statement from your for loop

for (int i=0; i<3; i++) {
    all[i].play();
}

or

for (Instrument i : all) {
    i.play();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.