Question: Im having trouble coding these methods The prompt is as follows and there is a picture of my code below Method Details Markov() The constructor

Im having trouble coding these methods

The prompt is as follows and there is a picture of my code below

Method Details

Markov()

The constructor for this class will initialize the HashMap words with the key PUNCTUATION and a value of a new ArrayList.

This method will also set prevWord = PUNCTUATION.

getWords()

This is a getter that returns the list of words

addFromFile(String)

Others methods called: addLine(String)

This method takes a String called filename that represents the file that will be parsed into the words HashMap.

This method opens the file and calls the addLine method to parse the individual lines from the filename.

This method should catch any errors that are generated from file operations.

addLine(String)

Other methods called: addWord(String)

This is a simple method that performs two very important operations.

First it ensures that the line being read is not 0 length. This is important because without this functionality an empty string (blank line) will break the program.

Once a String is determined to have content, the String is split into individual words. These words are then passed to the addWord(String) method.

addWord(String)

Other methods called : endsWtihPunctuation(String)

This is the method that does most of the processing for this application. The first thing that is done is the previous word is checked to see if it ended with punctuation. If the previous word ended with punctuation then the current word is added under the PUNCTUATION key in the words HashMap.

If the previous word did NOT end with punctuation then the HashMap words is checked to see if the previous word has an entry in the HashMap. If the previous word is not present in the HashMap, it will need to be added.

Once the previous word is present in the HashMap the current word is added to the ArrayList that uses the previous word as a key.

Note: We will use a method called endsWtihPunctuation(String) to check for punctuation. The reason for this is to make debugging easier and because it is used in the next method.

getSentence()

Other methods called: randomWord(String), endsWtihPunctuation(String)

This method is responsible for building a sentence from the contents of the HashMap words. It will make use of the method randomWord(String) described below.

This method first pics a random word from the values stored under the key PUNCTUATION. This word becomes the current word.

If the current word does not end in punctuation, it is added to the sentence being constructed along with a space and a new random word is selected.

If the current word DOES end in punctuation, it is added to the String being constructed and no additional word is chosen.

The String being built is the returned.

randomWord(String)

This method takes a word as a parameter. That word is used to retrieve an ArrayList of words that follow from the HashMap words.

A random word is chosen from the ArrayList and returned.

endsWithPunctuation(String)

This method takes a String and checks if the last character of the String exists in PUNCTUATION_MARKS. If the last character does exist in PUNCTUATION_MARKS the method returns true, otherwise it returns false

This method should also catch any errors that may occur when checking for punctuation. If an error is caught, print the word that caused the error along with an error message.

Im having trouble coding these methods The prompt is as follows and

import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.Scanner; public class Markov { private String PUNCTUATION; private String PUNCTUATION_MARKS; private HashMap> words; private String prevWord; Markov() { } HashMap> getWords() { return words; } void addFromFile(String ADDFROMFILE){ } void addLine(String ADDLINE){ } void addWord(String ADDWORD){ } String getSentence(){ // not coded yet return null; } String randomWord(String g){ return null; } boolean endsWithPunctuation(String k){ return true; } //generate to string }

Fimport java.io.File; import java.util.ArrayList; import java.util. HashMap; import java.util.Scanner; public class Markov { private String PUNCTUATION; private String PUNCTUATION_MARKS; private HashMap> words; private String prevWord; Markov { } HashMap> getWords() {...} void addFromFile(String ADDFROMFILE){} void addLine(String ADDLINE){} void addWord(String ADDWORD) { G String getSentence(){...} String randomWord(String g) { return null; } boolean endsWithPunctuation(String k) { return true; } //generate to string Fimport java.io.File; import java.util.ArrayList; import java.util. HashMap; import java.util.Scanner; public class Markov { private String PUNCTUATION; private String PUNCTUATION_MARKS; private HashMap> words; private String prevWord; Markov { } HashMap> getWords() {...} void addFromFile(String ADDFROMFILE){} void addLine(String ADDLINE){} void addWord(String ADDWORD) { G String getSentence(){...} String randomWord(String g) { return null; } boolean endsWithPunctuation(String k) { return true; } //generate to string

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!