Question: n this project, you create a Java application that generates what I am calling pseudo-sentences. These imitation sentences are generated by randomly stringing together words,

n this project, you create a Java application that generates what I am calling pseudo-sentences. These imitation sentences are generated by randomly stringing together words, in the following form:

[article or pronoun] adjective1 noun1 adverb verb [article2 or pronoun2] adjective2 noun2

Here's an example:

The common cumin childishly comfort the contrarious cat.

Our point in this exercise is not so much to generate bad poetry and prose, as to learn about Strings, File input and output, and Java collections, in the form of ArrayList.

Step by step

You will need at least two classes WordList and SentenceBuilder. SentenceBuilder contains your main ( ) method, and instantiates and used instances of WordList.

1. Create a class WordList. It should have a constructor that takes a filename/filepath as a String parameter.

In the WordList constructor, instantiate an ArrayList, open the filename and read the words from the specified filename/filepath into the array list. (You may want to use an object of type Scanner to do this).

Use Java Exception handling (try/catch) to handle the errors that will occur if the file you specify as an argument to the WordList constructors cannot be opened.

2. Code a method in WordList called randomWordStartingWith ( ) that takes a String parameter and returns a randomly-selected word from the ArrayList, where that word is constrained to start with the same first letter as the word passed as an argument. This will be used to make alliterative pseudo-sentences.

Your WordList class should have member variable count that the randomWordStartingWith ( ) method increments each time it searches for a word. This will be used to see how many tries it takes to find a word that matches the required first letter.

In pseudo-code, this is:

Set count = 1

Pick a random integer between 0 and the size of the ArrayList holding the words.

Select the word at the ArrayList location given by the random integer.

While ( first letter of random word does not match first letter of word passed as arg)

Increment count

Pick a random integer between 0 and the size of the ArrayList holding the words.

Select the word at the ArrayList location given by the random integer.

return the randomly-selected word (which will match the required letter).

3. Provide a getter and a setter for count.

4. In your main ( ) method in SentenceBuilder, instantiate a separate WordList object for the contents of:

verb.txt

adv.txt

adj.txt

noun.txt

5. In main ( ) design your program to construct random pseudo-sentences as follows:

[article or pronoun] adjective1 noun1 adverb verb [article2 or pronoun2] adjective2 noun2

You supply a handful of articles (The and A and An), as well as a few pronouns such as That, Your, My and so on. These are just to make the pseudo-sentences seem a bit more natural. Select them randomly as needed.

Adjective1 comes from the WordList object you instantiated that holds the contents of the adj.txt file. So does Adjective2. Choose the other required parts of speech using the randomWordStartingWith ( word ) method of the appropriate WordList.

This will ensure that the sentences are alliterative, meaning that they all start with the same letter, except for the pronouns and articles you throw in.

6. Have code in your main to construct a moderate (say 100 or so) pseudo-sentences and print or otherwise display them so you can select a handful of the best (e.g. most novel, weirdest, or almost-makes-sense ones).

7. For each sentence you generate, print out the count of the number of attempts the WordList had to make to get a first-letter match. Note the pattern you see in the counts.

For this example pseudo-sentence:

The common cumin childishly comfort the contrarious cat.

When I ran this one, the total count for the search for all the required parts of speech starting with c was about 50. Your results will vary widely, of course.

Note: If the initial word starts with X or Z (I forget which), your program may be unable to find any matching alliterative words. Code a maximum number of attempts to avoid having your code loop forever.

Have Fun. I am looking forward to some interesting pseudo-sentences on this one.

noun.txt

adj.txt

adv.txt

verb.txt

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!