Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
    }
}
like image 602
user17349337 Avatar asked Jan 22 '26 16:01

user17349337


1 Answers

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

like image 176
javadev Avatar answered Jan 24 '26 07:01

javadev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!