Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

0. Introduction. This laboratory assignment involves implementing a data structure called a map. A map acts something like a Python dictionary, because it associates keys

0. Introduction.

This laboratory assignment involves implementing a data structure called a map. A map acts something like a Python dictionary, because it associates keys with their corresponding values. However, it is implemented as a Java class, and it uses Java arrays internally.

1. Theory.

A map is a set of key-value pairs. Each key is said to be associated with its corresponding value, so there is at most one pair in the set with a given key. You can perform the following operations on maps.

You can test if a key has a value in the map.

You can add a new key-value pair to the map.

You can get the value that is associated with a given key.

You can change the value that is associated with a given key.

For example, the keys might be Strings that are the English words for numbers. The values might be Integers that are the numbers corresponding to those words. If you give the map an English word, then you can get back its corresponding number. The maps for this assignment use arrays, and they work by doing linear search on those arrays. As a result, if a map has n pairs, then its operations may need O(n) key comparisons. However, there are better ways to implement maps, using data structures that we have not yet discussed in this course. These require only O(log n) or even O(1) key comparisons.

2. Implementation.

You must write a Java class called Map that implements a map. To simplify grading, your class must use the same names for things that are given here. Your class Map must have two generic class parameters, Key and Value, so it looks like this. Here Key is the type of the maps keys, and Value is the type of the maps values.

class Map { }

Within the class Map, you must have two private arrays called keys and values. The array keys must be an array whose base type is the class parameter Key. The array values must be an array whose base type is the class parameter Value. Suppose that a key k is found at the index i in the arraykeys. Then the value associated with k is found at the same index i in the array values. Do not try to use only one array, because that will not work. You must also have a private integer variable called count that records how many elements of the arrays are being used. You may also need other private variables that are not mentioned here. Your class must have the following methods. Most of them use count, keys, and values somehow. Some methods are public and others are private. The private methods are helpers for the public methods; they may make your code easier to write. Also, both keys and values may be null. This may affect how you test if keys are equal.

public Map(int length)

Constructor. If length is less than 0 then you must throw an IllegalArgumentException. Otherwise, initialize an empty Map whose keys and values arrays have length elements. (Recall that you must make arrays of Objects, then cast them to the appropriate types.)

public Value get(Key key)

Search the array keys for an element that is equal to key. If that element is at some index in keys, then return the element at the same index in the array values. If there is no element equal to key in keys, then throw an IllegalArgumentException.

private boolean isEqual(Key leftKey, Key rightKey)

Test if leftKey is equal to rightKey. Either or both may be null. This method is necessary because you must use == when leftKeyor rightKey are null, but you must use the equals method when both are not null. (Recall that null has no methods.)

public boolean isIn(Key key)

Test if there is an element in keys that is equal to key.

public void put(Key key, Value value)

Search the array keys for an element that is equal to key. If that element is at some index in keys, then change the element at the same index in values to value. If there is no element in keys that is equal to key, then add key to keys, and add value at the same index in values. If keys and values are full, so you cannot add key and value, then throw an IllegalStateException.

private int where(Key key)

Search the array keys for an element that is equal to key. Return the index of that element. If there is no element equal to key in keys, then return 1.

The file tests.java on Moodle contains Java code that performs a series of tests. Each test calls a public method from your class Map, and prints what the method returns. Each test is also followed by a comment that tells how many points it is worth, and what it must print if it works correctly. You may want to run additional tests, but you dont get points for those.

// // // The TRY-CATCH statements catch exceptions thrown by MAP's methods, so that // the program can continue to run even if a method fails. We haven't talked // about TRY-CATCH'es in lecture yet. // // Each test has a comment that shows what it should print, and how many // points it is worth, for a total of 40 points. // HOGWARTS. The Hogwarts dating service. class Hogwarts { // MAIN. Make an instance of MAP and test it. public static void main(String [] args) { Map map; try { map = new Map(-5); } catch (IllegalArgumentException ignore) { System.out.println("No negatives"); // No negatives 2 points. } map = new Map(5); map.put("Harry", "Ginny"); map.put("Ron", "Lavender"); map.put("Voldemort", null); map.put(null, "Wormtail"); System.out.println(map.isIn("Harry")); // true 2 points. System.out.println(map.isIn("Ginny")); // false 2 points. System.out.println(map.isIn("Ron")); // true 2 points. System.out.println(map.isIn("Voldemort")); // true 2 points. System.out.println(map.isIn(null)); // true 2 points. System.out.println(map.isIn("Joanne")); // false 2 points. System.out.println(map.get("Harry")); // Ginny 2 points. System.out.println(map.get("Ron")); // Lavender 2 points. System.out.println(map.get("Voldemort")); // null 2 points. System.out.println(map.get(null)); // Wormtail 2 points. try { System.out.println(map.get("Joanne")); } catch (IllegalArgumentException ignore) { System.out.println("No Joanne"); // No Joanne 2 points. } map.put("Ron", "Hermione"); map.put("Albus", "Gellert"); map.put(null, null); System.out.println(map.isIn(null)); // true 2 points. System.out.println(map.isIn("Albus")); // true 2 points. System.out.println(map.get("Albus")); // Gellert 2 points. System.out.println(map.get("Harry")); // Ginny 2 points. System.out.println(map.get("Ron")); // Hermione 2 points. System.out.println(map.get("Voldemort")); // null 2 points. System.out.println(map.get(null)); // null 2 points. try { map.put("Draco", "Pansy"); } catch (IllegalStateException minnesota) { System.out.println("No Draco"); // No Draco 2 points. } } } 

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

Main Memory Database Systems

Authors: Frans Faerber, Alfons Kemper, Per-Åke Alfons

1st Edition

1680833243, 978-1680833249

More Books

Students also viewed these Databases questions

Question

The models used to analyse different national cultures.

Answered: 1 week ago

Question

The nature of the issues associated with expatriate employment.

Answered: 1 week ago