PLEASE HELP IN JAVA: This is an exercise in using the java LinkedList class. Usi
ID: 3750719 • Letter: P
Question
PLEASE HELP IN JAVA:
This is an exercise in using the java LinkedList class.
Using input file words.txt , create a LinkedList BUT you must NOT add a word that is already in the list and you MUST add them in the order received to the end of the list.
For example, if your input was cat, cat, dog, mouse, dog, horse
your list would now contain cat, dog, mouse, horse. And the head of the list would be cat, and the tail would be horse. Note: it is NOT sorted.
This list represents a code, and the code for one word is the next word in the list. So the code for cat is dog and the code for mouse is horse, and the code for horse is cat(go round to the head of the list).
Now ask your user for a phrase (input from the keyboard) and output the coded phrase. In the above example if the input was "mouse cat" the output would be "horse dog".
How do you know if you have the correct output? I suggest you make your own (small) input file so you can test it.
Rubric:
* use of try/catch
*use of while loop to collect inout
* no duplicates put into list
* prompt user for phrase
* code the phrase
* correct output
words.txt (around 1000 words)
noncollectable
reallocation
drenching
obnoxious
venality
dybbuk
shotgun
changelessly
handiwork
unheralded
dovecote
anode
spellbind
psychologist
improvisational
prejudiced
apply
pokey
secular
masterfully
at
overdrawn
Explanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
// LinkedList.java
public class LinkedList {
// pointers to head and tail
private Node head;
private Node tail;
private int size;// number of words
/**
* default constructor
*/
public LinkedList() {
head = null;
tail = null;
size = 0;
}
/**
* @return true if the list is empty
*/
public boolean isEmpty() {
return size == 0;
}
/**
* return true if a given word exists in the linked list, otherwise false
*
* @param word
* - word to be checked (case sensitive)
*/
public boolean contains(String word) {
Node node = head;
// looping through the list to check if the word existss
while (node != null) {
if (node.getWord().equals(word)) {
return true;
}
node = node.getNext();
}
return false;
}
/**
* method to add a word to the list
*/
public void add(String word) {
// adding the word only if it is not already added
if (!contains(word)) {
Node newNode = new Node(word);
if (isEmpty()) {
// adding as first node
head = newNode;
tail = newNode;
} else {
// adding at the tail
tail.setNext(newNode);
tail = newNode;
}
size++;
}
}
/**
* method to find the code of a word in the list
*
* @param word
* - original word
* @return the next word of the given word in the list, will return head
* node's data if the word is at the tail, or return the word
* unchanged if it is not found in the list
*/
public String getCode(String word) {
Node temp = head;
while (temp != null) {
if (temp.getWord().equals(word)) {
// found the word, getting next word
if (temp.getNext() == null) {
// no next node, so returning head node's data
return head.getWord();
} else {
// returning word at next position
return temp.getNext().getWord();
}
}
temp = temp.getNext();
}
// not found, returning word unchanged
return word;
}
@Override
public String toString() {
// returning a String representation of the list
String data = "[";
Node temp = head;
for (int i = 0; i < size; i++) {
data += temp.getWord();
if (i != size - 1) {
data += ", ";
}
temp = temp.getNext();
}
data += "]";
return data;
}
/**
* private inner class to represent a Node, must be placed within
* LinkedList.java (inside LinkedList class)
*/
private class Node {
String word;
Node next;
public Node(String word) {
this.word = word;
}
public void setNext(Node next) {
this.next = next;
}
public Node getNext() {
return next;
}
public void setWord(String word) {
this.word = word;
}
public String getWord() {
return word;
}
}
}
// Main.java (with main method)
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main {
// method to load words from a file, fill a linked list and return it
static LinkedList loadWords(String fileName) throws FileNotFoundException {
LinkedList wordsList = new LinkedList();
Scanner scanner = new Scanner(new File(fileName));
while (scanner.hasNext()) {
String word = scanner.nextLine();
wordsList.add(word);
}
return wordsList;
}
public static void main(String[] args) {
Scanner inScanner = new Scanner(System.in); //user input scanner
Scanner textScanner; // to scan individual words in the input text sequence
String input = "";
try {
//loading words from file
LinkedList wordsList = loadWords("words.txt");
//displaying loaded words, for debugging. remove it if you want
System.out.println("Loaded words: " + wordsList);
//looping until user quits
while (!input.equalsIgnoreCase("quit")) {
System.out.print("Enter a phrase (or 'quit' to end): ");
input = inScanner.nextLine();
if (!input.equalsIgnoreCase("quit")) {
//iterating through input phrase
textScanner = new Scanner(input);
System.out.print("Code: ");
//displaying the code for each words
while (textScanner.hasNext()) {
String word = textScanner.next();
System.out.print(wordsList.getCode(word) + " ");
}
System.out.println();
}
}
} catch (FileNotFoundException e) {
//words.txt file not found, must be there in the same directory
e.printStackTrace();
}
}
}
//OUTPUT
Loaded words: [noncollectable, reallocation, drenching, obnoxious, venality, dybbuk, shotgun, changelessly, handiwork, unheralded, dovecote, anode, spellbind, psychologist, improvisational, prejudiced, apply, pokey, secular, masterfully, at, overdrawn]
Enter a phrase (or 'quit' to end): noncollectable shotgun
Code: reallocation changelessly
Enter a phrase (or 'quit' to end): masterfully overdrawn
Code: at noncollectable
Enter a phrase (or 'quit' to end): dybbuk someunknowntext
Code: shotgun someunknowntext
Enter a phrase (or 'quit' to end): quit
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.