-3

Why isn't this working for me?

public class Band {

    public void addMember(Musician musician) {      
    musicians.add(musician);
    System.out.println("Muscian: " + musician + "was successfully added");
    }
}

public static void main(String[] args) {

        Band Beatles = new Band("Beatles");
        Beatles.addMember("John Lennon");
}

public class Musician {


private String name;

    public Musician(String name, Instrument instrument) {
        this.name = name;
        Instrument Guitar = instrument;
    }

    public void play() {

    }
}
Puck
  • 2,080
  • 4
  • 19
  • 30
vanark
  • 1
  • 4
  • How do you know it isn't working? – Tim B Dec 10 '13 at 20:20
  • Beatles is a band. The addMember method accepts a Musician which is a different class. You are passing in a String, which is the Musician's name, but you aren't creating a new Musician object. – Andrew T. Dec 10 '13 at 20:21
  • `Beatles.addMember("John Lennon");` should be `Beatles.addMember(new Musician("John Lennon", "Instrument"));` – Gustavo Dec 10 '13 at 20:21
  • We don't know what the constructor for an instrument takes but Gustavo's line above is as close as we can figure. – Andrew T. Dec 10 '13 at 20:22
  • Band Beatles = new Band("Beatles"); Beatles.addMember(new Musician("John Lennon", "Guitar")); Is that how it should be done then? – vanark Dec 10 '13 at 20:22
  • @user3087630 No it should be Beatles.addMember(new Musician("John Lennon", new Instrument(What Ever the heck this takes)); – Andrew T. Dec 10 '13 at 20:23
  • @Paradopolis you're right – Gustavo Dec 10 '13 at 20:24
  • http://xup.dk/instrument.jpg This is how the instrument class should work. I have made it as a interface.. public interface Instrument { class Guitar { public Guitar() { System.out.println("Dew dow dee"); } public String use() { return null; } } class Drums { public Drums() { System.out.println("Bom bum tji"); } public String use() { return null; } } } – vanark Dec 10 '13 at 20:25
  • @user3087630 That is fine but an interface is inherently abstract. You must have an implementation of the interface, and that implementation must take a constructor. – Andrew T. Dec 10 '13 at 20:27
  • @user3087630 I'm also going to make an assumption you are a bit new to this. I would go check out some java tutorials on interfaces like here: http://docs.oracle.com/javase/tutorial/java/concepts/interface.html . Also, if you are serious about programming, read up a bit on camel case and java style. For instance, variable names should be lower case while class names should be upper case. Good luck! – Andrew T. Dec 10 '13 at 20:30
  • I am very new. This is my school assignment im struggeling with. I am not sure - maybe, Drums and Guitar isnt supposed to be subclasses, but their own classes? Is that the error? - Or does the uml at http://www.xup.dk/instrument.jpg imply that it should be subclasses? – vanark Dec 10 '13 at 20:54
  • @user3087630 could you paste complete code or your assignment details.You miss instrument class.paste your assignment statement. – sar Dec 11 '13 at 07:27

1 Answers1

0
Beatles.addMember("John Lennon");

should be

Beatles.addMember(new Musician("John Lennon", new Instrument(new Guitar()));

I suspect, without seeing your actual Instrument class, based on your comment that this line should work.

The problem is that you are treating some objects as Strings. You need to create the object out of the class that defines it first.

Andrew T.
  • 4,598
  • 4
  • 35
  • 54
  • I doesnt work. `public static void main(String[] args) { Band Beatles = new Band("Beatles"); Beatles.addMember(new Musician("John Lennon", new Instrument(new Guitar()))); Beatles.addSong(new Song("Yesterday", 122, 4)); }` Error: Guitar cannot be resolved to type. Instrument class: `public interface Instrument { class Guitar { public Guitar() { System.out.println("Dew dow dee"); } public String use() { return null; } } class Drums { public Drums() { System.out.println("Bom bum tji"); } public String use() { return null; } } }` – vanark Dec 10 '13 at 20:49
  • And as you see here, I dont think im allowed to make constructors and variables in the guitar class - This is an asignment. xup.dk/instrument.jpg – vanark Dec 10 '13 at 20:52
  • I am not sure - maybe, Drums and Guitar isnt supposed to be subclasses, but their own classes? Is that the error? - Or does the uml at http://www.xup.dk/instrument.jpg imply that it should be subclasses? – vanark Dec 10 '13 at 20:53
  • @user3087630 Guitar and Drums should both implement Instrument. They should not be inner classes of Instrument. You should seriously go and read up on how interfaces work, there are plenty of other SO answers that can show you. – Andrew T. Dec 11 '13 at 04:40