isefire icon

Q4_Midterm_2_CS302_UAB

isefire | PRO | 11/13/14 07:13:28 AM UTC | 0 ⭐ | 375 👁️ | Never ⏰ | []
Java |

1.68 KB

|

None

|

0 👍

/

0 👎

package edu.uab.cis.midterm;
 
public class Question4 {
 
    public class Fruit {
    
        public boolean eatable;
        
        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (!(obj instanceof Fruit))
                return false;
            Fruit other;
            try { other = (Fruit) obj; }
            catch (Exception e) { return false; }
            if (!getClass().equals(other.getClass()))
                return false;
            if (eatable != other.eatable)
                return false;
            return true;
        }
        
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + getClass().hashCode();
            result = prime * result + (eatable ? 1231 : 1237);
            return result;
        }
 
    }
    
    public class Apple extends Fruit
    {
        public int color;
 
        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (!(obj instanceof Apple))
                return false;
            Apple other;
            try { other = (Apple) obj; }
            catch (Exception e) { return false; }
            if (!getClass().equals(other.getClass()))
                return false;
            if (eatable != other.eatable)
                return false;
            if (color != other.color)
                return false;
            return true;
        }
        
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = super.hashCode();
            result = prime * result + getClass().hashCode();
            result = prime * result + color;
            return result;
        }
 
 
        
    }
}

Comments