Answered Essay: This is an exercise in using the LinkedList class.

Help in JAVA:

This is an exercise in using the 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”.

-I suggest you make your own (small) input file so you can test it.

words.txt:

noncollectable
reallocation
drenching
obnoxious
venality
dybbuk
shotgun
changelessly
handiwork
unheralded
dovecote
anode
spellbind
psychologist
improvisational
prejudiced
apply
pokey
secular
masterfully
at
overdrawn
costar
scruff
awareness
impurely
tuft
minstrelsy
calcimine

Expert Answer

 

//*********Program Plan*********

//Add each element of the word.txt in LinkedList if it does not exist.To find the code word , find the index of phrase in //linked list and find the next element of the index

//*************Code****************

import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.LinkedList;

public class CodePhrase {

public static void main(String[] args) {

// declare wordLinkedList to store the words

LinkedList<String> wordLinkedList = new LinkedList<String>();

// declare and initialize the codedPhraes to store the output

String codedPhrase = “”;

// declare phrase string variable to read the input entered

String phrase;

// call function readWordTextFile which read file and return LinkedList

// store the linkedlist return from function call readWordTextFile()

wordLinkedList = readWordTextFile();

System.out.println(“Please enter a phrase”);

try {

// buffer reader to read input

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

// read whole phrase in one line and store into phrase

phrase = bufferedReader.readLine();

// declare and initlise the splitedPhrase array to store the each word of phrase

String[] splitedPhrases = phrase.trim().split(” “);

// for loop to iterate over all the word of the entered phrase

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

// find index of word in wordLinkedList

int index = wordLinkedList.indexOf(splitedPhrases[i].trim());

// index =-1 mean word does not exists in word LinkedList

if (index != -1) {

// check if the word is not last element of the wordLinkedList

if (index != wordLinkedList.size() – 1)

// then code word for this word is next element(i+1);

codedPhrase = codedPhrase + wordLinkedList.get(index + 1);

else

// if the word is last element of the wordLinkedList

// then add the first element as its code word

codedPhrase = codedPhrase + wordLinkedList.get(0);

// seprate the coded word by a space

if (i < splitedPhrases.length – 1)

codedPhrase = codedPhrase + ” “;

}

}

System.out.println(“nCoded phrase is: “);

System.out.println(codedPhrase);

} catch (IOException e) {

// catch the exception

e.printStackTrace();

}

}

// function to read the word.txt file

public static LinkedList<String> readWordTextFile() {

// declare wordLinkedList to store the words

// which is to be return by this function to its caller

LinkedList<String> wordLinkedList = new LinkedList<String>();

try {

// Open the file

FileInputStream fstream = new FileInputStream(“words.txt”);

BufferedReader br = new BufferedReader(new InputStreamReader(fstream));

// declare word

String word;

// Read File Line By Line

while ((word = br.readLine()) != null) {

// if word does not exist in the wordLinkedList

// then add the word in the wordLinkedList

if (isWordExist(wordLinkedList, word) == false)

wordLinkedList.add(word);

}

// Close the input stream

br.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

// return the wordLinkedList and exit from the function

return wordLinkedList;

}

// function to check if the word exisit in linkedlist

public static boolean isWordExist(LinkedList linkedList, String word) {

// if word exists in linkedlist then return true else return false

if (linkedList.contains(word))

return true;

else

return false;

}

}

//*****************Input/Output Screenshot**********

//word.txt

cat
cat
dog
mouse
dog
horse
deer
elephant
deer
mouse
monkey

//*****Output****

Please enter a phrase
dog horse elephant monkey

Coded phrase is:
mouse deer monkey cat

Buy Essay
Calculate your paper price
Pages (550 words)
Approximate price: -

Help Me Write My Essay - Reasons:

Best Online Essay Writing Service

We strive to give our customers the best online essay writing experience. We Make sure essays are submitted on time and all the instructions are followed.

Our Writers are Experienced and Professional

Our essay writing service is founded on professional writers who are on stand by to help you any time.

Free Revision Fo all Essays

Sometimes you may require our writers to add on a point to make your essay as customised as possible, we will give you unlimited times to do this. And we will do it for free.

Timely Essay(s)

We understand the frustrations that comes with late essays and our writers are extra careful to not violate this term. Our support team is always engauging our writers to help you have your essay ahead of time.

Customised Essays &100% Confidential

Our Online writing Service has zero torelance for plagiarised papers. We have plagiarism checking tool that generate plagiarism reports just to make sure you are satisfied.

24/7 Customer Support

Our agents are ready to help you around the clock. Please feel free to reach out and enquire about anything.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

HOW OUR ONLINE ESSAY WRITING SERVICE WORKS

Let us write that nagging essay.

STEP 1

Submit Your Essay/Homework Instructions

By clicking on the "PLACE ORDER" button, tell us your requires. Be precise for an accurate customised essay. You may also upload any reading materials where applicable.

STEP 2

Pick A & Writer

Our ordering form will provide you with a list of writers and their feedbacks. At step 2, its time select a writer. Our online agents are on stand by to help you just in case.

STEP 3

Editing (OUR PART)

At this stage, our editor will go through your essay and make sure your writer did meet all the instructions.

STEP 4

Receive your Paper

After Editing, your paper will be sent to you via email.

× How can I help you?