Question: Java Using Eclipse Recursive syntax exercise SEE BELOW FOR AN EXAMPLE OF THIS Write a similar program (to the example below), that implements the following

Java Using Eclipse

Recursive syntax exercise

SEE BELOW FOR AN EXAMPLE OF THIS

Write a similar program (to the example below), that implements the following rules:

For each of the first three rules, you should write a subroutine to represent that rule. Note that a choice of alternatives (represented in the rules by "|") can be implemented using a switch or if..else statement; the various choices don't necessarily have to have the same probability. An optional element (represented by brackets, "[ xxx ]") can be implemented by a simple if. And a repeated optional element (represented by brackets with dots, "[ xxx ]...") can be represented by a while loop. You should implement the first four rules exactly as stated here. The main routine should call the subroutine to generate random sentences.

You have to be careful in this program to avoid infinite recursion in this program. Since it will use random choices, there is no guarantee that the recursion will ever end. If your probabilities of doing recursion and continuing loops are too high, it is possible for the program to get lost in recursive calls forever -- or to produce some finite but ridiculously long sentences. You should adjust your probabilities to make sure that this doesn't happen, but that you still get some interesting sentences.

RULES AND WORDS

::= [ ] ::= ::= | [ ]. [ who ] ::= | | is | believes that ::= and | or | but | because ::= Fred | Jane | Richard Nixon | Miss America ::= man | woman | fish | elephant | unicorn ::= a | the | every | some ::= big | tiny | pretty | bald ::= runs | jumps | talks | sleeps ::= loves | hates | sees | knows | looks for | finds

EXAMPLE:

public class SimpleRandomSentences {

static final String[] nouns = { "farmer", "rooster", "judge", "man", "maiden",

"cow", "dog", "cat", "cheese" };

static final String[] verbs = { "kept", "waked", "married",

"milked", "tossed", "chased", "lay in" };

static final String[] modifiers = { "that crowed in the morn", "sowing his corn",

"all shaven and shorn",

"all forlorn", "with the crumpled horn" };

public static void main(String[] args) {

while (true) {

randomSentence();

System.out.println(". ");

try {

Thread.sleep(3000);

}

catch (InterruptedException e) {

}

}

}

static void randomSentence() {

System.out.print("this is ");

if (Math.random() > 0.2)

randomNounPhrase();

System.out.print("the house that Jack built");

if (Math.random() > 0.75) {

System.out.print(" and ");

randomSentence();

}

}

static void randomNounPhrase() {

int n = (int)(Math.random()*nouns.length);

int v = (int)(Math.random()*verbs.length);

int m = (int)(Math.random()*modifiers.length);

System.out.print("the " + nouns[n]);

if (Math.random() > 0.75)

System.out.print(" " + modifiers[m]);

System.out.print(" that " + verbs[v] + " ");

if (Math.random() > 0.5)

randomNounPhrase();

}

AMENDED WITH CLARIFICATION AND MY WORK

What they want is for us to use recursive code with the words provided to come up with random sentences. The sentences do not have to make sense. It is just recursive success that is important.

This is what I have but it does not work. Maybe you can understand what this means by seeing my work.

import java.util.*;

public class RandomSentences {

// declaring parts of speech examples in arrays

public String[] sentence;

static final String[] conjunction = {"and", "or", "but", "because"};

static final String[] proper_noun = {"Fred", "Jane", "Richard Nixon", "Miss America"};

static final String[] common_noun = {"man", "woman", "fish", "elephant", "unicorn"};

static final String[] determiner = {"a", "the", "every", "some"};

static final String[] adjective = {"big", "tiny", "pretty", "bald"};

static final String[] intransitive_verb = {"runs", "jumps", "talks", "sleeps"};

static final String[] transitive_verb = {"loves", "hates", "sees", "knows", "looks for", "finds"};

int bool = 0;

public static void main(String[] args) {

while (true) {

randomSentence();

System.out.println(". ");

try {

Thread.sleep(3000);

}

catch (InterruptedException e) {

}

}

}

static void randomSentence() {

System.out.print(". ");

if (Math.random() > 0.2)

randomNounPhrase();

System.out.print(". ");

if (Math.random() > 0.75) {

System.out.print(" . ");

randomSentence();}

}

static void randomNounPhrase() {

int n = (int)(Math.random()*nouns.length);

int v = (int)(Math.random()*verbs.length);

int m = (int)(Math.random()*modifiers.length);

System.out.print("the " + nouns[n]);

if (Math.random() > 0.75)

System.out.print(" " + modifiers[m]);

System.out.print(" that " + verbs[v] + " ");

if (Math.random() > 0.5)

randomNounPhrase();

}

}

}

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!