Difference between revisions of "CISC181 S2017 Lab3"
(→Instructions) |
(→Instructions) |
||
(3 intermediate revisions by the same user not shown) | |||
Line 10: | Line 10: | ||
In this lab you will write a simple "Turtle" simulator. Imagine a turtle as existing in a 2-D grid with rectangular borders as shown below: | In this lab you will write a simple "Turtle" simulator. Imagine a turtle as existing in a 2-D grid with rectangular borders as shown below: | ||
− | [[Image:Turtle_grid.png|300px]]<br> | + | [[Image:300px-Turtle_grid.png|300px]]<br> |
There are several facts about a turtle: | There are several facts about a turtle: | ||
Line 18: | Line 18: | ||
* It can change its position according to a ''movement pattern'' <tt>(forward, lateral)</tt>. These must be integers, but each of them can be positive, negative, or zero. <tt>forward</tt> determines how many squares ahead the turtle would move ''in the direction it is currently facing'', and <tt>lateral</tt> determines how many squares to the left (a negative value) or right (positive) the turtle would move ''relative to the direction it is facing''. NO TURNING OCCURS. Also, the turtle cannot make any movement that would land '''outside''' the grid boundaries . Both turtles below have a movement pattern of <tt>(2, -1)</tt>; the ''blue'' turtle has moved once from its initial position, and the ''red'' turtle has turned once before moving once. | * It can change its position according to a ''movement pattern'' <tt>(forward, lateral)</tt>. These must be integers, but each of them can be positive, negative, or zero. <tt>forward</tt> determines how many squares ahead the turtle would move ''in the direction it is currently facing'', and <tt>lateral</tt> determines how many squares to the left (a negative value) or right (positive) the turtle would move ''relative to the direction it is facing''. NO TURNING OCCURS. Also, the turtle cannot make any movement that would land '''outside''' the grid boundaries . Both turtles below have a movement pattern of <tt>(2, -1)</tt>; the ''blue'' turtle has moved once from its initial position, and the ''red'' turtle has turned once before moving once. | ||
− | [[Image:Turtle_grid_after_move.png|300px]]<br> | + | [[Image:300px-Turtle_grid_after_move.png|300px]]<br> |
Write a class <tt>Turtle</tt> which stores all relevant information in appropriately named and typed fields, and implements the above behaviors. To that end, define an enumerated type <tt>Direction</tt> for the compass directions '''outside of the Turtle and Lab3 classes''', and set the grid dimensions with static constants <tt>NUM_ROWS</tt>, <tt>NUM_COLS</tt> (both equal to 10) '''inside the Turtle class'''. All fields should be <tt>private</tt>. Your class does not need to be <tt>public</tt>, so please keep it inside <tt>Lab3.java</tt> file, but define it ''above'' the <tt>Lab3</tt> class. | Write a class <tt>Turtle</tt> which stores all relevant information in appropriately named and typed fields, and implements the above behaviors. To that end, define an enumerated type <tt>Direction</tt> for the compass directions '''outside of the Turtle and Lab3 classes''', and set the grid dimensions with static constants <tt>NUM_ROWS</tt>, <tt>NUM_COLS</tt> (both equal to 10) '''inside the Turtle class'''. All fields should be <tt>private</tt>. Your class does not need to be <tt>public</tt>, so please keep it inside <tt>Lab3.java</tt> file, but define it ''above'' the <tt>Lab3</tt> class. | ||
Line 27: | Line 27: | ||
* A 3-argument constructor which sets the turtle's initial position and direction. The default movement pattern is used | * A 3-argument constructor which sets the turtle's initial position and direction. The default movement pattern is used | ||
* A 5-argument constructor which is like the 3-argument constructor, but adds the ability to set the movement pattern | * A 5-argument constructor which is like the 3-argument constructor, but adds the ability to set the movement pattern | ||
− | * <tt>void turn(int n)</tt>: The turtle executes <tt>n</tt> counter-clockwise 90 degree turns | + | * <tt>void turn(int n)</tt>: The turtle executes <tt>n</tt> counter-clockwise 90 degree turns. A negative number of turns is ignored. |
− | * <tt>void move(int n)</tt>: The turtle executes its current movement pattern <tt>n</tt> times. Any attempted movement that would land outside the grid is ignored | + | * <tt>void move(int n)</tt>: The turtle executes its current movement pattern <tt>n</tt> times. Any attempted movement that would land outside the grid is ignored (but all moves before it happen). A negative number of moves is also ignored. |
* <tt>void flip(boolean doForward, boolean doLateral)</tt>: Negate forward and/or lateral component of movement pattern. This changes the movement pattern for all future moves until <tt>flip()</tt> is called again | * <tt>void flip(boolean doForward, boolean doLateral)</tt>: Negate forward and/or lateral component of movement pattern. This changes the movement pattern for all future moves until <tt>flip()</tt> is called again | ||
Line 45: | Line 45: | ||
Test the correctness of your implementation in <tt>main()</tt> by constructing <tt>Turtle</tt> objects with different constructors and arguments, calling their methods in different sequences, then checking whether the final position, orientation, distance traveled, and distance home are correct. | Test the correctness of your implementation in <tt>main()</tt> by constructing <tt>Turtle</tt> objects with different constructors and arguments, calling their methods in different sequences, then checking whether the final position, orientation, distance traveled, and distance home are correct. | ||
− | I have created a helper class for testing called [http://nameless.cis.udel.edu/class_data/ | + | I have created a helper class for testing called [http://nameless.cis.udel.edu/class_data/181_s2017/TurtleTester.java TurtleTester.java]. Download it and add it to your project by putting it in the same folder as <tt>Lab3.java</tt>. '''Instructions for and examples of using it are [[CISC181_S2017_TurtleTester | here]].''' The TA will grade your submission using the examples on that page. |
Latest revision as of 10:21, 27 February 2017
Preliminaries
- Make a new project with n = 3 (following these instructions)
- Name your main class "Lab3" (when creating a new module in the instructions above, in the Java class name field)
- Modify Lab3.java by adding your name in a comment before the Lab3 class body.
Instructions
In this lab you will write a simple "Turtle" simulator. Imagine a turtle as existing in a 2-D grid with rectangular borders as shown below:
There are several facts about a turtle:
- It has an integer grid position (row, column) (where (0, 0) is the upper-left corner of the rectangle). The turtle above is at (2, 3)
- It faces in a compass direction: NORTH, SOUTH, EAST, or WEST. The turtle above is facing NORTH.
- It can turn, but only to the left (counter-clockwise), and 90 degrees at a time. If the turtle above turned once, it would be facing WEST.
- It can change its position according to a movement pattern (forward, lateral). These must be integers, but each of them can be positive, negative, or zero. forward determines how many squares ahead the turtle would move in the direction it is currently facing, and lateral determines how many squares to the left (a negative value) or right (positive) the turtle would move relative to the direction it is facing. NO TURNING OCCURS. Also, the turtle cannot make any movement that would land outside the grid boundaries . Both turtles below have a movement pattern of (2, -1); the blue turtle has moved once from its initial position, and the red turtle has turned once before moving once.
Write a class Turtle which stores all relevant information in appropriately named and typed fields, and implements the above behaviors. To that end, define an enumerated type Direction for the compass directions outside of the Turtle and Lab3 classes, and set the grid dimensions with static constants NUM_ROWS, NUM_COLS (both equal to 10) inside the Turtle class. All fields should be private. Your class does not need to be public, so please keep it inside Lab3.java file, but define it above the Lab3 class.
Your class must have the following public methods that set or change the state of the turtle:
- The default (no-argument) constructor should place the turtle initially at (NUM_ROWS / 2, NUM_COLS / 2), facing NORTH, with movement pattern (1, 0)
- A 3-argument constructor which sets the turtle's initial position and direction. The default movement pattern is used
- A 5-argument constructor which is like the 3-argument constructor, but adds the ability to set the movement pattern
- void turn(int n): The turtle executes n counter-clockwise 90 degree turns. A negative number of turns is ignored.
- void move(int n): The turtle executes its current movement pattern n times. Any attempted movement that would land outside the grid is ignored (but all moves before it happen). A negative number of moves is also ignored.
- void flip(boolean doForward, boolean doLateral): Negate forward and/or lateral component of movement pattern. This changes the movement pattern for all future moves until flip() is called again
The class must also have the following public accessor and informational methods:
- int getRow(), int getColumn(), Direction getDirection()
- int squaresTraveled(): How many squares has the turtle moved forward and laterally since it was constructed?
- double distanceToHome(): Euclidean distance from the turtle's current position to its initial position
You may add any "helper" private methods that you need.
Just to be clear, there is no drawing by you in this lab, and no need for arrays. The pictures above are just for illustration
Testing
Test the correctness of your implementation in main() by constructing Turtle objects with different constructors and arguments, calling their methods in different sequences, then checking whether the final position, orientation, distance traveled, and distance home are correct.
I have created a helper class for testing called TurtleTester.java. Download it and add it to your project by putting it in the same folder as Lab3.java. Instructions for and examples of using it are here. The TA will grade your submission using the examples on that page.