Question: Create a class hierarchy which has an abstract class called WordList (the same class as in project 2 but it is abstract). This class should

Create a class hierarchy which has an abstract class called WordList (the same class as in project 2 but it is abstract). This class should have two subclasses: UnsortedWordList and SortedWordList, each having a method called add. In the UnsortedWordList the method add will add to the end of the list (append), and in the SortedWordList it will do an insert. So now, rather than in project 2 where you used two of the same kind of list (WordList) you will use one unsorted list and one sorted list.

Create a new exception called IllegalWordException by extending lllegalArgumentException.

When you create a new Word from a String read from the input file, catch any exceptions thrown by the constructor and print the offending string to the console along with the message from the Exception. The constructor should check that the word is 3 characters, and all the characters are letters.

Add a File menu to your GUI which has menu items for Open and Quit. You should now be able to select an input file using the GUI.

Submitting the Project.

You should now have at least the following files to submit for this project:

Project3.java Word.java WordGUI.java WordNode.java WordList.java UnsortedWordList.java SortedWordList.java FileMenuHandler.java IllegalWordException.java 

*************************************************

Thats what i have so far

******************************

//FileMenuHandler

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.io.*;

public class FileMenuHandler implements ActionListener {

JFrame jframe;

public FileMenuHandler (JFrame jf) {

jframe = jf;

}

public void actionPerformed(ActionEvent event) {

String menuName = event.getActionCommand();

if (menuName.equals("Open"))

JOptionPane.showMessageDialog(null,"You clicked on Open");

else if (menuName.equals("Quit"))

JOptionPane.showMessageDialog(null,"You clicked on Quit");

} //actionPerformed

}

//project3

import java.io.File; import java.io.FileNotFoundException; import java.util.*;

public class Project3 { public static void main(String[] args) {

String fileName = "words.txt"; WordList list1=new WordList(),list2=new WordList(); //create 2 empty lists, list 1 is for 3 letter words

// read from file and store 3 letter words in list1 and others in list2 inputFromFile(fileName, list1,list2);

// Creates a GUI object with the title Project1 WordGUI myGUI = new WordGUI("Project2"); WordGUI.intialize();

// Method calls to: 1.print unsorted list to GUI, // 2.sort the list, 3.Print sorted list to GUI myGUI.printUnsorted(list1); myGUI.printSorted(list2);

}

// Reads text file line by line and stores words in list1 if size is less than 3 characters and list2 otherwise /** * * @param myFile * @param list1 * @param list2 */ private static void inputFromFile(String myFile, WordList list1 ,WordList list2) { Scanner in; try { in = new Scanner(new File(myFile)); } catch (FileNotFoundException e) { System.out.println("Input file not found : "+myFile); return; }

String line;

//while there are more lines to process while (in.hasNextLine()) {

line = in.nextLine(); // Creates StringTokenizer object that will read each word separately StringTokenizer st = new StringTokenizer(line, ",");

while (st.hasMoreTokens()) { // Creates a new word from the next string in text String subWord = st.nextToken();

// If word is valid store it in array, otherwise print to console if (isValidWord(subWord)) { Word w = new Word(subWord); if(subWord.length()!=3){ System.out.println(subWord); }

else{ list2.insert(w); //sorted list1.append(w);//unsorted }

}

}

}

in.close(); }

// Checks if the word is not of size 3 and if any characters // are not letters, if so invalid, else valid word /** * * @param singleWord character letter * @return */ private static boolean isValidWord(String singleWord) { for (int i = 0; i < singleWord.length(); i++) { if(!Character.isLetter(singleWord.charAt(i))) return false; } return true;

}

}

//word

public class Word {

static int WORD_SIZE=3;

private String word;

int counter=0;

/**

*

* @param constructor

*/

public Word(String w){//constructor

word=w;

}

/**

* to String

*/

public String toString(){

return word;

}

/**

*

* @param other compareTo Method

* @return

*/

public int compareTo(Word other){

return word.compareTo(other.toString());

}

}

//wordGui

import javax.swing.*;

import java.awt.*;

public class WordGUI extends JFrame {

public WordGUI(String title, int height, int width) {

setTitle(title);

setSize(height,width);

setLocation (400,200);

createFileMenu();

setDefaultCloseOperation(EXIT_ON_CLOSE);

setVisible(true);

} //SSNGUI

private void createFileMenu( ) {

JMenuItem item;

JMenuBar menuBar = new JMenuBar();

JMenu fileMenu = new JMenu("File");

FileMenuHandler fmh = new FileMenuHandler(this);

item = new JMenuItem("Open"); //Open...

item.addActionListener( fmh );

fileMenu.add( item );

fileMenu.addSeparator(); //add a horizontal separator line

item = new JMenuItem("Quit"); //Quit

item.addActionListener( fmh );

fileMenu.add( item );

setJMenuBar(menuBar);

menuBar.add(fileMenu);

} //createMenu

} //SSNGUI

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!