Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment