Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Lab 9 In lab 9, you will use Java generics to implement an interface for maintaining a one-to-one mapping. The name of your Eclipse project

Lab 9

In lab 9, you will use Java generics to implement an interface for maintaining a one-to-one mapping.

The name of your Eclipse project should be abc123-lab9, where you replace abc123 with your abc123 id. The name of the file that contains the main method should remain DualMapTest.java. The other files should also keep their original names. The file numbers.txt should be copied to the project directory (should not be in src or bin). To submit the project, export the project and upload the zip file to Blackboard.

Task

Your task is to write the DualHashMap class so it works in combination with the code in lab9.zip. DualHashMap should implement the DualMap interface so the main method in DualMapTest runs correctly.

The idea of the DualMap interface is to maintain a one-to-one mapping between two sets (the "keys" and the "values"). This is similar to the Map interface.

In the Map interface, each key is associated with one value, but duplicate values are allowed. For example, the keys might be id numbers and the values might be names. Two id numbers might map to the same name because two people might have the same name.

Map map = new HashMap(); map.put(123456. "Queen Elizabeth"); map.put(654321. "Queen Elizabeth"); // prints Queen Elizabeth twice System.out.println(map.get(123456)); System.out.println(map.get(654321));

In a DualMap , duplicate values are not allowed (so not a good choice for id numbers to names). A DualMap also has a reverseGet method that returns the key for a specified value.

DualMap dualmap = new DualHashMap(); dualmap.put(13, "thirteen"); dualmap.put(42, "forty-two"); // prints thirteen and 42 System.out.println(dualmap.get(13)); System.out.println(dualmap.reverseGet("forty-two"));

As with Map s, DualMap s have two generic type parameters (see the Pair2 class from the notes for an example). Your DualHashMap.java does not need to know the types for keys and values, but to work properly, both types should have an equals method and a hashcode method. You do not need to implement equals or hashcode as the types used in DualMapTest.java have these methods.

To implement DualHashMap , there should be two HashMap instance variables for the two mappings to be maintained: one from keys to values and the other from values back to keys. The constructor for DualHashMap needs to create these HashMap s. DualMap.java provides information on how the methods of a DualMap are supposed to operate. When you run DualMapTest you should get output like:

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec January February March April May June July August September October November December null Feb null Apr null Jun null Aug null Oct null Dec null February null April null June null August null October null December U M T W R F S Sunday Monday Tuesday Wednesday Thursday Friday Saturday null null null null null null null null null null null null null null U M T W R F S Sunday Monday Tuesday Wednesday Thursday Friday Saturday [nine, thousand, eight, hundred] [thirty-one, thousand, sixty] [twenty-seven, thousand, two, hundred, ninety-nine] [seventeen, thousand, four, hundred, eleven] [twelve, thousand, sixty-five] [ten, thousand, nine, hundred, ninety-six] [eight, thousand, nine, hundred, twenty-four] [three, thousand, three, hundred, eighty-three] [twenty-six, thousand, five, hundred, eighty-seven] [thirteen, thousand, three, hundred, seventy-two] 19986 3931 9843 18911 11453 16495 23820 14675 13274 18724

The first 10 lines of your output should be identical. The last 11 lines will very likely be different because the numbers are randomize

lab9.java:

DualMapTest:

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 testdm = new DualHashMap();

for (int i = 0; i < months1.length; i++) { testdm.put(months1[i], months2[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 < months1.length; i += 2) { testdm.remove(months1[i], months2[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 testdm = new DualHashMap();

for (int i = 0; i < days1.length; i++) { testdm.put(days1[i], days2[i]); testdm.put(days2[i], days3[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 < days1.length; i++) { testdm.put(days1[i], days3[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> testdm = new DualHashMap>();

while (scan.hasNextLine()) { int n = scan.nextInt(); String s = scan.nextLine(); Scanner scanline = new Scanner(s); List list = new ArrayList(); while (scanline.hasNext()) { list.add(scanline.next()); } scanline.close(); testdm.put(n, list); }

scan.close();

Random random = new Random(); // should print 10 random numbers as Lists of Strings for (int i = 0; i < 10; i++) { System.out.println(testdm.get(random.nextInt(32769))); }

// should print 10 semi-random numbers as Integers for (int i = 0; i < 10; i++) { List part1 = testdm.get(1 + random.nextInt(31)); List part2 = testdm.get(1 + random.nextInt(999)); // Need to create a new list to avoid damaging the Lists // in the DualMap. List list = new ArrayList(); for (String s : part1) list.add(s); list.add("thousand"); for (String s : part2) list.add(s); System.out.print(testdm.reverseGet(list) + " "); } System.out.println(); } }

DualMap

** * A DualMap maintains a one-to-one mapping between keys and value. A DualMap * object should contain a Map 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 { /** * Associates the specified value with the specified key in this dual map. * After dualmap.put(key, value) is performed, then dualmap.get(key) should * return value and dualmap.reverseGet(value) should return key. *

* 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. After dualmap.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 that maps 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 that maps 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);

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

Transactions On Large Scale Data And Knowledge Centered Systems Vi Special Issue On Database And Expert Systems Applications Lncs 7600

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Stephen W. Liddle ,Klaus-Dieter Schewe ,Xiaofang Zhou

2012th Edition

3642341780, 978-3642341786

More Books

Students also viewed these Databases questions