Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

*****USING ECLIPSE The grammar of natural languages such as English exhibits a recursive structure. This structure can be expressed in syntax rules written in the

*****USING ECLIPSE

The grammar of natural languages such as English exhibits a recursive structure. This structure can be expressed in syntax rules written in the format known as BNF (Bachus-Naur Form, named after the people who invented it). You have probably seen BNF used to specify the syntax of programming languages. While BNF is ordinarily used as a guide for parsing (that is, determining whether and how a given string follows the syntax rules), it can also be used a guide for generating strings that follow the syntax rules. An example of this can be found in the sample program SimpleRandomSentences. In this example, each syntax rule -- except for the most basic ones -- is represented by a method that generates strings that follow that rule. Where one syntax rule refers to another rule, the method that represents the first rule calls the method that represents the second rule. For the first exercise of the lab, you should write a similar program that implements the following rules: ::= [ ] ::= ::= | [ ]. [ 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 As in SimpleRandomSentences.java, you can use arrays to implement the last seven rules in this list. (You might improve on that program by writing a single method "void String randomItem(String[] listOfStrings)" for picking a random item from an array of strings.) You are welcome to add to or modify the items in the lists given here. 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.

****REQUIRED FILE #1

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class FamousFractals extends JFrame implements ActionListener {

public static void main(String[] args) {

JFrame window = new FamousFractals();

window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

window.setLocation(50, 50);

window.setVisible(true);

}

private int currentNumberOfPoints;

private InputCanvas input;

private DisplayCanvas display;

private JComboBox pointCountSelect;

private JComboBox recursionLevelSelect;

public FamousFractals() {

super("Famous Fractals");

pointCountSelect = new JComboBox();

recursionLevelSelect = new JComboBox();

display = new DisplayCanvas();

input = new InputCanvas(display);

JPanel canvases = new JPanel();

canvases.setBackground(Color.DARK_GRAY);

canvases.setLayout(new GridLayout(1,2,5,5));

canvases.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 5));

canvases.add(input);

canvases.add(display);

JPanel bottom = new JPanel();

bottom.add(new JLabel("Number of Points: "));

bottom.add(pointCountSelect);

for (int i = 3; i < 10; i++)

pointCountSelect.addItem("" + i);

pointCountSelect.setSelectedIndex(2);

currentNumberOfPoints = 5;

input.setPointCount(5);

pointCountSelect.addActionListener( new ActionListener() {

public void actionPerformed(ActionEvent evt) {

if (currentNumberOfPoints != pointCountSelect.getSelectedIndex() + 1) {

currentNumberOfPoints = pointCountSelect.getSelectedIndex() + 3;

input.setPointCount(currentNumberOfPoints);

checkMaxRecursionLevel();

display.setRecursionLevel(recursionLevelSelect.getSelectedIndex() + 1);

; }

}

});

JButton clear = new JButton("Clear");

bottom.add(Box.createHorizontalStrut(40));

bottom.add(clear);

clear.addActionListener( new ActionListener() {

public void actionPerformed(ActionEvent e) {

input.setPointCount(currentNumberOfPoints);

}

});

bottom.add(Box.createHorizontalStrut(40));

bottom.add(new JLabel("Recursion Level"));

bottom.add(recursionLevelSelect);

checkMaxRecursionLevel();

recursionLevelSelect.setSelectedIndex(3);

display.setRecursionLevel(4);

recursionLevelSelect.addActionListener( new ActionListener() {

public void actionPerformed(ActionEvent evt) {

display.setRecursionLevel(recursionLevelSelect.getSelectedIndex() + 1);

}

});

getContentPane().setLayout(new BorderLayout());

getContentPane().add(canvases, BorderLayout.CENTER);

getContentPane().add(bottom,BorderLayout.SOUTH);

setJMenuBar(makeMenus());

pack();

setResizable(false);

}

private void checkMaxRecursionLevel() {

int max = (int)(Math.log(100000)/Math.log(currentNumberOfPoints-1));

int curMax = recursionLevelSelect.getItemCount();

if (curMax < max) {

for (int i = curMax+1; i <= max; i++)

recursionLevelSelect.addItem("" + i);

}

else if (curMax > max) {

int selected = recursionLevelSelect.getSelectedIndex();

if (selected >= max)

recursionLevelSelect.setSelectedIndex(max-1);

for (int i = curMax-1; i >= max; i--)

recursionLevelSelect.removeItemAt(i);

}

}

