goal... The goal of this assignment is to practice writing your own classes, including the Keyboard class.
modify... homework #3 as follows:
Keyboard
,
which will prompt the user for and read input from the keyboard
(instead of using command-line input).
Field
,
which will handle initializing, marking and printing the field.
hw4eis2003
,
which will have a main()
method in it and
will instantiate and/or invoke the Keyboard and Field classes
and their methods. Obviously, name the class using your CUNIX id
instead of mine :-)
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:
Keyboard.read_line()
method
and converting each String input to int
;
int
form of the user's input;
Field
variable;
Field
variable as in hw#3; and
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).