Assignment 10 - Help

Hashtables, Dictionaries, Property Lists

Here's a bit of help, in case you need it.

Small tests such as this can be really helpful.

tucotuco.cs.indiana.edu% ls -l

total 2 -rw-r--r-- 1 dgerman students 423 Apr 6 17:04 Tests.java
tucotuco.cs.indiana.edu% cat Tests.java
import java.util.*; import java.io.*; class Tests { public static void main(String[] args) { try { Properties p = new Properties(); p.put("Java", "Language"); p.put("Pascal", "Another language"); p.put("C", "Yet another language"); p.save(new FileOutputStream("Dictionary"), "Header"); } catch (Exception e) { System.out.println("E: " + e.toString()); } } }
tucotuco.cs.indiana.edu% javac Tests.java
tucotuco.cs.indiana.edu% ls -l
total 6 -rw-r--r-- 1 dgerman students 1113 Apr 6 17:04 Tests.class -rw-r--r-- 1 dgerman students 423 Apr 6 17:04 Tests.java
tucotuco.cs.indiana.edu% java Tests
tucotuco.cs.indiana.edu% ls -l
total 8 -rw-r--r-- 1 dgerman students 99 Apr 6 17:04 Dictionary -rw-r--r-- 1 dgerman students 1113 Apr 6 17:04 Tests.class -rw-r--r-- 1 dgerman students 423 Apr 6 17:04 Tests.java
tucotuco.cs.indiana.edu% cat Dictionary
#Header #Tue Apr 06 18:04:47 EDT 1999 C=Yet another language Pascal=Another language Java=Language
tucotuco.cs.indiana.edu%

Here now is a complete (minimal) solution for assignment 10:

import java.io.*; 
import java.awt.*;
import BreezyGUI.*; 
import java.util.*; 

public class Homework10 extends GBFrame { 
  MenuItem option1 = addMenuItem("Word", "Add"); 
  MenuItem option2 = addMenuItem("Word", "Delete"); 
  MenuItem option3 = addMenuItem("Word", "Show"); 
  MenuItem option31= addMenuItem("Word", "List"); 
  MenuItem option4 = addMenuItem("File", "Load"); 
  MenuItem option5 = addMenuItem("File", "Save"); 
  MenuItem option6 = addMenuItem("File", "Quit"); 
  public void menuItemSelected (MenuItem menuItemObj) { 
    if (menuItemObj == option1) {  //------------------ Word/Add
      dict.put(word.getText(), wDef.getText()); 
      messageBox("Word entry added."); 
    } else if (menuItemObj == option2) { //------------ Word/Delete
      dict.remove(word.getText()); 
      messageBox("Entry for " + word.getText() + " deleted."); 
    } else if (menuItemObj == option3) { //------------ Word/Show
      String key = word.getText(); 
      String def = (String)dict.getProperty(key); 
      wDef.setText(def); 
      messageBox(word + ": " + dict.getProperty(key));   
    } else if (menuItemObj == option31) { //----------- Word/List
      Enumeration e = dict.keys(); String words; 
      for (words = ""; e.hasMoreElements(); ) {
        words += e.nextElement() + "\n"; 
      } 
      messageBox("List: \n" + words); 
    } else if (menuItemObj == option4) { //------------ File/Load
      try {
        dict.load(new FileInputStream("Dictionary")); 
      } catch (Exception e) {
        messageBox("Something went wrong:\n" + e.toString()); 
        dict = new Properties(); 
      } 
      messageBox("Dictionary has been loaded. "); 
    } else if (menuItemObj == option5) { //------------ File/Save
      try {
        dict.save(new FileOutputStream("Dictionary"), 
                  "My Dictionary"); 
      } catch (Exception e) {
        messageBox("Something went wrong on output:\n" 
                   + e.toString()); 
      } 
      messageBox("Dictionary saved now."); 
    } else if (menuItemObj == option6) {
      // messageBox("Quit: Thanks for using this program. "); 
      System.exit(0); 
    } else {

    } 
  } 
  Label      text = addLabel     ("Type your word here: ",
                                          1, 1, 1, 1); 
  TextField  word = addTextField ("Java", 1, 3, 1, 1); 
  TextArea   wDef = addTextArea  ("Computer programming language.",
                                          2, 1, 3, 3); 
  Properties dict = new Properties(); 
  public static void main(String[] args) {
    Frame f = new Homework10(); 
    f.setSize(200, 200); 
    f.setVisible(true); 
  } 
}