CS101: Algorithms and Programming I

Assignment No.  10

March 22, 2002

 

Notes: 1. Please READ the assignment.  2. Always bring your textbook to the lab for reference purposes.  3. Study the programs before coming to the lab and try to solve them as much as possible.  Keep them in your H drive so that it would be easy to access them in the labs.  You may also keep them in diskette.  4. Your assistants may slightly change the programs as you work on them.

 

A. Purpose of Assignment

Introduction to object-oriented programming.

 

B. Description of Assignment

Program 1: Modify the Clock class

Modify the Clock class that we studied in the classroom so that now it also keeps track of the day information by including a new private member variable days of type String.  Modify the other member methods as needed.  Add the following methods to the Clock class.

 

public int equals(Clock otherClock);

public void displayMilitaryTime( );  // example: “19:32:00, Wednesday”

 

Military times means that we do not use am-pm, but use 13:00 for 1:00 pm, etc.

int ý= myClock.equals(yourClock) assigns –1 to i if the time of myClock is lower than the time of  yourClock, assigns 0 if their time is the same, and assigns +1 if the time of myClock is ahead of yourClock.  Take Sunday as the first day of week and assume that Sunday < Monday and so on.

 

Please test your program with an appropriate main(  ) method.  Make sure that days are changed appropriately and include cases to compare clocks.

 

Program 2

Define a class “Robot” as defined in the following.  The location of a robot is given by two integer coordinates: x for its east-west position and y for its north-south position (x increases as the robot moves east and y increases as the robot moves north).  The robots can face in any four directions, and it is implemented by using the following private member variable.

 

String direction;  // it can be “east”, “west”, “north” and “south”

The public interface for a robot is as follows (i.e., these are all public member methods):

public Robot( int x, int y, String d );

public Robot( ); // default constructor sets the initial coordinates as 0, 0, and the direction as “east”

public void move( int distance );

public void left_face( );

public void right_face( );

public int x_position( );

public int y_position( );

public String orientation( );

The constructor creates a robot in a given location and facing a given direction.  The method move( ) moves the robot a given distance in the direction it is facing; this is done by adding or subtracting the distance from the appropriate coordinate.  The methods left_face( ) and right_face( ) turn the robot to the left or right by 90 degrees; x_position( ), y_position( ), and orientation( ) return the robot's current location and the direction in which it is facing.  

 

Implement an interactive main( ) method to test your robot.  First get the initial conditions, after that perform the following.  (You may assume that the user will always enter correct data.)

1.      Ask the user if he/she wants to turn the robot.  If the user response is "Y" (for yes) ask where to turn.  The user response can be L (left) or R (right).

2.      Ask the user to enter the distance to be traveled.  The user response can be an integer >= 0.

3.      Display current coordinates and direction using orientation.

4.      If the last "distance to be traveled" is grater than zero go to step 1, otherwise stop. 

 

C. When and How to Submit Your Work

Make sure that your programs are all seen and approved by your assistant.  If they have different instructions regarding grading please follow their instructions.