Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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)

image text in transcribed Date, a Date is identifified by (month, day, year)

image text in transcribed String, a String is identifified by its string contents

image text in transcribed Counter, a Counter is identifified by its name

image text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Larry Ellison Database Genius Of Oracle

Authors: Craig Peters

1st Edition

0766019748, 978-0766019744

More Books

Students also viewed these Databases questions

Question

1. Why do people tell lies on their CVs?

Answered: 1 week ago

Question

2. What is the difference between an embellishment and a lie?

Answered: 1 week ago