Can someone please help me out with this JAVA program!?
-
(Turtle Graphics) The Logo language, which is popular among young computer users, made the concept of turtle graphics famous. Imagine a mechanical turtle that walks around the room under the control of a Java program. The turtle holds a pen in one of two positions, up or down. While the pen is down, the turtle traces out shapes as it moves; while the pen is up, the turtle moves about freely without writing anything. In this problem you will simulate the operation of the turtle and create a computerized sketchpad as well. Use a 20-by-20 array floor which is initialized to zeros. Read commands from an 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 program must process are as follows: Command Meaning 1 Pen up 2 Pen down 3 Turn right 4 Turn left 5,10 Move forward 10 spaces (or a number other than 10) 6 Print the 20-by-20 array 9 End of data (sentinel) Suppose that the turtle is somewhere near the center of the floor. The following "program" would draw and print a 12-by-12 square leaving the pen in the up position: 2 5,12 3 5,12 3 5,12 3 5,12 1 6 9 As the turtle moves with the pen down, set the appropriate elements of array floor to 1s. When the 6 command (print) is given, wherever there is a 1 in the array, display an asterisk or some other character you choose. Wherever there is a zero, display a blank. Write a program to implement the turtle graphics capabilities discussed here. Write several turtle graphics programs to draw interesting shapes. Add other commands to increase the power of your turtle graphics language.
-
Answer:
Break the problem up. 1. Start by writing a class that reads the commands and converts them to something you understand. (This is how I'd implement it. I'd create a CommandReader and a Command class. The CommandReader would have several methods. read() which would return one Command at a time and hasMore(). The Command would have a main commend (aka pen up, pen down) and an optional argument (aka distance). You can use a series of Constants to help you with identifying each command.) 2. Write another class that displays the graphics. This class has the array inside and would have a few methods associated with it like paint (aka print) and drawLine. In this case, paint would do a bunch of System.out that would display the grid. DrawLine would accept four arguments x1,y1, x2, y2. This would give a starting point in the array and an end point in the array. Just implement the problem for when either x varies or y varies. If you really want extra credit, then you can have x and y vary using the formula for a line with slope of rise over run. 3. Write another class that keeps track of state. This is essentially your main program, but the other classes are more important. public class Turtle { public static final int PEN_UP = 1; public static final int PEN_DOWN = 2; public static final int TURN_RIGHT = 3; public static final int TURN_LEFT = 4; public static final int MOVE = 5; public static final int PRINT = 6; public static final int END_OF_DATA = 9; private Graphic g = new Graphic(20,20); private int currentLocationX = 0; private int currentLocationY = 0; private boolean penDown = false; private int direction = 0; public static void main(String[] args) { CommandReader reader = new CommandReader("myFileName"); while(reader.hasMore()) { Command command = reader.read(); int instruction = command.getCommand(); switch(instruction) { case PEN_UP: penDown = false; break; case PEN_DOWN: penDown = true; break; case TURN_RIGHT: //update direction break; case TURN_LEFT: // update direction break; case MOVE: int newLocationX = ;//This needs to get updated based on direction int newLocationY = ; if(!penUp) { graphic.drawLine(currentLocationX, currentLocationY, newLocationX, newLocationY); } currentLocationX = newLocationX; currentLocationY = newLocationY; break; case PRINT: graphic.paint(); case END_OF_DATA: reader.close(); break; default: System.out.println(instruction + " is not supported"); break; } // You should probably add a method that prints out the state to help you debug. } } }
java at Yahoo! Answers Visit the source
Related Q & A:
- Can someone please help me? Nursing?Best solution by Yahoo! Answers
- Can someone please help me with my iPod?Best solution by Yahoo! Answers
- Can someone please help me to make a hotmail.fr address?Best solution by Yahoo! Answers
- What is Computer Science? Can someone please explain to me?Best solution by seas.gwu.edu
- Can someone please help me with my Spanish homework?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.