Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA homework help The class below describes a Book. (You wrote a similar class

ID: 3673698 • Letter: J

Question

JAVA homework help

The class below describes a Book. (You wrote a similar class for Homework W3). In the next questions, you will write a class called Dictionary.

For full credit, follow good principles of class design, encapsulation, and inheritance.

I recommend writing the class in your development environment to make sure it compiles and then copying the code into the appropriate question

public class Book {

private String title, author;
private int copyrightYear;

public Book(String title, String author, int copyrightYear) {

this.title = title;
this.author = author;
this.copyrightYear = copyrightYear;

}
public String getTitle() {

return title;

}

public String getAuthor() {

return author;

}
public int getCopyrightYear() {

return copyrightYear;

}
public void setAuthor(String author) {

this.author = author;

}
public void setTitle(String title) {

this.title = title;

}
public void setCopyrightYear(int copyrightYear) {

if(copyrightYear > 0) {

this.copyrightYear = copyrightYear;

}

}
@Override
public String toString() {

return title + " by " + author + " (" + copyrightYear + ")";

}

}

1.

Write the class header and the instance data variables for the Dictionary class.

A dictionary is a book that is further described by the number of words in the dictionary.

2.

Write a constructor for the Dictionary class. The constructor should initialize the instance data variables using parameters. The parameters should specify all information needed to describe a dictionary.

3.

Write accessor and mutator (getter and setter) methods. Include validity checking where appropriate.

4.

Write a toString method to return a text representation of the book. The text representation should include the title, author, year, and number of words, like this:

The Dictionary by Merriam-Webster (2015)

contains 171,500 words

5.

Write the equals method (including the method header) for the Book class (not the dictionary class). Two book objects are logically equivalent if they have the same title, author, and copyright year.

6.

Write code that would go inside of a main method. The code should create one book object and one dictionary object and invoke at least two methods on each object. Also include a statement to print a text representation of the book and the dictionary to the console.

Thank you!

Explanation / Answer

public class Book {
   private String title, author;
   private int copyrightYear;
   public Book(String title, String author, int copyrightYear) {
       this.title = title;
       this.author = author;
       this.copyrightYear = copyrightYear;
   }
   public String getTitle() {
       return title;
   }
   public String getAuthor() {
       return author;
   }
   public int getCopyrightYear() {
       return copyrightYear;
   }
   public void setAuthor(String author) {
       this.author = author;
   }
   public void setTitle(String title) {
       this.title = title;
   }
   public void setCopyrightYear(int copyrightYear) {
       if(copyrightYear > 0) {
           this.copyrightYear = copyrightYear;
       }
   }
   @Override
   public String toString() {
       return title + " by " + author + " (" + copyrightYear + ")";
   }
   // equal method
   @Override
   public boolean equals(Object obj) {
       if(obj instanceof Book){
           Book b = (Book)obj;
           return this.title.equalsIgnoreCase(b.title) &&
                   this.copyrightYear == b.copyrightYear &&
                   this.author.equalsIgnoreCase(b.author);
       }
       return false;
   }
}
// Dictionary Class
class Dictionary extends Book{
  
   private int words;
   public Dictionary(String title, String author, int copyrightYear, int words) {
       super(title, author, copyrightYear); // passing to parent
       this.words = words;
   }
   public int getWords() {
       return words;
   }
   public void setWords(int words) {
       if(words < 0) // should be >=0
           this.words = 0;
       this.words = words;
   }
   // toString method
   @Override
   public String toString() {
       return "The "+getTitle()+" by "+getAuthor()+" ("+getCopyrightYear()+")contains "+
               getWords()+" words";
   }
  
   public static void main(String[] args) {
       Book b = new Book("Book", "Coreman", 2016);
       Book d = new Dictionary("Dictionary", "Alex Bob", 2015, 1234);
      
       System.out.println(b);
       System.out.println(d);
      
       System.out.println("Is b an d equal ? "+b.equals(d));
   }
  
}

/*

Output:

Book by Coreman (2016)
The Dictionary by Alex Bob (2015)contains 1234 words
Is b an d equal ? false

*/