0
  class Author
  {
    String name;
    String email;
    char gender;

    Author(String name,String email,char gender)
    {
        this.name = name;
        this.email = email;
        this.gender = gender;
    }
  }

  class Book
  {
    String name;
    Author author;
    double price;
    int qtyInStock;

    Book(String name,Author author,double price,int qtyInStock)
    {
        this.name = name;
        this.author = author;
        this.price = price;
        this.qtyInStock = qtyInStock;
    }

    void setName(String name)
    {
        this.name = name;
    }

    void setAuthor(Author author)
    {
            this.author = author;
    }

    void setPrice(double price)
    {
        this.price = price;
    }

    void setQtyInStock(int qtyInStock)
    {
        this.qtyInStock = qtyInStock;
    }

    String getName()
    {
        return name;
    }

    Author getAuthor()
    {
        return author;
    }

    double getPrice()
    {
        return price;
    }

    int getQtyInStock()
    {
        return qtyInStock;
    }   
  }

  public class HOA1
  {
    public static void main(String[] args)
    {
        Author author = new Author("Javed","[email protected]",'M');
        Book book = new Book("WiproS",author,500.99,3);

        book.setName("JavaWorld");
        System.out.println(book.getName());
        String[] authDetails = book.getAuthor().toString();
        for(String strr: authDetails)
        {
           System.out.println(strr);
        }


    }
  }

In the above code, I have to provide solution for the question stated as

"Create a class Author with the following information.
Member variables : name (String), email (String), and gender (char)
Parameterized Constructor: To initialize the variables
Create a class Book with the following information.
Member variables : name (String), author (of the class Author you have just created), price (double), and qtyInStock (int)
[Assumption: Each book will be written by exactly one Author]
Parameterized Constructor: To initialize the variables
Getters and Setters for all the member variables
In the main method, create a book object and print all details of the book (including the author details) "

I am not able to solve it ! I want to print author details using book object .Please help.

2
  • I don't understand your question. What exactly is the thing you are struggling with? Commented Mar 11, 2020 at 12:08
  • in last lines, I am trying to print the details for author via book object. How to do this ? Commented Mar 11, 2020 at 12:09

2 Answers 2

4

First, you need to override the toString() method in your Author class, to pretty print the fields. An example could be:

class Author {

    // ... your fields ...

    @Override
    public String toString() {
        return "Author{" +
                "name='" + name + '\'' +
                ", email='" + email + '\'' +
                ", gender=" + gender +
                '}';
    }
}

Then, here's the mistake that you are making. The toString() method from the Author class returns a String, not an array of String (String[] as you did). So, you could just do:

String authDetails = book.getAuthor().toString();
System.out.println(authDetails);

But, the recommended way to do this is the following. Just use an object for the Author class, and pass that object to the System.out.println(...) method. This will automatically call the toString() method on the given object.

Author author = book.getAuthor();
System.out.println(author);

Has the same effect as:

System.out.println(author.toString());
Sign up to request clarification or add additional context in comments.

Comments

0

Override the toString() method in your Author class. See below the code

class Author {

// ... your fields ...

@Override
public String toString() {
    return "Author{" +
            "name='" + name + '\'' +
            ", email='" + email + '\'' +
            ", gender=" + gender +
            '}';
}

Once toString method is defined in Author class you can print the Author class all objects data using either toString method or when you print the Author instance as well.

System.out.println("Author data:"+book.getAuthor());

The toString() method returns the string representation of the object. If you print any object, java compiler internally invokes the toString() method on the object. So overriding the toString() method, returns the desired output, it can be the state of an object etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.