Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

List of Words For this project we will use linked lists to store the unsorted and sorted Words (there should be no arrays in this

List of Words

For this project we will use linked lists to store the unsorted and sorted Words (there should be no arrays in this project.)

Create a class called WordNode which has fields for the data (a Word) and next (WordNode) instance variables. Include a one-argument constructor which takes a Word as a parameter. (For hints, see the PowerPoint on "Static vs. Dynamic Structures.)

 public WordNode (Word w) { . . } 

The instance variables should have private access. There will not be any get and set methods for the two instance variables.

Create linked list class called WordList. This should be a linked list with head node as described in lecture. Modify it so that the data type in the nodes is Word. The no-argument constructor should create an empty list with first and last pointing to an empty head node, and length equal to zero.

Include two methods in class WordList: append and insert. The append method will add the new node to the end of the list; the insert method will add the node in the proper position to keep the list sorted in order by Word.

Instantiate two linked lists, and for every Word read from the file, add it (if it is three letters) to the first list using append, and to the second list using insert. You will end up with the first list having the Words from the input file in the order they were read, and in the second list the Words will be in sorted order. Display the unsorted and sorted Words in the GUI just as in project 1.

Submitting the Project.

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

Project2.java Word.java WordGUI.java WordNode.java WordList.java 

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

/* +-----------------------+ | Fill in lines 52, 64. | +-----------------------+ */

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

public class WordGUI extends JFrame {

/** * */ private static final long serialVersionUID = 1L; static JFrame myFrame; static Container myContentPane; static TextArea unsorted, sorted;

// Constructor public WordGUI(String title) { setTitle(title);

}

public static void intialize() {

// Creates JFrame

myFrame = new JFrame("Project 1"); myFrame.setLayout(new GridLayout(1, 2)); myFrame.setSize(800, 400); myFrame.setLocation(200, 200); myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

myFrame.setVisible(true); myContentPane = myFrame.getContentPane();

// Creates a text area for unsorted/sorted list unsorted = new TextArea(); sorted = new TextArea(); unsorted.setEditable(false); sorted.setEditable(false);

// Adds the unsorted/sorted text areas to GUI myContentPane.add(unsorted); myContentPane.add(sorted); }

// Prints unsorted list by appending each word to 1st column of GUI // In the order read from file public void printUnsorted(Word[] list, int size) {

// Goes through every element in the unsorted list and adds it to unsorted textArea for (int i = 0; i < size; i++) { unsorted.append("Unsorted"+list[i]);

} }

// Prints sorted list by appending it to the 2nd column of GUI public void printSorted(Word[] wordArray, int myArrayLength) {

// Goes through every element in the sorted list and adds it to sorted textArea for (int i = 0; i < myArrayLength; i++) { sorted.append(wordArray[i]+"sorted");

}

} }

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

/*

+-----------------------+

| Fill in lines 21, 29. |

+-----------------------+

*/

public class Word {

static int WORD_SIZE = 3;

String word;

// Constructor which takes in a string

// and stores it in word

public Word(String w) {

this.word=w;

// TODO: Fill in code here

}

// method for converting a string into a word

public String toString() {

return word;

// TODO: Fill in code here

}

// returns negative number if first is < second

// returns positive number if first > second

// and zero if the same

public int compareTo(Word other) {

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

// TODO: Fill in code here

}

}

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

import java.util.*;

public class Project1 {

public static void main(String[] args) {

String fileName = "words.txt";

Word[] wordArray = new Word[100];

// Stores the number of words put into the array

int myArrayLength = inputFromFile(fileName, wordArray);

// Creates a GUI object with the title Project1

WordGUI myGUI = new WordGUI("Project1");

WordGUI.intialize();

// Method calls to: 1.print unsorted list to GUI,

// 2.sort the list, 3.Print sorted list to GUI

myGUI.printUnsorted(wordArray, myArrayLength);

selectSort(wordArray, myArrayLength);

myGUI.printSorted(wordArray, myArrayLength);

}

// Sorts words in alphabetical order using selection sort

private static void selectSort(Word[] wordArray, int myArrayLength) {

for (int i = 0; i < myArrayLength - 1; i++) {

int indexLowest = i;

for (int j = i + 1; j < myArrayLength; j++) {

/*

* If the word at indexLowest comes after the word in the

* current element alphabetically, new lowestIndex is the index

* of the current element

*/

if (wordArray[indexLowest].compareTo(wordArray[j]) >= 1)

indexLowest = j;

} // for j

// swaps the word at the lowest index with the word at the top of

// list that isn't sorted already

if (indexLowest != i) {

Word temp = wordArray[indexLowest];

wordArray[indexLowest] = wordArray[i];

wordArray[i] = temp;

} // if

} // for i

}

// Reads text file line by line and stores words into an array of words

private static int inputFromFile(String myFile, Word[] words) {

TextFileInput in = new TextFileInput(myFile);

int lengthFilled = 0;

// Read line

String line = in.readLine();

while (lengthFilled < words.length && line != null) {

// 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);

subWord+=w;

}

else

System.out.println(subWord);

}

// reads in next line

line = in.readLine();

}

// If array isn't big enough

if (line != null) {

System.out.println("File contains too many words.");

System.out.println("This program can process only " + words.length + " words.");

System.exit(1);

}

in.close();

// return the size of the array filled

return lengthFilled;

}

// Checks if the word is not of size 3 and if any characters

// are not letters, if so invalid, else valid word

private static boolean isValidWord(String singleWord) {

if (singleWord.length() != 3)

return false;

for (int i = 0; i < singleWord.length(); i++) {

if(!Character.isLetter(singleWord.charAt(i)))

return false;

}

return true;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Finance The Role Of Data Analytics In Manda Due Diligence

Authors: Ps Publishing

1st Edition

B0CR6SKTQG, 979-8873324675

More Books

Students also viewed these Databases questions