fork(1) download
  1. import java.util.*;
  2.  
  3. class Book implements Comparable<Book>, Cloneable {
  4. /* private */ String title;
  5. /* private */ Date publishDate;
  6. /* private */ String comment;
  7.  
  8. public boolean equals(Object obj) {
  9. if(obj == this) return true;
  10. if(obj == null) return false;
  11. if(!(obj instanceof Book)) return false;
  12. Book result = (Book) obj;
  13. if((this.title == null && result.title == null)
  14. && (this.publishDate == null || result.publishDate == null)) return true;
  15. else if((this.title == null || result.title == null)
  16. || (this.publishDate == null || result.publishDate == null)) return false;
  17. else if(!result.title.equals(this.title)) return false;
  18. else if(!result.publishDate.equals(this.publishDate)) return false;
  19. else return true;
  20. }
  21.  
  22. public int hashCode() {
  23. int hash = 1;
  24. hash = hash * 31 + title.hashCode();
  25. hash = hash * 31 + publishDate.hashCode();
  26. return hash;
  27. }
  28.  
  29. public int compareTo(Book obj) {
  30. return this.publishDate.compareTo(obj.publishDate);
  31. }
  32.  
  33. public Book clone() {
  34. Book result = new Book();
  35. result.title = this.title;
  36. result.comment = this.comment;
  37. result.publishDate = (Date) this.publishDate.clone();
  38. return result;
  39. }
  40. }
  41.  
  42. public class Main {
  43. public static void main(String[] args) {
  44. Book book1 = new Book();
  45. book1.title = "本1";
  46. book1.publishDate = new Date(1);
  47. Book book2 = new Book();
  48. book2.title = "本1";
  49. book2.publishDate = new Date(1);
  50. boolean bool1 = book1.equals(book2);
  51. System.out.println(bool1);
  52.  
  53. Book book3 = new Book();
  54. book3.title = "本2";
  55. book3.publishDate = new Date(1);
  56. Book book4 = new Book();
  57. book4.title = "本2";
  58. boolean bool2 = book3.equals(book4);
  59. System.out.println(bool2);
  60.  
  61. Book book5 = new Book();
  62. Book book6 = new Book();
  63. boolean bool3 = book5.equals(book6);
  64. System.out.println(bool3);
  65.  
  66. Book book7 = new Book();
  67. book7.title = "本3";
  68. book7.publishDate = new Date(1);
  69. Book book8 = new Book();
  70. book8.publishDate = new Date(1);
  71. boolean bool4 = book7.equals(book8);
  72. System.out.println(bool4);
  73. }
  74. }
Success #stdin #stdout 0.09s 27632KB
stdin
Standard input is empty
stdout
true
false
true
false