isefire

Q4_Midterm_2_CS302_UAB

Nov 13th, 2014
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.68 KB | None | 0 0
  1. package edu.uab.cis.midterm;
  2.  
  3. public class Question4 {
  4.  
  5.     public class Fruit {
  6.    
  7.         public boolean eatable;
  8.        
  9.         @Override
  10.         public boolean equals(Object obj) {
  11.             if (this == obj)
  12.                 return true;
  13.             if (obj == null)
  14.                 return false;
  15.             if (!(obj instanceof Fruit))
  16.                 return false;
  17.             Fruit other;
  18.             try { other = (Fruit) obj; }
  19.             catch (Exception e) { return false; }
  20.             if (!getClass().equals(other.getClass()))
  21.                 return false;
  22.             if (eatable != other.eatable)
  23.                 return false;
  24.             return true;
  25.         }
  26.        
  27.         @Override
  28.         public int hashCode() {
  29.             final int prime = 31;
  30.             int result = 1;
  31.             result = prime * result + getClass().hashCode();
  32.             result = prime * result + (eatable ? 1231 : 1237);
  33.             return result;
  34.         }
  35.  
  36.     }
  37.    
  38.     public class Apple extends Fruit
  39.     {
  40.         public int color;
  41.  
  42.         @Override
  43.         public boolean equals(Object obj) {
  44.             if (this == obj)
  45.                 return true;
  46.             if (obj == null)
  47.                 return false;
  48.             if (!(obj instanceof Apple))
  49.                 return false;
  50.             Apple other;
  51.             try { other = (Apple) obj; }
  52.             catch (Exception e) { return false; }
  53.             if (!getClass().equals(other.getClass()))
  54.                 return false;
  55.             if (eatable != other.eatable)
  56.                 return false;
  57.             if (color != other.color)
  58.                 return false;
  59.             return true;
  60.         }
  61.        
  62.         @Override
  63.         public int hashCode() {
  64.             final int prime = 31;
  65.             int result = super.hashCode();
  66.             result = prime * result + getClass().hashCode();
  67.             result = prime * result + color;
  68.             return result;
  69.         }
  70.  
  71.  
  72.        
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment