// Lab Feb. 12, 2003

/*
 * @(#)GUI1.java 1.0 03/02/12
 *
 * You can modify the template of this file in the
 * directory ..\JCreator\Templates\Template_1\Project_Name.java
 *
 * You can also create your own project template by making a new
 * folder in the directory ..\JCreator\Template\. Use the other
 * templates as examples.
 *
 */


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

class GUI1 extends JFrame {
	
	JTextField newWord = new JTextField(10);
	JTextArea display = new JTextArea();
	JScrollPane scroller = new JScrollPane(display);
	
	public GUI1() {
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				dispose();
				System.exit(0);
			}
		});
		
		Container c = getContentPane();  //default BorderLayout
		
		c.add(newWord, BorderLayout.NORTH);
		c.add(scroller, BorderLayout.CENTER);
		
		newWord.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				enterAction();
			}
		});
		
		newWord.setBorder(makeBorder("Enter word and press enter:"));
	}
	
	private void enterAction() {
		String str = newWord.getText();
		display.append(str + "\n");
		newWord.setText("");
		newWord.requestFocus();
	}
	
	private Border makeBorder(String str) {
		return BorderFactory.createTitledBorder(str);	
	}

	public static void main(String args[]) {
		
		GUI1 mainFrame = new GUI1();
		mainFrame.setSize(400, 400);
		mainFrame.setTitle("GUI1");
		mainFrame.setVisible(true);
	}
}

// WordList

/**
 *  Quiz One
 *
 *	Rewrite toString to return word:count pairs
 *
 */

import java.util.Vector;
import java.util.Arrays;
import java.util.List;

public class WordList {
	
	private Vector words = new Vector();
	private Vector counts = new Vector();
	private static final Integer ONE = new Integer(1);
	
	public int addWord(String word) {
		
		int foundIndex = words.indexOf(word);
		
		if (foundIndex == -1) {
			words.add(word);
			counts.add(ONE);
			foundIndex = words.size() - 1;	
		}
		else {
			int count = ((Integer)counts.get(foundIndex)).intValue();
			count++;
			counts.setElementAt(new Integer(count), foundIndex);			
		}
		
		return foundIndex;	
	}	
	
	public String toString() {
		//return words.toString() + counts.toString();
		Vector output = new Vector();
		
		for (int i = 0; i < words.size(); i++) {
			Object word = words.get(i);
			Object count = counts.get(i);
			output.add(word + ":" + count);	
		}
		
		// return output.toString();
		// or sort it first
		Object[] wordsArray = output.toArray();
		Arrays.sort(wordsArray);		
		List out = Arrays.asList(wordsArray);
		

		return out.toString();
	}
	
	public static void main(String[] args) {
		WordList w = new WordList();
		
		w.addWord("bob");
		w.addWord("hey");
		w.addWord("cat");
		w.addWord("bob");
		w.addWord("cat");
		w.addWord("Cat");
		w.addWord("dog");
		w.addWord("bob");
		
		
		System.out.println(w);
	}
}