Question: Consider there object classes: Point2D , a point is identifified by its coordinates (x, y) Date , a Date is identifified by (month, day, year)

Consider there object classes:

Point2D, a point is identifified by its coordinates (x, y)

Consider there object classes: Point2D, a point is identifified by its coordinates Date, a Date is identifified by (month, day, year)

(x, y) Date, a Date is identifified by (month, day, year) String, String, a String is identifified by its string contents

a String is identifified by its string contents Counter, a Counter is Counter, a Counter is identifified by its name

identifified by its name Question. (a) Examine these object classes for their

Question.

(a) Examine these object classes for their equals, hashCode, and compareTo, and determine if Point2D can be used for HashSetand separately if it can be used for TreeSetand then if Date can be used for its HashSet or TreeSet, and so on. Briefly explain your reasoning. Note that the same analysis holds for HashMap and TreeMap using the key object class.

(b) If the class doesn't have both an equals method and a hashCode method, write equals and/or hashCode for it based on the identifying fields listed above, so that it ends up with both methods. After this, HashSet should be usable for all four object classes.

public class Point 2D Point 2D(double x, double y) create a point double x x coordinate double yo y coordinate double ro radius (polar coordinates) double theta) angle (polar coordinates) double distanceTo(Point 2D that) Euclidean distance from this point to that void draw draw the point on StdDraw An API for points in the plane public class Date private final int month; private final int day; private final int year; public Date(int m, int d, int y) { month = m; day = d; year = y; } public int month { return month; } public int day { return day; } public int year { return year; } public String toString() { return month() + "/" + day() + "/" + year(); public boolean equals(Object x) } if (this -- X) return true; if (x == null) return false; if (this.getClass() !- x.getClass() return false; Date that - (Date) x; if (this.day != that.day) return false; if (this.month!- that.month) return false; if (this.year !- that year) return false; return true; public class String String create an empty string int length length of the string int charAt(int i) ith character int indexOf(String p) first occurrence of p (-1 if none) int indexOf(String P, int i) first occurrence of p after i (-1 if none) String concat(String t) this string with t appended String substring(int i, int ;) substring of this string (ith to j-1st chars) String[] split(String delim) strings between occurrences of delim int compareTo(String t) string comparison boolean equals(String t) is this string's value the same as t's? int hashCode hash code Java String API (partial list of methods) public class Counter class name instance variables private final String name; private int count: constructor public Counter (String id) { name = id; } public void incremento { count++; } instance methods public int tally { return count; } instance variable name public String toString() { return count + " " + name; } test client public static void main(String[] args) create and initialize objects Counter heads = new Counter("heads"); Counter tails = new Counter("tails"); invoke heads.incremento; Constructor heads.increment; tails.incremento: automatically invoke toString object Stdout.println(heads + " " + tails); name Stdout.println(heads.tally + tails.tally ); invoke method public class Point 2D Point 2D(double x, double y) create a point double x x coordinate double yo y coordinate double ro radius (polar coordinates) double theta) angle (polar coordinates) double distanceTo(Point 2D that) Euclidean distance from this point to that void draw draw the point on StdDraw An API for points in the plane public class Date private final int month; private final int day; private final int year; public Date(int m, int d, int y) { month = m; day = d; year = y; } public int month { return month; } public int day { return day; } public int year { return year; } public String toString() { return month() + "/" + day() + "/" + year(); public boolean equals(Object x) } if (this -- X) return true; if (x == null) return false; if (this.getClass() !- x.getClass() return false; Date that - (Date) x; if (this.day != that.day) return false; if (this.month!- that.month) return false; if (this.year !- that year) return false; return true; public class String String create an empty string int length length of the string int charAt(int i) ith character int indexOf(String p) first occurrence of p (-1 if none) int indexOf(String P, int i) first occurrence of p after i (-1 if none) String concat(String t) this string with t appended String substring(int i, int ;) substring of this string (ith to j-1st chars) String[] split(String delim) strings between occurrences of delim int compareTo(String t) string comparison boolean equals(String t) is this string's value the same as t's? int hashCode hash code Java String API (partial list of methods) public class Counter class name instance variables private final String name; private int count: constructor public Counter (String id) { name = id; } public void incremento { count++; } instance methods public int tally { return count; } instance variable name public String toString() { return count + " " + name; } test client public static void main(String[] args) create and initialize objects Counter heads = new Counter("heads"); Counter tails = new Counter("tails"); invoke heads.incremento; Constructor heads.increment; tails.incremento: automatically invoke toString object Stdout.println(heads + " " + tails); name Stdout.println(heads.tally + tails.tally ); invoke method

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!