John Shanks John Shanks -4 years ago 100
Java Question

How to return toString method from methods stored in an ArrayList

I'm new to java and object oriented programming, but eager to get better at java and involved in the stackoverflow community.

I suspect there is an easy solution to this problem, but I can't find the answer anywhere.
I have a bunch of book details stored in an arraylist, each with a different level of detail (i.e. an extra string "subject" for engineering book, while only 3 variables for a standard book).
I am wondering if it is possible to call the toString methods from each book class from the values stored in the ArrayList?
OR, is it possible to store the toString outputs from each of the different book class methods IN the ArrayList?

This way if there is an engineering textbook it will show all details from the array list, whereas a programming textbook will show less detail (without leaving 'null' if a string field is empty.

i.e. the arraylist will print:

Textbook1: Java101/Dr Migoreng/1331/Java
Textbook2: Python201/Dr Karl/112/Python
Textbook3: Fluids101/Dr Brisbane/122/GIS/Modelling (extra field there)

etc...

If anyone can offer even a keyword on what might help It would be really appreciated.

Thanks

Here is the code:

The TestTextBooks class which builds the ArrayList:






import java.util.ArrayList;

public class TestTextbooks {

public static void main(String[] args) {
Textbook java101 = new ProgrammingTextbook("Java101","Dr Migoreng",1331,"Java");
Textbook python201 = new ProgrammingTextbook("Python201","Dr Karl",112,"Python");
Textbook fluidMechanics = new EngineeringTextbook("Fluids101","Dr Brisbane",122,"GIS","Fluid Mechanics");
Textbook modelling101 = new EngineeringTextbook("Modelling101","Dr Suzuki",322,"WaterGems","Water Modelling");

ArrayList books = new ArrayList();
books.add(java101);
books.add(python201);
books.add(fluidMechanics);
books.add(modelling101);

System.out.println("" + books.toString());
}
}





The Programming Textbook class (adds the field "language" if available)



public class ProgrammingTextbook extends Textbook {
private String language;
private String title;
private String author;
private int pages;


public ProgrammingTextbook (){
}

//overloaded constructor
public ProgrammingTextbook (String title, String author,int pages,String language){
this.title = title;
this.author = author;
this.pages = pages;
this.language=language;
}
//set method for overloaded constructor
public void setProgrammingTextBook (String title, String author,int pages,String language){
setLanguage(language);
}
//individual set methods
public void setLanguage(String language){
this.language = language;
}
//individual get methods
public String getLanguage(){
return language;
}
public String toString(String title, String author,int pages,String language){
return title + "/" + author + "/" + pages + "/" + language;
}

}





The Engineering Textbook class (adds the field language and subject)



public class EngineeringTextbook extends Textbook {
private String language;
private String subject;
//default constructor (probs shouldn't actually have values)
public EngineeringTextbook (){
}
//overloaded constructor
public EngineeringTextbook (String title, String author,int pages,String language,String subject){
this.title = title;
this.author = author;
this.pages = pages;
this.language=language;
this.subject=subject;
}
//set method for overloaded constructor
public void setEngineeringTextBook (String title, String author,int pages){
setLanguage(language);
setSubject(subject);
}
//individual set methods
public void setLanguage(String language){
this.language = language;
}
public void setSubject(String subject){
this.subject = subject;
}

//inbdividual get methods
public String getLanguage(){
return language;
}
public String getSubject(){
return subject;
}

//toString method
public String toString(String title, String author,int pages,String language,String subject){
return title + "/" + author + "/" + pages + "/" + language + "/" + subject;
}
}





The Abstract Textbook Class (defines author,title and pages)(shared by all books)



public abstract class Textbook {

protected String title;
protected String author;
protected int pages;

//default constructor (Don't really need to add null for all values but put in to show it is a default constructor
public Textbook (){
this.title = null;
this.author = null;
this.pages = 0;
}
//overloaded constructor
public Textbook (String title, String author,int pages){
this.title = title;
this.author = author;
this.pages = pages;
}
//individual set methods
public void setTitle(String title){
this.title = title;
}
public void setAuthor(String author){
this.author = author;
}
public void setPages(int pages){
this.pages = pages;
}
//individual get methods
public String getTitle(){
return title;
}
public String getAuthor(){
return author;
}
public int getPages(){
return pages;
}
//abstract toString method
public String toString(String title, String author,int pages){
return title + "/" + author + "/" + pages;
}
}

Answer Source

Object.toString() doesn't take any parameters. Use the @Override annotation to catch it when you have failed to correctly override a method.

@Override
public String toString() {
    return title + "/" + author + "/" + pages + "/" + language;
}

and

@Override
public String toString() {
   return title + "/" + author + "/" + pages;
}   

etc.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download