last updated: Thu Feb 7 10:31:07 EST 2002

Spring 2002
Homework #2
due Thu Feb 14, by 6AM -- electronic submission

hardcopy due IN CLASS: 11.05AM cs1007-002; 2.45PM cs1007-001

The goals of this assignment are to:

  1. practice using comments in your code;
  2. learn how to use command-line input;
  3. learn how to convert values from String to int;
  4. practice using mathematical operators; and
  5. learn how to use if-else statements.

Imagine a maze with a robot in it. The maze is arranged in a square grid of infinite size in both directions (although you can assume that an int is large enough to store the largest coordinate in our grid).
As an example, here's a picture of a 5x5 square grid maze. The robot is indicated by [] and the gold coin is indicated by ().

|  |  |  |  |  |
|  |  |  |  |()|
|  |  |  |  |  |
|  |[]|  |  |  |
|  |  |  |  |  |
In addition to your robot, there is also a gold coin in the maze. The object of the game is for the robot to find the gold coin. In this illustration, the robot would travel 3 steps to the left and 2 steps up to reach the gold coin.

You have to write a program that will take as input from the command line two pairs of coordinates: the location of the robot and the location of the gold coin. These must be whole numbers.

Your program has to do the following:

  1. echo the input;
  2. output the number of steps (i.e., squares) that the robot has to travel horizontally and vertically to reach the gold coin;
  3. specify the orientation for the robot to travel both horizontally (i.e., left or right) and vertically (i.e., up or down); and
  4. calculate and output which direction travelled (horizontal or vertical) is greater.

You can assume that the grid squares are numbered like a Cartesian (X-Y) graph, with positive X going horizontally to the right and positive Y going vertically upwards. Input coordinates can be negative, but they must be whole numbers. The output distance must be positive and must be expressed in whole numbers.

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

unix$ java hw2eis2003 8 9 1 7
robot is at position (8,9)
coin is at position (1,7)
robot travels 7 steps to the left
robot travels 2 steps down
robot travels a greater distance horizontally than vertically

unix$ java hw2eis2003 8 9 8 1
robot is at position (8,9)
coin is at position (8,1)
robot does not travel horizontally
robot travels 8 steps down
robot travels a greater distance vertically than horizontally

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

Follow the submission instructions carefully!!! If you don't, human intervention will be required to fix your mistakes, and you will lose 1 point.

  1. Name your file properly.
    Your program file MUST be named hw2<yourCUNIXusername>.java
    For example, mine would be called

  2. Submit your program electronically.
    Follow instructions on the assignments page.

  3. Turn in a harcopy of your program during THE FIRST 5 MINUTES OF class.
    Print out the homework template cover page ( and fill it out.
    Staple the cover page to a hardcopy (print-out) of your program (the Java source code, e.g., the file named Make sure your name is on all the papers you hand in!
    Bring the cover page and hardcopy to class on the day the assignment is due and deposit it in the homework box at the front of the classroom within the first 5 minutes of the class:
    cs1007-002: 11.05am
    cs1007-001: 2.45pm

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