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

Objective: Java Program It is always a good idea to organize your fears in a dat

ID: 3691810 • Letter: O

Question

Objective: Java Program

It is always a good idea to organize your fears in a data structure.  Create a hash table of phobias where each entry the key is the name of the phobia, and the data is the explanation.

Create a class Phobia

Instance variables

Name

Description

Methods

toString: overrides the base toString and returns the name plus the description

equals: returns true only if both the name and the description match

Create a hash table PhobiaHashTable where each of the buckets corresponds to letters in the English alphabet.

Each index corresponds to a letter of the alphabet, and a hash method should be implement where a phobia given will return the index

Every time there is a phobia that starts with the same letter simply add it to the end of the bucket

It is a good idea to make an array of ArrayLists

Methods

add: Adds a new phobia to the hash table

remove: if the phobia’s name and description matches then remove it.  Get over that fear.

lookup: given the phobia’s name return the description

printHashTable: prints out all entry in the hash table

Create a front end where users can add, remove, look up, and print out their fears.

Example Dialog:

Welcome to the fear hash table!

Enter:

1.    To add a fear

2.    To remove a fear

3.    To look up a fear

4.    Print all fears

5.    Quit

1

What is the fear’s name

Ancraophobia

Describe the fear

Fear of wind

Enter:

1.    To add a fear

2.    To remove a fear

3.    To look up a fear

4.    Print all fears

5.    Quit

1

What is the fear’s name

Cyberphobia

Describe the fear

Fear of computers or working on a computer

Enter:

1.    To add a fear

2.    To remove a fear

3.    To look up a fear

4.    Print all fears

5.    Quit

1

What is the fear’s name

Kakorrhaphiophobia

Describe the fear

Fear of failure or defeat

Enter:

1.    To add a fear

2.    To remove a fear

3.    To look up a fear

4.    Print all fears

5.    Quit

1

What is the fear’s name

Lutraphobia

Describe the fear

Fear of otters

Enter:

1.    To add a fear

2.    To remove a fear

3.    To look up a fear

4.    Print all fears

5.    Quit

1

What is the fear’s name

Arachibutyrophobia

Describe the fear

Fear of peanut butter sticking to the roof of the mouth

Enter:

1.    To add a fear

2.    To remove a fear

3.    To look up a fear

4.    Print all fears

5.    Quit

1

What is the fear’s name

Barophobia

Describe the fear

Fear of gravity

Enter:

1.    To add a fear

2.    To remove a fear

3.    To look up a fear

4.    Print all fears

5.    Quit

1

What is the fear’s name

Samhainophobia

Describe the fear

Fear of Halloween

4

Ancraophobia

Fear of wind

Arachibutyrophobia

Fear of peanut butter sticking to the roof of the mouth

Barophobia

Fear of gravity

Cyberphobia

Fear of computers or working on a computer

Kakorrhaphiophobia

Fear of failure or defeat

Lutraphobia

Fear of otters

Samhainophobia

Fear of Halloween

Enter:

1.    To add a fear

2.    To remove a fear

3.    To look up a fear

4.    Print all fears

5.    Quit

5

Goodbye Fears

Explanation / Answer

import java.io.*; import java.util.ArrayList; import java.util.Hashtable; class Test { static Hashtable phobiaHashTable = new Hashtable(); public static void main(String[] args) throws IOException { System.out.println("Welcome to the fear hash table!"); while (true){ System.out.println("Enter: " + "1. To add a fear " + "2. To remove a fear " + "3. To look up a fear " + "4. Print all fears " + "5. Quit"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); Integer c = Integer.parseInt(br.readLine()); switch (c){ case 1:{ System.out.println("What is the fear’s name"); String name = br.readLine(); System.out.println("Describe the fear"); String desc = br.readLine(); add(new Phobia(name,desc)); break; } case 2:{ System.out.println("What is the fear’s name"); String name = br.readLine(); System.out.println("Describe the fear"); String desc = br.readLine(); remove(name, desc); break; } case 3:{ System.out.println("Enter fear’s name"); String name = br.readLine(); System.out.println(lookup(name)); break; } case 4:{ printHashTable(); break; } case 5:{ break; } default: System.out.println("Invalid choice"); break; } } } public static String getKey(String s){ return s.charAt(0)+""; } public static void add(Phobia phobia){ String key = getKey(phobia.getName()); if(!phobiaHashTable.containsKey(key)){ ArrayList list = new ArrayList(); list.add(phobia); phobiaHashTable.put(key,list); }else { ArrayList list = phobiaHashTable.get(key); list.add(phobia); phobiaHashTable.put(key,list); } } public static void remove(String name, String desc){ if(getKey(name)==null) { System.out.println("No such entry found"); return; } boolean found = false; for(String key : phobiaHashTable.keySet()){ ArrayList list = phobiaHashTable.get(key); Phobia r = new Phobia(name,desc); for(Phobia p : list){ if(p.equals(r)){ found = true; list.remove(r); } } phobiaHashTable.put(key,list); } if(found) System.out.println("Successfully removed"); else System.out.println("No such entry!"); } public static String lookup(String name){ if(getKey(name)==null) { System.out.println("No such entry found"); return null; } for(String key : phobiaHashTable.keySet()){ ArrayList list = phobiaHashTable.get(key); for(Phobia p : list){ if(p.getName().equalsIgnoreCase(name)){ return p.getDescription(); } } phobiaHashTable.put(key,list); } return null; } public static void printHashTable(){ for(String key : phobiaHashTable.keySet()){ ArrayList list = phobiaHashTable.get(key); for(Phobia p : list){ System.out.println(p); } } } } class Phobia{ String name; String description; public Phobia(String name, String description) { this.name = name; this.description = description; } @Override public String toString() { return "Name:"+name+" , Description:"+description; } @Override public boolean equals(Object o) { Phobia p = (Phobia) o; return p.getName().equalsIgnoreCase(name) && p.getDescription().equalsIgnoreCase(description); } /* @Override public int hashCode() { int result = name != null ? name.hashCode() : 0; result = 31 * result + (description != null ? description.hashCode() : 0); return result; } */ public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }