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

Objective: It is always a good idea to organize your fears in a data structure.

ID: 3691697 • Letter: O

Question

Objective:

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

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

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

PhobiaHashTable.java


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Hashtable;

import java.util.Set;

public class PhobiaHashTable {

   /**
   * @param args
   * @throws IOException
   */
   public static void main(String[] args) throws IOException {
       // TODO Auto-generated method stub
       BufferedReader scan = new BufferedReader(new InputStreamReader(System.in));
       Hashtable<String, Phobia> table = new Hashtable<String, Phobia>();
       System.out.println("Welcome to the fear hash table! ");
       int choice = 0;
       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");
       choice = Integer.parseInt(scan.readLine());
       switch(choice){
       case 1: addFear(scan, table); break;
       case 2: removeFear(scan, table); break;
       case 3: lookUpFear(scan, table); break;
       case 4: printFears(table); break;
       case 5: System.exit(0); break;
       }
       }
   }
   public static void addFear(BufferedReader scan, Hashtable<String, Phobia> table) throws IOException{
       System.out.println("What is the fear’s name");
       Phobia p = new Phobia();
       String name = scan.readLine();
       p.setName(name);
       System.out.println("Describe the fear");
       String description = scan.readLine();
       p.setDescription(description);
       table.put(name, p);
       System.out.println("Fear details added");
   }
   public static void removeFear(BufferedReader scan, Hashtable<String, Phobia> table) throws IOException{
       System.out.println("Enter the fear’s name that to be removed");
       String name = scan.readLine();
       table.remove(name);
       System.out.println("Fear removed");
   }
   public static void lookUpFear(BufferedReader scan, Hashtable<String, Phobia> table) throws IOException{
       System.out.println("Enter the fear’s name");
       String name = scan.readLine();  
       Phobia p = (Phobia)table.get(name);
       System.out.println(p.toString());
      
   }
   public static void printFears(Hashtable<String, Phobia> table){
       Set<String> keys = table.keySet();
       for(String key: keys){
           Phobia p = table.get(key);
           System.out.println(p.toString());
       }
   }  
}

Phobia.java


public class Phobia {
   private String name;
   private String description;
   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;
   }
   public String toString(){
       return getName() +" :: "+getDescription();
   }
   public boolean equlas(Phobia p){
       if(name.equals(p.getName())){
           return true;
       }
       else{
           return false;
       }
   }
}

Output:

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
Fear details added
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
Fear details added
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
Fear details added
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
Fear details added
Enter:
1. To add a fear
2. To remove a fear
3. To look up a fear
4. Print all fears
5. Quit
4
Ancraophobia :: Fear of wind
Cyberphobia :: Fear of computers or working on a computer
Barophobia :: Fear of gravity
Lutraphobia :: 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
2
Enter the fear’s name that to be removed
Lutraphobia
Fear removed
Enter:
1. To add a fear
2. To remove a fear
3. To look up a fear
4. Print all fears
5. Quit
4
Ancraophobia :: Fear of wind
Cyberphobia :: Fear of computers or working on a computer
Barophobia :: 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
3
Enter the fear’s name
Cyberphobia
Enter:
1. To add a fear
2. To remove a fear
3. To look up a fear
4. Print all fears
5. Quit
5