private JMenuBar makeMenus() {

JMenuBar menubar = new JMenuBar();

JMenu menu = new JMenu("Examples");

menubar.add(menu);

String[] itemList = new String[] {

"Koch Curve", "Square Koch Curve", "Dragon Curve", "C-Curve", "Sierpinski Triangle",

null, "Quit"

};

for (String cmd : itemList) {

if (cmd == null) {

menu.addSeparator();

}

else {

JMenuItem item = new JMenuItem(cmd);

item.addActionListener(this);

menu.add(item);

}

}

return menubar;

}

public void actionPerformed(ActionEvent evt) {

String cmd = evt.getActionCommand();

if (cmd.equals("Quit"))

System.exit(0);

else if (cmd.equals("Koch Curve")) {

int height = (int)(400/Math.sqrt(3));

int[] coords = { 50,250, 50+400/3,250, 250,350-height, 50+800/3,250, 450,250 };

installExample(7,coords);

}

else if (cmd.equals("Square Koch Curve")) {

int[] coords = { 50,250, 50+400/3,250, 50+400/3,250-400/3, 50+800/3,250-400/3, 50+800/3,250, 450,250 };

installExample(6, coords);

}

else if (cmd.equals("Dragon Curve")) {

int h = (int)(400/(2*Math.sqrt(3)));

int[] coords = { 50,250, 250,250-h, 250,250+h, 450,250};

installExample(8, coords);

}

else if (cmd.equals("C-Curve")) {

int h = 120;

int[] coords = {130,350, 250,350-h, 370,350};

installExample(14, coords);

}

else if (cmd.equals("Sierpinski Triangle")) {

int h = (int)(400*Math.sqrt(3)/4);

int[] coords = { 50,400, 250,400, 150,400-h, 350,400-h, 250,400, 450,400};

installExample(7, coords);

}

}

private void installExample(int recursionLevel, int[] coordinates) {

int pointCt = coordinates.length / 2;

currentNumberOfPoints = pointCt;

pointCountSelect.setSelectedIndex(pointCt - 3);

input.install(coordinates);

checkMaxRecursionLevel();

display.setRecursionLevel(recursionLevel);

recursionLevelSelect.setSelectedIndex(recursionLevel-1);

}

}

*****REQUIRED FILE #2

/*

The last verse of a well-known nursery rhyme:

This is the farmer sowing his corn

That kept the rooster that crowed in the morn

That waked the judge all shaven and shorn

That married the man all tattered and torn

That kissed the maiden all forlorn

That milked the cow with the crumpled horn

That tossed the dog

That worried the cat

That chased the rat

That ate the cheese

That lay in the house that Jack built.

Some rules that capture the syntax of this verse:

::= [ and ]

::= this is [ ] the house that Jack built

::= the [ ] that [ ]

::= farmer | rooster | judge | man | maiden | cow | dog | cat | cheese

::= kept | waked | married | milked | tossed | chased | lay in

::= that crowed in the morn | all shaven and shorn |

all tattered and torn | all forlorn | with the crumpled horn

This program implements these rules to generate random sentences. All the

verses of the rhyme can be generated, plus a lot of sentences that make no

sense (but still follow the syntax). Note that an optional item like

[ ] has a chance of being used, depending on the value of some

randomly generated number.

The program generates and outputs one random sentence every three seconds until

it is halted (for example, by typing Control-C in the terminal window where it is

running).

*/

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();

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Spatial And Temporal Databases 8th International Symposium Sstd 2003 Santorini Island Greece July 2003 Proceedings Lncs 2750

Authors: Thanasis Hadzilacos ,Yannis Manolopoulos ,John F. Roddick ,Yannis Theodoridis

2003rd Edition

3540405356, 978-3540405351

More Books

Students also viewed these Databases questions

Question

Perform the indicated operations. -2[3 (1-2) + 2] 9(-3) - (-2)

Answered: 1 week ago

Question

What is Accounting?

Answered: 1 week ago