CS1007
Fall 2001
Homework #4
due WEDNESDAY 24 October 2001

last updated: Wed Oct 17 23:04:27 EDT 2001

goal... The goal of this assignment is to practice writing your own classes, including the Keyboard class.

modify... homework #3 as follows:

Note that all the specifications for the size of the field and how to initialize it are the same as in hw#3.

1. write the Keyboard class... Create a file called Keyboard.java and write the code we develop in class on Thursday (Oct 18) to implement a cursory class for reading input from the keyboard and storing it in a String (see the lecture notes if you skipped class). Here's a skeleton for the Keyboard class:

import java.lang.*;
import java.io.*;

public class Keyboard {

  /*------------------------------------------------------------*/
  /* This method reads a line of input from the keyboard,       */
  /* stopping when a new-line character is read, and            */
  /* returning the line read as a String.                       */
  /*------------------------------------------------------------*/
  public static String read_line() {
  } // end of method read_line()

} // end of class Keyboard

2. write the Field class... Create a file called Field.java and modify the portions of your hw#3 that initialize, mark and print the field by converting each of these steps to a method inside the Field class. Here's a skeleton for the Field class:

import java.lang.*;

public class Field {

  private int rows;
  private int cols;
  private char[][] theField;

  /*------------------------------------------------------------*/
  /* This constructor for the Field class defines the number of */
  /* rows and columns in the class and instantiates and         */
  /* initializes the field.                                     */
  /* This method takes two arguments:                           */
  /*   the number of rows and columns to use for the field size */
  /*------------------------------------------------------------*/
  public Field( ... ) {
  } // end of Field constructor

  /*------------------------------------------------------------*/
  /* This method initializes the field, as defined in hw#3.     */
  /*------------------------------------------------------------*/
  public void init() {
  } // end of method init()

  /*------------------------------------------------------------*/
  /* This method takes three arguments:                         */
  /*   x and y coordinates: indicating a location in the field  */
  /*                        which should be marked              */
  /*   a character marker:  indicating the marker that should   */
  /*                        be used ('R' for robot, etc)        */
  /*------------------------------------------------------------*/
  public void mark( ... ) {
  } // end of method mark()

  /*------------------------------------------------------------*/
  /* This method should print out the field on the screen.      */
  /*------------------------------------------------------------*/
  public void print() {
   } // end of method print()

} // end of class Field

3. write the main class... This class (and its containing file) MUST be named hw4<yourCUNIXusername>.java. For example, mine would be called hw4eis2003.java.

This class will contain a main() method which will:

  1. prompt the user four times, to enter the x and y coordinates for the location of the robot and the ball, reading each user input as a String, using your Keyboard.read_line() method and converting each String input to int;
  2. echo the int form of the user's input;
  3. instantiate and initialize a Field variable;
  4. mark the Field variable as in hw#3; and
  5. print the contents of the Field variable.

4. check input values... You must check each input value entered by the user to make sure that they enter valid coordinates. If an invalid coordinate is entered, then print out an error message and loop until the user enters a valid coordinate.

5. comment your code... Your program MUST contain meaningful comments describing each class and method clearly and concisely. Each file must contain a header comment. Each method must contain a comment at the top (as shown above, elaborating where necessary).

6. submit... As usual, submit your program in two ways: electronically and hard copy.
You will submit 3 java files this time: Keyboard.java, Field.java and hw4<yourCUNIXusername>.java.
To submit all three files, make sure that all three files are located in the working directory where you invoke the submit script. The submit script should automatically pick up all three java files (and should tell you so when asking for you to confirm the submission).
The hardcopy should contain 4 sections in the following order: (1) the homework template page, (2) Keyboard.java, (3) Field.java, and (4) hw4<yourCUNIXusername>.java. You can hand in your hardcopy either by dropping it in a box in the CS department office (Mudd 450) or bringing it to class on Thu Oct 25.

Homeworks must be submitted electronically by midnight on Wed Oct 24; otherwise they will be counted as late (unless you have an approved extension).