X986: Writing a toString Method

For this question you will be writing a toString() method within the following class.

public class Book
{
    private String title;
    private String author;
    private double price;

    public Book(String t, String a, double p)
    {
        this.title = t;
        this.author = a;
        this.price = p;
    }
}

The toString() method should return a string in the following format.

"<title> by <author> costs $<price>"

For example, if we ran:

Book b = new Book("Catch-22", "Joseph Heller", 19.05);
System.out.println(b.toString());

then "Catch-22 by Joseph Heller costs $19.05" would be printed out.

Your Answer:

Reset

Practice a different Java exercise

Feedback

Your feedback will appear here when you check your answer.