Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Equals/hashCode/compareTo for HashSet and TreeSet elements . Consider the following object classes in Section 1.2: Point2D, page 77, a point is identified by its coordinates

Equals/hashCode/compareTo for HashSet and TreeSet elements. Consider the following object classes in Section 1.2:

  • Point2D, page 77, a point is identified by its coordinates (x, y)

  • Date, page 103, a Date is identified by (month, day, year)

  • String, page 80, a String is identified by its string contents

  • Counter, page 85, a Counter is identified by its name

  1. Examine these object classes for their equals, hashCode, and compareTo, and determine if Point2D can be used for HashSet and separately if it can be used for TreeSet and 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 object as a key (values in Maps need no equals, etc.).

  2. If the class doesn't have both an equals method and a hashCode method, for example Date.java on page 103, 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. ===================================================================================================================================image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

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; } } class public class Counter { private final String name; private int count; name instance variables constructor public Counter(String id) { name = id; } public void increment() { count++; } instance methods public int tally() { return count; } instance variable name public String toString() { return count + + name; } test client create and initialize objects public static void main(String[] args) { Counter heads = new Counter("heads"); Counter tails = new Counter("tails"); invoke heads.increment(); constructor heads.increment(); tails.increment(); automatically invoke toString object Stdout.println(heads + + tails);an name Stdout.println(heads.tally() tails.tally() ); } invoke method } Anatomy of a class that defines a data type public class Point2D Point2D(double x, double y) double x() double y() double r() create a point x coordinate y coordinate radius (polar coordinates) angle (polar coordinates) Euclidean distance from this point to that draw the point on StdDraw double theta () double distanceTo (Point2D that) void draw() public class String String() int length() char charAt(int i) int indexOf(String p) int indexOf(String p, int i) String concat(String t) String substring(int i, int j) String[] split(String delim) int compareTo(String t) boolean equals(String t) int hashCode () create an empty string length of the string ith character first occurrence of p (-1 if none) first occurrence of p after i (-1 if none) this string with t appended substring of this string (ith to j-1st chars) strings between occurrences of delim string comparison is this string's value the same as t's? hash code

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2022 Grenoble France September 19 23 2022 Proceedings Part 4 Lnai 13716

Authors: Massih-Reza Amini ,Stephane Canu ,Asja Fischer ,Tias Guns ,Petra Kralj Novak ,Grigorios Tsoumakas

1st Edition

3031264118, 978-3031264115

More Books

Students also viewed these Databases questions

Question

Explain the six co1nn1on forn1s of union security clause.

Answered: 1 week ago

Question

help asp

Answered: 1 week ago