T Loch. T Loch. - 7 months ago 15
Java Question

Adding objects to ArrayList

This if my first question on stackoverflow. I can usually find answers myself but I'm having trouble with this one. I've got 2 objects, "Book", and "Periodical". These are subclasses to a class "Publication". Now, I'm trying to add 3 instances of "Book" and 3 instances of "Periodical" to an ArrayList. I'm having trouble figuring out how to do this.

With this current code, I get an error "no suitable method found for add(Book,Book,Book,Periodical,Periodical,Periodical).

Here is the current code:

import java.util.ArrayList;
import java.util.Date;

public class DriverProgram {
public static void main(String[] args) {
// Instantiate 3 instances of each object.
Book book1 = new Book(1234, 1, "James", 100, "Hello", "Berkwood Inc.", new java.util.Date(), "History");
Book book2 = new Book(2345, 2, "Ralph", 200, "Goodbye", "Shackles Co.", new java.util.Date(), "English");
Book book3 = new Book(3456, 3, "Julia", 300, "Hello Again", "Trustin Inc.", new java.util.Date(), "History");
Periodical periodical1 = new Periodical("Daily", "Dylan", "History 101", "History Inc.", new java.util.Date(), "History");
Periodical periodical2 = new Periodical("Weekly", "Jannette", "Mathematics 101", "Mathematics Inc.", new java.util.Date(), "Mathematics");
Periodical periodical3 = new Periodical("Monthly", "Patricia", "Science 101", "Science Inc.", new java.util.Date(), "Science");

// Create an array list of the Publication class type, and add the objects to it.
ArrayList <Publication> publications = new ArrayList<Publication>();
publications.add(book1, book2, book3, periodical1, periodical2, periodical3);

// Pass the array list to a method to loop through it and display the toString methods.
displayObjects(publications);
} // End of main

static void displayObjects (ArrayList<Publication> publications) {
// Loop through array list and display the objects using the toString methods.
for (Publication p : publications) {
System.out.print(p.toString());
} // End of for each loop
} // End of displayObjects
} // End of DriverProgram class


I've also tried changing:

publications.add(book1, book2, book3, periodical1, periodical2, periodical3);


To this:

publications.add(book1);
publications.add(book2);
publications.add(book3);
publications.add(periodical1);
publications.add(periodical2);
publications.add(periodical3);


Which rids my program of the compiler error, but then it just prints the "periodical3" object, 6 times. I'm not sure what I'm doing wrong. Any suggestions? Thank you in advance! :)

EDIT:

Here is my Book class:

public class Book extends Publication{
private static int isbn = 0;
private static int libraryOfCongressNbr = 0;
private static String author = "";
private static int nbrOfPages = 0;

// Constructor for Book class with parameters for each attribute.
public Book(int newISBN, int newLibraryOfCongressNbr, String newAuthor, int newNbrOfPages, String newTitle, String newPublisher, java.util.Date newPublicationDate, String newSubject) {
super(newTitle, newPublisher, newPublicationDate, newSubject);
isbn = newISBN;
libraryOfCongressNbr = newLibraryOfCongressNbr;
author = newAuthor;
nbrOfPages = newNbrOfPages;
}

/////////////////////////////////////////////////////// Getters ///////////////////////////////////////////////////////

int getISBN() {
return isbn;
}

int getLibraryOfCongressNbr() {
return libraryOfCongressNbr;
}

String getAuthor() {
return author;
}

int getNbrOfPages() {
return nbrOfPages;
}

/////////////////////////////////////////////////////// Setters ///////////////////////////////////////////////////////

void setISBN(int newISBN) {
isbn = newISBN;
}

void setLibraryOfCongressNbr(int newLibraryOfCongressNbr) {
libraryOfCongressNbr = newLibraryOfCongressNbr;
}

void setAuthor(String newAuthor) {
author = newAuthor;
}

void setNbrOfPages(int newNbrOfPages) {
nbrOfPages = newNbrOfPages;
}

//toString method for Book class
public String toString () {
StringBuilder result = new StringBuilder();
result.append("\nISBN: " + isbn + "\n");
result.append("\nPublisher: " + libraryOfCongressNbr + "\n");
result.append("\nAuthor: " + author + "\n");
result.append("\nNumber of Pages: " + nbrOfPages + "\n");
result.append("--------------------------------------------------------- ");
return super.toString() + result.toString();
} // End of toString
} // End of Book class


My Periodical class is identical, but here is my Publication class:

import java.util.Date;

public abstract class Publication {

// Data fields.
private static String title = "";
private static String publisher = "";
private static java.util.Date publicationDate;
private static String subject = "";

// Constructor for Publication class with parameters for each attribute.
public Publication(String newTitle, String newPublisher, java.util.Date newPublicationDate, String newSubject){
title = newTitle;
publisher = newPublisher;
publicationDate = newPublicationDate;
subject = newSubject;
}

/////////////////////////////////////////////////////// Getters ///////////////////////////////////////////////////////

String getTitle() {
return title;
}

String getPublisher() {
return publisher;
}

java.util.Date getPublicationDate() {
return publicationDate;
}

String getSubject() {
return subject;
}

/////////////////////////////////////////////////////// Setters ///////////////////////////////////////////////////////

void setTitle(String newTitle) {
title = newTitle;
}

void setPublisher(String newPublisher) {
publisher = newPublisher;
}

void setPublicationDate(java.util.Date newPublicationDate) {
publicationDate = newPublicationDate;
}

void setSubject(String newSubject) {
subject = newSubject;
}

//toString method for Publication class
public String toString () {
StringBuilder result = new StringBuilder();
result.append("\nTitle: " + title + "\n");
result.append("\nPublisher: " + publisher + "\n");
result.append("\nPublication Date: " + publicationDate + "\n");
result.append("\nSubject: " + subject + "\n");
return result.toString();
} // End of toString
} // End of Publication class


Let me know if you need anything else!

EDIT x2: Sorry, I realize my post is getting quite long.

So I've gotten rid of all "static" keywords from my class variables, or "data fields" as I've called them in my code. I then changed my code back to this code:

ArrayList <Publication> publications = new ArrayList<Publication>();
publications.add(book1);
publications.add(book2);
publications.add(book3);
publications.add(periodical1);
publications.add(periodical2);
publications.add(periodical3);


And it works! It executes as it should! I just one question though, since this code doesn't seem to work:

publications.add(book1, book2, book3, periodical1, periodical2, periodical3);


Is there a shorter way to add all of the objects to the ArrayList with out doing it one by one?

Answer

If I understand the problem correctly, you have 6 Publication objects, and you are only seeing the values of the most recently created one.

That would likely be caused because you have static class variables instead of instance variables.

For example

class A {
    static int x; // class variable
    int y;        // instance variable

    public A(int val) {
        x = val; // All 'A' classes now have x = val;
        y = val; // Only 'this' class has y = val;
    }
}

If I were to run this

A a1 = new A(4);
A a2 = new A(5);
System.out.println(a1.x);

Then I would see it print 5 and not 4, which describes the scenario you are seeing because you have assigned all variables in the Publication class to those that you use during the last call of new Periodical.

The solution is to not use static variables if you want to have multiple instances of a class with their own values.

Comments