/** * A version of the place class from Assignment 1. This is quite abbreviated * as the point here is to show overriding of Equals especially * so it is useful for generics * @author gtowell * Created: Feb 2021 */ @SuppressWarnings("unused") public class Place { // The data stored about a place private String zip, city, state; /** * Create an instance of Place. Yes, the variable names are awful * @param z the zip code * @param c the city name * @param s the state name */ public Place(String z, String c, String s) { zip = z; city = c; state = s; } // get accessor needs no comment public String getZip() { return zip; } // Other stuff would go here to complete the class , but is unneeded for this example /** * Override equals so that if the object being compared is another Place, * then you just compare zip codes. * @param o the object to be compared */ @Override public boolean equals(Object o) { if (o instanceof Place) { return zip.equals(((Place) o).getZip()); } return super.equals(o); // use the equals method from the superclass for things that are not objects // return false; // Could just return false as any object that is not a place // will not be equal to a place. } public static void main(String[] args) { // example of equals in operation Place p1 = new Place("19380", "a", "b"); Place p2 = new Place("19380", "aaaa", "bbbb"); Place p3 = new Place("19385", "a", "b"); Integer ii = 10; System.out.println("p1 p2: " + p1.equals(p2)); System.out.println("p1 p3: " + p1.equals(p3)); System.out.println("p1 ii: " + p1.equals(ii)); System.out.println("p2 p3: " + p2.equals(p3)); } }