Question: (Using Java Eclipse I have the textfile input .java already, I try to do it but i get stuck) Use linked lists to store the
(Using Java Eclipse I have the textfile input .java already, I try to do it but i get stuck) Use linked lists to store the unsorted and sorted Words (there should be no arrays) 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. 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. The GUI should have a GridLayout with one row and two columns. The left column should display the words in the order read from the file, and the right column should display the words in sorted order (using Selection Sort).
Include: Project.java Word.java WordGUI.java WordNode.java WordList.java The input file : (Word.txt) Each line of the input file may contain several words separated by commas. You will need to use a StringTokenizer to separate out the individual words.
words.txt
catratsatthe
cat,is,red my,dog,is,not,blue
import java.awt.*; import javax.swing.*;
public class WordGUI {
public void display(String[] stringArray, String[] sortedArray ) { JFrame frame = new JFrame("WordGUI"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500,600);
Container contain = new Container(); contain = frame.getContentPane(); contain.setLayout(new GridLayout(1,2));
JTextArea textArea = new JTextArea(); String words=""; for (int i = 0; i < stringArray.length; i++) { words = words + " " + stringArray[i]; } textArea.setText(words); contain.add(textArea); JTextArea sortedTextArea = new JTextArea(); words=""; for (int i = 0; i < sortedArray.length; i++) { words = words + " " + sortedArray[i]; } sortedTextArea.setText(words); contain.add(sortedTextArea); frame.setVisible(true); } }
//wordlist.java public class WordList {
private WordNode first = new WordNode(null);
private WordNode last = first; private int length = 0; public int getLength() { return length; }
public void append(Word d) { // TODO Code here for append WordNode temp = new WordNode(d); this.last.next = temp; this.last = temp; this.length++;
}
public void insert(Word b) { }
public String toString() { WordNode p = first.next; String returnString = ""; while (p != null) { returnString += p.data + " "; p = p.next; } return returnString; }
//word.java
public class Word {
static int WORD_SIZE = 3; private String word; public String getWord() { return word; }
public void setWord(String word) { this.word = word; }
Word(String word) { this.word=word; } }
//project2 .java
import java.io.File; import java.util.Arrays; import java.util.StringTokenizer;
public class Project2 { public static String str; public static StringTokenizer token; public static int range; public static String [] words; public static String [] readline(String file) { //open textfile TextFileInput myFile = new TextFileInput(file); //start to read line from file String word_line = myFile.readLine(); //while loop to checl the end of file while(word_line != null) { //change everything as tokens token = new StringTokenizer (word_line, ","); //check if more token while(token.hasMoreTokens()) { str = token.nextToken(); //check if the length of word equal to three if(str.length() == Word.WORD_SIZE) { }else { System.out.println(str); } word_line = myFile.readLine(); } } //copy hold array to sort it //return Arrays.copyOfRange(words, 0, range); } public static String[] sortthearray (String [] sort) { //copy the array from readline words = Arrays.copyOfRange(sort, 0, range); //for loop to check the array for(int j=0; j { int shortstring = j; for (int i=j+1 ; i { //keep track of the index to the smallest string if(words[shortstring].trim().compareTo(words[shortstring].trim())<0) { shortstring = i; } } //swap with the smallest string if(shortstring != j) { String temp = words[j]; words[j] = words[shortstring]; words[shortstring] = temp; } } return words; } public static void main(String[] args) { WordList orginallist = new WordList(); WordList sortlist = new WordList(); //call GUI.java WordGUI wordGUI = new WordGUI(); //call the display function from GUI wordGUI.display(orginallist,sortlist); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
