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

WRITE CODE IN JAVA Encyclopedia of Phobias Objective: It is always a good idea t

ID: 3691817 • Letter: W

Question

WRITE CODE IN JAVA

Encyclopedia of Phobias

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

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:

To add a fear

To remove a fear

To look up a fear

Print all fears

Quit

1

What is the fear’s name

Ancraophobia

Describe the fear

Fear of wind

Enter:

To add a fear

To remove a fear

To look up a fear

Print all fears

Quit

1

What is the fear’s name

Cyberphobia

Describe the fear

Fear of computers or working on a computer

Enter:

To add a fear

To remove a fear

To look up a fear

Print all fears

Quit

1

What is the fear’s name

Kakorrhaphiophobia

Describe the fear

Fear of failure or defeat

Enter:

To add a fear

To remove a fear

To look up a fear

Print all fears

Quit

1

What is the fear’s name

Lutraphobia

Describe the fear

Fear of otters

Enter:

To add a fear

To remove a fear

To look up a fear

Print all fears

Quit

1

What is the fear’s name

Arachibutyrophobia

Describe the fear

Fear of peanut butter sticking to the roof of the mouth

Enter:

To add a fear

To remove a fear

To look up a fear

Print all fears

Quit

1

What is the fear’s name

Barophobia

Describe the fear

Fear of gravity

Enter:

To add a fear

To remove a fear

To look up a fear

Print all fears

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:

To add a fear

To remove a fear

To look up a fear

Print all fears

Quit

5

Goodbye Fears

Explanation / Answer

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(getName().equals(p.getName()) && getDescription().equals(p.getDescription()) ){
           return true;
       }
       else{
           return false;
       }
   }
}

PhobiaHashTable.java


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.out.println("Goodbye Fears");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());
       }
   }  
}

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
   Kakorrhaphiophobia
Describe the fear
Fear of failure or defeat
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
Arachibutyrophobia
Describe the fear
Fear of peanut butter sticking to the roof of the mouth
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 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
Samhainophobia
Describe the fear
Fear of Halloween
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
   Kakorrhaphiophobia
Fear of failure or defeat
Arachibutyrophobia
Fear of peanut butter sticking to the roof of the mouth
Samhainophobia
Fear of Halloween
Barophobia

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
5
Goodbye Fears