Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Sentence Program

807603Jan 20 2008 — edited Jan 20 2008
Write a program that can create random English sentences consisting out of a subject, verb, object, and an adverbial. The object and the adverbial are optional. Examples of such sentences are:
"The cat caught the mouse." (subject = "the cat", verb = "caught", object = "the mouse")
"John lies." (subject = "John", verb = "lies" )
"John met Mary in the mall."(subject = "John", verb = "met", object ="Mary", adverbial = "in the mall")
Part A. To implement this program, do the following:
Create an interface SentenceUnit. It has two methods: String toText(), which returns the contents of the grammatical unit (e.g., "the cat"), and String toStructure(), which returns the type of the unit followed by the text surrounded by parentheses (e.g., "subject (the cat)" )
Create the following classes, which implement SentenceUnit: NounUnit, Verb, Adverbial. Each of these has a zero-parameter constructor which creates a random text of the appropriate kind. A NounUnit can represent either a subject or an object.
The SentenceUnit classes have constant arrays that contain sample strings, e.g. "the cat" for a NounUnit. Each class should have at least five sample strings.
Create a Sentence class. It has fields called subject, verb, object, and adverbial; only object and adverbial may be null. Its constructor creates a random sentence by assigning newly created SentenceUnits to the fields. There are appropriate getter methods for all fields. Class Sentence also implements SentenceUnit.
Create a main method that creates a new random Sentence and prints it out, both as plain text (using toText()), and showing its structure (using toStructure()).


Okay, I've created the SentenceUnit classes, and now I am on the Sentence class. I can't seem to figure out how to put them all together in order to make a random sentence. Any help?

//This is what I have so far:

package hw2;

public class Sentence implements SentenceUnit
{
public Sentence(String sentenceSubject, String sentenceVerb, String sentenceAdverbial, String sentenceObject)
{
this.sentenceSubject=sentenceSubject;
this.sentenceVerb=sentenceVerb;
this.sentenceAdverbial=sentenceAdverbial;
this.sentenceObject=sentenceObject;
}

public String getSubject()
{
return sentenceSubject;
}

public String getVerb()
{
return sentenceVerb;
}

public String getAdverbial()
{
return sentenceAdverbial;
}

public String getObject()
{
return sentenceObject;
}
public String stringToText()
{
return null;
}


public String stringToStructure()
{
return null;
}

private String sentenceSubject;
private String sentenceVerb;
private String sentenceAdverbial;
private String sentenceObject;
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 17 2008
Added on Jan 20 2008
1 comment
129 views