Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello! I am new here and I am currently a first year college student. I am going to show you the code first.

public class Author {
    private String name;
    private String email;
    private char gender;
    
    public Author(String name, String email, char gender){
        this.name = name;
        this.email = email;
        this.gender = gender;
    }
    public String toString(){
        return  "Author[name = " + this.name + ", email = " + this.email + ", gender = " + this.gender + "]";
    }
    public String getName(){
        return this.name;
    }
    public void setEmail(String email){
        this.email = email;
    }
    public String getEmail(){
        return this.email;
    }
    public char getGender(){
        return this.gender;
    }
}

public class Book {
    private String name;
    private Author author;
    private double price;
    private int qty;
    
    
    public Book(String name, Author author, double price){
        this.name = name;
        this.author = author;
        this.price = price;
    }
     public Book(String name, Author author, double price, int qty){
       this(name, author, price);
        this.qty = qty;
    }
     public String getName(){
         return this.name;
     }
     public Author getAuthor(){
         return this.author;
     }
     public double getPrice(){
         return this.price;
     }
     public void setPrice(double price){
         this.price = price;
     }
     public int getQty(){
         return this.qty;
     }
     public void setQty(int qty){
         this.qty = qty;
     }
     
     public String toString(){
         
         return "[name = " + this.name + ", " + ???  + ", price = " + this.price + ", qty = " + this.qty;
     }
     
}


So our professor tasked us to reuse the toString() from class Author to the toString() from class Book(in replacement with the 3 question marks I put there as of now)

What I have tried:

I have tried calling the constructor Author(null, null, null) in order to access the toString method from class Author. But, the parameters weren't assigned to the user's desired value until the main method. Here is my main method where we were tasked to check if the methods in class Author were working.

public class TestAuthor {
    public static void main(String[] args){
        Author author = new Author("Janina San Miguel", null, 'F');
        author.setEmail("janinasmigszxc@email.com");
        System.out.println("Name : " + author.getName());
        System.out.println("Email : " + author.getEmail());
        System.out.println("Gender : " + author.getGender());
        System.out.println(author.toString());
    }

Here is where the objects were initialized. Here is the main method where we were supposed to check if the methods in class Book is working.
public class TestBook {
    public static void main(String[] args){
    Book book = new Book("Miss Universe and How to Win It", null, 0, 0);
    book.setPrice(250.75);
    book.setQty(5);
    System.out.println(book.toString());
    
    }
}

What should I do to reuse the toString with the initialized parameters? Thank you for your help mx.
Posted
Comments
Richard MacCutchan 31-Aug-22 9:50am    
Just replace "???" with "this.author.toString()". But you need to pass an Author object to the Book constructor. In fact the constructor should check that it is not null.
Paul Lawrence Perez 31-Aug-22 10:17am    
The author object is already a parameter in the book constructor
Richard MacCutchan 31-Aug-22 10:33am    
Yes, but in the above code you have:
Book book = new Book("Miss Universe and How to Win It", null, 0, 0); // author is null

So if you try to call author.toString in that book you will get a NullReference exception.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900