-1

With 5 fields title - String, the title of the book. Author - String, the author of the book. Pages - integer, the number of pages in the book.
Publisher - String, the publisher of the book. Year - integer, the year the book was published.

With 6 methods
A constructor that accepts as arguments the values for the fields above, in the order listed. A copy constructor that makes a copy of a Book object. A getTitle method that returns the value of the title field. A setAuthor method that accepts a String argument, which is used to set the author field. An equals method that returns a boolean indicating if two objects contain the same information. If they do, it returns true, false otherwise. The objects contain the same information when their fields contain the same values. A toString method that returns a String that contains on separate lines (5 lines), the names and values for each field. Demonstrate the class in a program. Demo program is required to: Create at least two Book objects. Create a copy of a Book object. Use each of the other four methods to show they work properly. Do not explicitly call the toString method in your demo program.

Sammy
  • 1
  • 1

1 Answers1

1

Here is code for class Book. In toString method there is String.format method which makes readable string. Nothing more special. When equals method is overridden then hashCode method should be too. They both compare objects if they are equals. You can find it more for example from here.

public class Book {

    private String title;
    private String author;
    private int pages;
    private String publisher;
    private int year;

    public Book(String title, String author, int pages, String publisher, int year) {
        this.title = title;
        this.author = author;
        this.pages = pages;
        this.publisher = publisher;
        this.year = year;
    }

    public Book(Book book) {
        this.title = book.title;
        this.author = book.author;
        this.pages = book.pages;
        this.publisher = book.publisher;
        this.year = book.year;
    }

    public String getTitle() {
        return title;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof Book)) {
            return false;
        }
        Book other = (Book) o;
        
        if (this.title.equals(other.title) &&
                this.author.equals(other.author) &&
                this.publisher.equals(other.publisher) &&
                this.pages == other.pages && this.year == other.year) {
            return true;
        }
        return false;
    }

    @Override
    public String toString() {
        return String.format("%-10s %s\n", "Title:", title) +
            String.format("%-10s %s\n", "Author:", author) +
            String.format("%-10s %d\n", "Pages:", pages) +
            String.format("%-10s %s\n", "Publisher:", publisher) +
            String.format("%-10s %d", "Year:", year);
    }

}

Here is main class which uses all of the methods.

public class Main {

    public static void main(String[] args) {
        Book first = new Book("The Book", "Famous Writer", 199, "Big Book Publisher", 1984);
        Book second = new Book("The Second Book", "Less F. Writer", 249, "The Book Publishers", 1999);

        System.out.println("First book");
        System.out.println(first + "\n");
        System.out.println("Second book");
        System.out.println(second + "\n");

        System.out.println("Title of the first book: " + first.getTitle() + "\n");


        Book firstCopy = new Book(first);
        System.out.println("Copy of the first book:");
        System.out.println(firstCopy + "\n");

        System.out.println("Copy equals first book: " + firstCopy.equals(first));
        System.out.println("Second book equals first book: " + second.equals(first));
        System.out.println();

        System.out.println("Change writer of the copy to :'Most F. Writer'");
        firstCopy.setAuthor("Most F. Writer");
        System.out.println(firstCopy + "\n");
        System.out.println("Copy equals first book: " + firstCopy.equals(first));

    }

}

When executed main class outputs this:

First book
Title:     The Book
Author:    Famous Writer
Pages:     199
Publisher: Big Book Publisher
Year:      1984

Second book
Title:     The Second Book
Author:    Less F. Writer
Pages:     249
Publisher: The Book Publishers
Year:      1999

Title of the first book: The Book

Copy of the first book:
Title:     The Book
Author:    Famous Writer
Pages:     199
Publisher: Big Book Publisher
Year:      1984

Copy equals first book: true
Second book equals first book: false

Change writer of the copy to :'Most F. Writer'
Title:     The Book
Author:    Most F. Writer
Pages:     199
Publisher: Big Book Publisher
Year:      1984

Copy equals first book: false
elehtine
  • 106
  • 5