Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Programming Problem 18.12 hashCode and equals methods for the Pair class with LabRat Provide suitable hashCode and equals methods for the Pair class of Section

Programming Problem 18.12 hashCode and equals methods for the Pair class with LabRat

Provide suitable hashCode and equals methods for the Pair class of Section 17.2 and implement a HashMap class, using a HashSet>.

Complete the following classes in your solution:

public class Pair { private T first; private S second; public Pair(T firstElement, S secondElement) { first = firstElement; second = secondElement; } public T getFirst() { return first; } public S getSecond() { return second; } public boolean equals(Object otherObject) { . . . } public int hashCode() { . . . } } 

import java.util.HashSet; import java.util.Collection; import java.util.Set; public class HashMap { private HashSet> set; public HashMap() { . . . } public void put(K key, V value) { . . . } public V get(K key) { . . . } public V remove(K key) { . . . } public Set keySet() { . . . } public int size() { return set.size(); } }

Use the following class as your tester class:

import java.awt.Color; import java.util.Map; import java.util.Set; /** This program tests a map that maps names to colors. */ public class MapTester { public static void main(String[] args) { HashMap favoriteColors = new HashMap(); favoriteColors.put("Juliet", Color.PINK); favoriteColors.put("Romeo", Color.GREEN); favoriteColors.put("Adam", Color.BLUE); favoriteColors.put("Eve", Color.PINK); favoriteColors.put("Romeo", Color.WHITE); favoriteColors.remove("Juliet"); System.out.println(favoriteColors.get("Romeo")); System.out.println("Expected: java.awt.Color[r=255,g=255,b=255]"); System.out.println(favoriteColors.get("Juliet")); System.out.println("Expected: null"); System.out.println(favoriteColors.size()); System.out.println("Expected: 3"); Set keys = favoriteColors.keySet(); System.out.println(keys.size()); System.out.println("Expected: 3"); } } 

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

Step: 3

blur-text-image

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

Data Access Patterns Database Interactions In Object Oriented Applications

Authors: Clifton Nock

1st Edition

0321555627, 978-0321555625

More Books

Students also viewed these Databases questions