Java how to program 10th


Java How To Program 10th Edition:
(Turtle Graphics) The Logo language made the concept of turtle graphics famous. Imagine a mechanical turtle that walks around the room under the control of a Java application. The holds a pen in one of two positions, up or down. When the pen is down, the turtle traces out shapes as it moves, and while the pen is up, the turtle moves about freely without writing anything. In this problem, you'll simulate the operation of the turtle and create a computerized sketchpad.
Use a 20-by-20 array floor that's initialized to zero. Read commands from the array that contains them. Keep track of the current position of the turtle at all times and whether the pen is currently up or down. Assume that the turtle always starts at position (0, 0) of the floor with its pen up. The set of turtle commands your application processes are shown in the figure below:
Command Meaning
1 Pen Up
2 Pen Down
3 Turn right
4 Turn left
5, 10 Move forward 10 spaces (replace 10 for a different number of spaces)
6 Display the 20-by-20 array
9 End of data (sentinel)


Special instructions from Instructor:
Please do Exercise 7.21 (Turtle Graphics), as modified by the instructions below.
This is a more challenging and longer exercise than the previous exercises.
Take an early look at what this exercise requires, and be prepared to ask questions while there is still time to get answers. Note that this program is an command-line style program, not a GUI program.
Please submit at least the following file for the assignment (together with any additional Java source files you find necessary):

  • TurtleGraphics.java, which implements the TurtleGraphics class with just the two methods as described below.

The two methods of the TurtleGraphics class
A "turtle program" is a sequence of commands that directs the movements and actions of a "mechanical turtle" as described in the the Exercise 7.21. The definition of the commands is given in the Figure 7.29 in the textbook.
The enterCommands method acts as the compiler of a turtle program. Each call to this method enters a new program. The signature and return value of the method is:
public List enterCommands(Scanner in)
The "program" will be passed to the enterCommands method as the Scanner parameter. When passed to the enterCommands method, the method should analyze the the values delivered by the Scanner to see that they are only recognizable and valid commands, and the program is terminated properly. Generate informative or, in particular, error messages for any problem recognized in the program (see the examples in the "About the Assignment" topic). The enterCommands method should transform the program into a more convenient representation, i.e., more convenient than having to re-analyze the commands read from the Scanner, and at the same time store this more convenient representation in an internal structure ready for execution.
The program that was entered may be "executed," by calling the executeCommands method. The program may be executed as often an desired, until a new program is entered. The signature and return value of the method is:
public boolean executeCommands()
This method does not need any parameters, as it will execute the program previously processed and stored by the enterCommands method. Executing the program actually does the work to move the turtle over the "floor," and as it moves, if its pen is down, it makes a mark, otherwise it just continues moving and leaves whatever is already on the floor undisturbed and unchanged. The executeCommands returns a value of false if there is no valid program to execute.
Assume that the turtle drawing is in a box that has a floor 20 units by 20 units in size (as mentioned in the description in the textbook). You yourself may specify what you want the behavior to be when the turtle gets to the edge of the box (since this is not mentioned in the problem description-your choice-but you must write some commentary to say what you have decided the turtle will do when reaching a boundary, so it may be determined whether you have done it correctly).
When a "display" ("6") instruction is found in the program, the current state of the floor is rendered to the standard output. ("*" is suggested in the textbook as a character to represent a mark on the floor, but two characters such as "* " works better.)
Format of the Turtle Program
Spaces, end-of-line, blank lines have no significance in a turtle program (just like in a Java program) other than to separate the numbers that make up the commands and distances. A single comma immediately after a number is like a single space. A comma may be replaced by a single space, and the program will be unchanged, and will produce exactly the same drawing. The single comma is allowed in a turtle program just for visual effect, intended to make it easier to read the program when a command has a related move value. It does not have to be there, so need not be checked. All of the five following programs (that draw the box shown in the textbook) are equivalent.
2 5,12 3 5,12 3 5,12 3 5,12 1 6 9
2 5 12 3 5 12 3 5 12 3 5,12 1 6 9
2 5 12 3 5 12 3 5 12 3 5 12 1 6 9
2, 5,12 3, 5,12 3, 5,12 3, 5,12 1, 6, 9,
2, 5 12 3, 5, 12, 3 5, 12, 3 5 12 1 6 9
Initial conditions
When the turtle starts executing a program, it is at the point 0,0 (top left-hand corner) of the floor. When the turtle moves, it moves in the direction it is currently facing. The initial direction is in the positive x direction, that is, to the east. The pen is initially up, so that is makes no mark.
Test Program
The supplied test program tests drawing samples additional to just the textbook example.
For more information about test program supplied, see the topic ?About the Assignment.? If you wish, you may write additional test programs and add them to the list of programs. You will note that the test program has some input that is intended to be incorrect, in order to assess that the TurtleGraphics class deals with such problems.
This is my code so far, how do I change this per the Instructor's instructions.












