# cs3101-003 Java spring 2003 homework #2 due Wed Feb 5, by 6AM -- electronic submission

goal.
The goals of this assignment are to:

1. learn how to use Java native classes
2. practice using loops and switch statements

program.
As with the first assignment...
A robot and a treasure are located somewhere in a room (which is empty except for the robot and the treasure). The floor of the room is covered with square tiles, so you can think of the room as having a giant piece of graph paper covering its floor. The robot and the treasure each occupy a different tile in this grid, and you know the location of each one. The locations are specified using (x,y) coordinates.
For this assignment...
Assume that the x- and y-coordinates of the room range from -5 to 5 (it's a small room). Your job is to write a program that will first randomly choose locations for the robot and the treasure in the room. The user may (optionally) enter a seed for the random number generator on the command line.
Your program must then determine and provide directions to the robot for how to travel to the treasure.
You can assume that the robot begins by facing in the positive Y direction.
Your robot can only move ONE tile at a time, so you need to loop, continually updating the robot's position and giving it new instructions, until it reaches the treasure.

So, your program has to do the following:

1. Initialize the random number generator. First check if the user entered a random number seed and if so, use that seed; otherwise use the current time to seed the random number generator.
2. Echo the random seed.
3. Pick random locations for the robot and the treasure within the room.
4. Echo the locations of the robot and the treasure.
5. Loop, incrementally determining and outputting directions for the robot to travel to the treasure, until the robot has found the treasure.

You can assume that the locations are whole numbers. Your calculated outputs should be whole numbers in terms of the number of tiles to travel in each direction.

Hint: keep track of which direction the robot is facing, so that you don't turn too many (or not enough) times! (The robot can only face North, South, East or West; it starts by facing North.)

NOTE.
In order to do this assignment, you will need to use the `java.util.Random` and `java.util.Date` classes discussed in class this week. I don't think I pointed out in class that you need to include a kind of statement called `import` at the top of a class file that uses classes from any class other than `java.lang` (which is imported by default).
See the example on page 20 of the lecture slides from Wednesday's class. The statement
`import java.util.*;`
is located above the `public class ex2h` statement, which is what you'll have to do for this assignment.

sample run.
Below is a sample run for two cases. The unix command line is highlighted in bold font.

```unix\$ java hw2sklar
random number seed = 1044058435726
robot is at (-5,5)
treasure is at (-3,0)
turn around, go forward 1 tile
go forward 1 tile
go forward 1 tile
go forward 1 tile
go forward 1 tile
turn left, go forward 1 tile
go forward 1 tile
gotcha!!
unix\$ java hw2sklar 3
random number seed = 3
robot is at (0,0)
treasure is at (1,-2)
turn around, go forward 1 tile
go forward 1 tile
turn left, go forward 1 tile
gotcha!!
unix\$
```

source code.
Your source code (i.e., your `.java` file) must be neat and clearly commented. You must have a header comment and you should comment the end of each block (i.e., each `}`).

submission.

For example, mine would be called hw2sklar.java.
BE SURE TO NAME YOUR FILE USING ALL lower case LETTERS!!!

Follow the submission instructions for hw1, making sure you log into clic.cs.columbia.edu to run the submit script.
Also make sure that you use 2 as the argument to the submit script (instead of 1), since this is homework #2.

points.
This assignment is worth 15 points (out of 100 for the semester). Distribution of points is:

• 1 point for following submission instructions
• 1 point for commenting source code
• 1 point for handling an optional command-line argument (random seed)
• 1 point for getting current time to (if seed not entered on command-line)
• 1 point for echoing the seed
• 1 point for creating a random number object
• 2 points for randomly choosing locations for robot and treasure
• 1 point for echoing the robot and treasure locations
• 1 point for stopping loop when robot finds treasure
• 5 points for correctly determining and outputting robot travelling instructions