Question
DualMap.java /** * A DualMap maintains a one-to-one mapping between keys and value. A DualMap * object should contain a Map from keys to values
DualMap.java
/** * A DualMap maintains a one-to-one mapping between keys and value. ADualMap * object should contain aMap from keys to values and a Map from values to * keys. A DualMap cannot contain duplicate keys or duplicate values. * * @author Tom Bylander */ public interface DualMap * If the dual map previously contained a mapping for the key or the value, * the old association(s) should be removed. * * @param key * key to be associated with the specified value. * @param value * value to be associated with the specified key */ public void put(K key, V value);
/** * Removes the mapping from the key to the value from this dual map if it is * present. Afterdualmap.put(key, value) and dualmap.remove(key, value), * then dualmap.get(key) should return null and dualmap.reverseGet(value) * should return null. * * @param key * key whose mapping to value is to be removed * @param value * value whose mapping from key is to be removed */ public void remove(K key, V value);
/** * Returns the value thatmaps from the specified key, or null if this dual * map contains no mapping for the key. * * @param key * the key that maps to the value that should be returned * @return the value thatmaps from the specified key, or null if this dual * map contains no mapping for the key */ public V get(K key);
/** * Returns the key that maps to the specified value, or null if this dual * map contains no mapping for the value. * * @param value * the value that maps from the key that should be returned * @return the key that maps to the specified value, or null if this dual * map contains no mapping for the value */ public K reverseGet(V value);
}
DualMapTest.java
import java.io.*; import java.util.*;
/** * Test the implementation of DualMap * * @author Tom Bylander */ public class DualMapTest { public static void main(String[] args) { monthsTest(); daysTest(); numbersTest(); }
public static void monthsTest() { String[] months1 = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; String[] months2 = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
DualMap
for (int i = 0; i
// This should print: // Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec for (String s : months1) { System.out.print(testdm.get(s) + " "); } System.out.println();
// This should print: // January February March April May June July August September October // November December for (String s : months2) { System.out.print(testdm.reverseGet(s) + " "); } System.out.println();
for (int i = 0; i
// This should print: // null Feb null Apr null Jun null Aug null Oct null Dec for (String s : months1) { System.out.print(testdm.get(s) + " "); } System.out.println();
// This should print: // null February null April null June null August null October null // December for (String s : months2) { System.out.print(testdm.reverseGet(s) + " "); } System.out.println(); }
public static void daysTest() { String[] days1 = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; String[] days2 = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; String[] days3 = { "U", "M", "T", "W", "R", "F", "S" };
DualMap
for (int i = 0; i
// This should print: // U M T W R F S for (String s : days2) { System.out.print(testdm.get(s) + " "); } System.out.println();
// This should print: // Sunday Monday Tuesday Wednesday Thursday Friday Saturday for (String s : days2) { System.out.print(testdm.reverseGet(s) + " "); } System.out.println();
for (int i = 0; i
// This should print: // null null null null null null null for (String s : days2) { System.out.print(testdm.get(s) + " "); } System.out.println();
// This should print: // null null null null null null null for (String s : days2) { System.out.print(testdm.reverseGet(s) + " "); } System.out.println();
// This should print: // U M T W R F S for (String s : days1) { System.out.print(testdm.get(s) + " "); } System.out.println();
// This should print: // Sunday Monday Tuesday Wednesday Thursday Friday Saturday for (String s : days3) { System.out.print(testdm.reverseGet(s) + " "); } System.out.println();
}
public static void numbersTest() { Scanner scan = null; try { scan = new Scanner(new File("numbers.txt")); } catch (FileNotFoundException e) { throw new RuntimeException("unable to open numbers.txt"); }
DualMap
while (scan.hasNextLine()) { int n = scan.nextInt(); String s = scan.nextLine(); Scanner scanline = new Scanner(s); List
scan.close();
Random random = new Random(); // should print 10 random numbers as Lists of Strings for (int i = 0; i
// should print 10 semi-random numbers as Integers for (int i = 0; i part1 = testdm.get(1 + random.nextInt(31)); List
Du
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started