Search code examples
javaclassarraylistaddition

Adding to an ArrayList from another class


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);
    }
}

Solution

  • 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).