Java how to program 10th edition

Assignment Help Basic Computer Science
Reference no: EM13677003

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<String> 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

 

Reference no: EM13677003

Questions Cloud

Transitive-closure : Transitive-Closure
The variable accounttwo being set : What is being called by line 20 of Listing 2? How is the balance value of the object referenced by the variable accountTwo being set?
Global data distribution : Global Data Distribution
Evaluate student ability to research and evaluate security : Evaluate the student  ability to research and evaluate security testing software and present a proposal for review by executive team members
Java how to program 10th edition : Java How To Program 10th Edition
Network security & how do they work together : 1. In reference to firewalls, proxies, Intrusion Prevention Systems and Intrusion Detection Systems. Why are they important for network security & how do they work together?
Discuss sampling and sampling errors : Discuss sampling and sampling errors, as well as forms of validity and reliability and why they are important to the foundations of any statistics based study.
Hiring and keeping quality employees is one : Hiring and keeping quality employees is one of the most urgent concerns for today's organizations.( )
Discuss how amazon used the four functions of management : Discuss how Amazon used the four functions of management

Reviews

Write a Review

Basic Computer Science Questions & Answers

  How to call the function to update the statistics

A function which takes as in the parameters the three counters and displays them to the screen as shown in he example below.

  Determine the number of comparisons

Determine the number of comparisons (as a function of n and m) that are performed in merging two ordered files a and b of sizes n and m, respectively, by the merge method presented in the lecture.

  How could a business use information technology

How could a business use information technology to increase switching costs and lock in its customers and suppliers? Use business examples to support your answers

  What other errors are there if any cin value

void getValue(int value&) { cout > value& } I already know "cint" should be "cin". What other errors are there, if any? Should the cin value "value&" have the ampersand attached, or not?

  What does this value indicate

The value of the Destination Address in a given Ethernet frame contains ff:ff:ff:ff:ff:ff. What does this value indicate?

  Search the internet for information on itil

Minimum number of words 500, referencing with Harvard style and the number of references not less than three Academic Articles or books.

  Explain performance measures for balanced scorecard system

Explain three specific performance measures which could be utilized in Balanced Scorecard system in industry of your choice.

  Design the static method below to count

Design the static method below to count and return the frequency of each digit in the array of strings.

  Compare clock cycle times and execution times

Instead of a single cycle orgization we use multicycle organization where each instruction takes multiple cucles but only one instruction finishes before another is fetched. in this organization.

  Write a script to help users calculate compressed file size

Develop a small console program driven by a text menu (see the sample screenshot below):? It displays a simple menu with 4 items indicated by number 1-4.? Users will enter number 1 to 4 to run some commands.?

  Differentiate between the term machine language and

question 1. distinguish between machine language and assembly language. question 2. distinguish between

  Error detection-correction facility-interpreters- compilers

Compare error detection and correction facilities of interpreters and compilers. Compare static and dynamic linking. With respect to the requirements of modern applications, what are the shortcomings of 3GLs?

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd