JAVA Modify the attached Book class so that it correctly rec

JAVA:

Modify the attached Book class so that it correctly recognizes \"an equal book.\" HashPlay should leave its HashSet empty. Note that deciding \"what makes two books equal?\" is an essential part of this program. Is \"Gulag Archipelago\" the same book as \"The Gulag Archipelago\"? Is \"C S Lewis\" the same as \"Jack Lewis\" or \"Clive Staples Lewis\"? Allow for human frailty in deciding how to determine whether two books are the same, but don\'t try to be a hero by manufacturing fragile comparisons of sliced and diced information.


public class Book {
  
   private String title;
   private String author;
   private String isbn;
  
   public Book(String t, String a, String i) {
       title = t;
       author = a;
       isbn = i;
   }

   @Override
   public int hashCode() {
       final int prime = 31;
       int result = 1;
       result = prime * result + ((author == null) ? 0 : author.hashCode());
       result = prime * result + ((isbn == null) ? 0 : isbn.hashCode());
       result = prime * result + ((title == null) ? 0 : title.hashCode());
       return result;
   }

   @Override
   public boolean equals(Object obj) {
       if (this == obj)
           return true;
       if (obj == null)
           return false;
       if (getClass() != obj.getClass())
           return false;
       Book other = (Book) obj;
       if (author == null) {
           if (other.author != null)
               return false;
       } else if (!author.equals(other.author))
           return false;
       if (isbn == null) {
           if (other.isbn != null)
               return false;
       } else if (!isbn.equals(other.isbn))
           return false;
       if (title == null) {
           if (other.title != null)
               return false;
       } else if (!title.equals(other.title))
           return false;
       return true;
   }

   @Override
   public String toString() {
       return \"Book [title=\" + title + \", author=\" + author + \", isbn=\" + isbn
               + \" hashcode=\" + hashCode() + \"]\";
   }

}

import java.util.HashSet;


public class HashPlay {

   public static void main(String[] args) {
       Book b1 = new Book(\"C. S. Lewis\", \"Out of the Silent Planet\", \"1234\");
       Book b2 = new Book(\"CS Lewis\", \"Out Of The Silent Planet\", \"1234\");
      
       System.out.println(\"b1: \" + b1);
       System.out.println(\"b2: \" + b2);
      
       HashSet<Book> hm = new HashSet<>();
      
       hm.add(b1);
       System.out.println(\"removal of book: \" + hm.remove(b2));
      
       System.out.println(hm);

   }

}

Solution

I cannot tweak the logic a lot so I have made changes that attribute should be changes to lowe case before adding it to hash code and comparing without case in equals method.

public class Book {

  

private String title;

private String author;

private String isbn;

  

public Book(String t, String a, String i) {

title = t;

author = a;

isbn = i;

}

@Override

public int hashCode() {

final int prime = 31;

int result = 1;

result = prime * result + ((author == null) ? 0 : author.toLowerCase().hashCode());

result = prime * result + ((isbn == null) ? 0 : isbn.toLowerCase().hashCode());

result = prime * result + ((title == null) ? 0 : title.toLowerCase().hashCode());

return result;

}

@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

return false;

Book other = (Book) obj;

return (compareString(author, other.author) && compareString(title, other.title) && compareString(isbn, this.isbn));

}

@Override

public String toString() {

return \"Book [title=\" + title + \", author=\" + author + \", isbn=\" + isbn

+ \" hashcode=\" + hashCode() + \"]\";

}

  

private boolean compareString(String a, String b)

{

   if(a == null || b == null)

       return false;

   return a.equalsIgnoreCase(b);

}

}

JAVA: Modify the attached Book class so that it correctly recognizes \
JAVA: Modify the attached Book class so that it correctly recognizes \
JAVA: Modify the attached Book class so that it correctly recognizes \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site