//Header file section
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TurtleGraphics extends JApplet implements ActionListener
{
final int MAXCOMMANDS = 100;
final int SIZE = 20;
JLabel prompt;
JTextField input;
JTexeArea output;
int floor[][];
int commandArray[][];
int command, distnce, direction, count, xPos, yPos;
boolean penDown;

// set up GUI components and initialize instance
// variables
@Override
public void init()
{
prompt = new JLabel( "Enter command (from 1 to 9):");

input = new JTextField ( 4 );
input.addActionListener( this);

// set up JTextArea for displaying turtle
// graphics
output = new JTextArea (25,40);
Container con = getContentPane();
con.setLayout( new FlowLayout() );
con.add( prompt );
con.add( input );
con.add( output );

// initialize values
direction = 0;
count = 0;
xPos = 0;
yPos = 0;
penDown = false;
floor = new int[ SIZE ] [ SIZE ];
commandArry = new int[ MAXCOMMANDS ] [ 2 ];
} // end method init

// perform appoiate action
public void actionPerformed( ActionEvent actionEvent )
{
// input field
if ( actionEvent.getSource() == input )
{
int inputCommand = Integer.parseInt( input.getText() );
input.setText( "" );

// if reach max commands
if ( count < MAXCOMMANDS )
{
commandArray[ count ][ 0 ] = inputCommand;

// prompt for forward spaces
if ( inputCommand == 5 )
{
int spaces = Integer.praseInt(JoptionPane.showInputDialog(
"Enter forward spaces"));
commandArray[ count ][ 1 ] = spaces;
}
}

count++;

// excute commands if command is 9
if ( inputCommand == 9 || count == MAXCOMMANDS ) executeCommands();

}// end outer if

}//end method ctionPerformed

public void executeCommands()
{
int commandNumber = 0;
command = commandArray[ commandNumber ] [ 10 ];

// continue executing commands until either reach
// the end of reach the max commands
while ( command != 9 && commandNumber < MAXCOMMANDS )
{

// determine what command was entered
// and perform desired action
switch ( command )
{
case 1:
penDown = false;
break;
case 2:
penDown = true;
break;
case 3:
direction = turnRight( direction );
break;
case 4:
direction = turnLeft( direction );
break;
case 5:
distance = commandArray[ commandNumber ][ 1 ];
movePen( penDown, floor, direction, distance );
break;
case 6:
output.append( "nThe drawing is:nn");
printArray( floor );
break;
} // end switch

command = commandArray[ ++commandNumber ] [ 0 ];

} // end while

} // end method executeCommands

// method to turn turtle to the left
public int turnLeft( int d )
{
return ++d > 3 ? 3 : d;
}

// method to move the pen
public void movePen(
boolean down, int a[][], int dir, int dist )
{
int j; // looping variable

// determine which way to move pen
switch ( dir )
{

case 0: // move to right
for(j = 1; j <= dist && yPos +j< SIZE; ++j)
if ( down )
a[ xPos ][ yPos + j ] = 1;

yPos += j - 1;
break;

case 1: // move down
for ( j = 1; j <= dist && xPos + j < SIZE; ++j )

if ( down )
a[ xPos + j ][ yPos ] = 1;
xPos += j - 1;
break;

case 2: // move to left
for ( j = 1; j <= dist && yPos - j >= 0; ++j )
if ( down )
a[ xPos ][ yPos - j ] = 1;
yPos -= j - 1;
break;

case 3: // move up
for ( j = 1; j <= dist && xPos - j >= 0; ++j )
if ( down )
a[ xPos - j ][ yPos ] = 1;
xPos -= j - 1;
break;

} // end switch

} // end method movePen

// method to print array drawing
public void printArray( int a[][] )
{

// display array
for ( int i = 0; i < SIZE; ++i )
{
for ( int j = 0; j < SIZE; ++j )
output.append( ( a[ i ][ j ]==1? "*": " "));

output.append( "n" );
}
}

private static class JTexeArea {

public JTexeArea() {
}
}

} // end class TurtleGraphics

 

Request for Solution File

Ask an Expert for Answer!!
Basic Computer Science: Java how to program 10th
Reference No:- TGS0930582

Expected delivery within 24 Hours