Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 { /** * Associates the specified value with the specified key in this dual map. * Afterdualmap.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. 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 testdm = new DualHashMap();

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

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> 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

// should print 10 semi-random numbers as Integers for (int i = 0; i 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(); } }

image text in transcribed

Du

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

Database Concepts International Edition

Authors: David M. Kroenke

6th Edition International Edition

0133098222, 978-0133098228

More Books

Students also viewed these Databases questions

Question

What are the components of curriculum?

Answered: 1 week ago

Question

Discuss whistleblower protection under OSHA.

Answered: 1 week ago

Question

Which are non projected Teaching aids in advance learning system?

Answered: 1 week ago