3

I have two classes. Museum and Painting. The Painting class is working as expected, but I am having issues with the Museum class. We have been asked to create a method signature that will addPainting(String, String) which has two parameters Artist and Location and adds a new Painting to the museum paintings collection.

When I try to compile the code, I get no suitable matching methods found?

Does anyone know what I'm missing here?

public class Museum  {
    //creating the fields
    private ArrayList<Painting> paintings;
    private String name;

    /**
     * Create a Museum Class 
     */
    public Museum(String aMuseum) {
        paintings = new ArrayList<>();
        name = aMuseum;
    }

    /**
    * Add a painting from the Paintings class
    */
    public void addPainting(String artist, String location) {
        paintings.add(artist, location);
    }
}
3
  • 1
    paintings::add takes a single argument of type Painting, not two arguments of type Strings. Commented Nov 7, 2021 at 11:55
  • You are missing many key concepts, instead of fixing this code right away, I would suggest first read about Generics (docs.oracle.com/javase/tutorial/java/generics/index.html) and then read about ArrayList Commented Nov 7, 2021 at 12:19
  • You want to add a new Painting; so, it should be paintings.add(new Painting(artist, location)); Commented Nov 7, 2021 at 12:21

1 Answer 1

2

You should create a new Painting object, and then add it to the paintings list.

instead of

paintings.add(artist, location);

you should do something like:

Painting p = new Painting(artist, location);
paintings.add(p);

Of course, you should also implement the Painting constructor (in the Painting class).

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

2 Comments

Thanks. This allows me to add paintings directly to the Musuem array list. The challenge I am having is when I add a new object to the Paintings class, this should store within the Museum array list. I could be wrong, but I think I need to somehow call on a method from within the Paintings class to retreive this information?
If you want to use this list from out of the Museum class, you should have a "getter" method, in order you can getPaintings() and iterate the list's objects.

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.