Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this Assignment, you practice creating your own hashtable based on sample cod

ID: 670629 • Letter: I

Question

In this Assignment, you practice creating your own hashtable based on sample code. Be sure to refer to the Learning Resources as you create your hashtable.

For this Assignment, submit the following program:

Observe the following code, which creates a simple hashtable.

// Demonstrate a Hashtable
import java.util.*;
class HTDemo
{
public static void main(String args[])
{
Hashtable<String,Integer> numbers = new
Hashtable<String,Integer>();

numbers.put("one", new Integer(1));
numbers.put("two", new Integer(2));

  }
}

Starting with this provided code, add the following functionality:

Add the numbers 3 through 10 to the hashtable continuing in the same manner as shown.

Prompt the user for a string, and display the corresponding number. For example, if the user types “five”, the program would output “5”. This must be done using the hashtable as created in the previous step.

Using a loop and a single println statement, display all of the values (both strings and integers) in a table.

Explanation / Answer

import java.util.*;

public class HashTableDemo {

public static void main(String args[]) {
// Create a hash map
Hashtable balance = new Hashtable();
Enumeration names;
String str;
double bal;

balance.put("aaa", new Double(3434.34));
balance.put("bbb", new Double(123.22));
balance.put("ccc", new Double(1378.00));
balance.put("ddd", new Double(99.22));
balance.put("eee", new Double(-19.08));

// Show all balances in hash table.
names = balance.keys();
while(names.hasMoreElements()) {
str = (String) names.nextElement();
System.out.println(str + ": " +
balance.get(str));
}
System.out.println();
// Deposit 1,000 into aaa's account
bal = ((Double)balance.get("aaa")).doubleValue();
balance.put("aaa", new Double(bal+1000));
System.out.println("aaa's new balance: " +
balance.get("aaa"));
}
}