Charles Bell
Posts: 519
Nickname: charles
Registered: Feb, 2002
|
|
Re: A little help getting started!!
|
Posted: Mar 5, 2002 8:02 AM
|
|
/* ArrayTester2D.java
*/
import java.io.*;
/** ArrayTester2D
*/
public class ArrayTester2D{
private static final int ROWS = 10;
private static final int COLUMNS = 14;
// instance variable
boolean[][] array;
/** Constructor which initializes the array of booleans
* so that every array element is true.
*/
public ArrayTester2D(){
array = new boolean[ArrayTester2D.ROWS ][ArrayTester2D.COLUMNS ];
for (int i = 0;i<ArrayTester2D.ROWS ;i++){
for (int j = 0;j< ArrayTester2D.COLUMNS ;j++){
array[i][j] = true;
}
}
}
/**
*/
public static void main(String[] args){
ArrayTester2D arraytester2d = new ArrayTester2D();
arraytester2d.init();
}
/** Runs the program and iterates in a while loop unitl the use enters in
* a quit command.
*/
public void init(){
boolean done = false;
while (!done){
int row = -1;
int column = -1;
printArray();
printRowStatus();
printColumnStatus();
String message = "Enter a Row Number between 1 and " + String.valueOf(ArrayTester2D.ROWS) + " or QUIT to exit.";
promptUser(message);
String inputstring = getUserInput();
done = (inputstring.compareToIgnoreCase("Quit") == 0);
if ((getInt(inputstring) > 0) && (getInt(inputstring) < ArrayTester2D.ROWS )){
row = getInt(inputstring) - 1;//array rows start at 0
message = "Enter a Column Number between 1 and " + String.valueOf(ArrayTester2D.COLUMNS) + " or QUIT to exit.";
promptUser(message);
inputstring = getUserInput();
if ((getInt(inputstring) > 0) && (getInt(inputstring) < ArrayTester2D.COLUMNS)){
column = getInt(inputstring) - 1;
setCellOff(row,column);
}
}
}
//now exit
System.exit(0);
}
/** printArray prints the contents of the two dimensional array, row by row.
*/
public void printArray(){
for (int i = 0;i<ArrayTester2D.ROWS ;i++){
for (int j = 0;j< ArrayTester2D.COLUMNS ;j++){
if (array[i][j]){
System.out.print("T "); //each column in the row
}else{
System.out.print("F "); //each column in the row
}
}
System.out.print("\n"); //end of row
}
}
/** prints the status of all rows.
*/
public void printRowStatus(){
for (int i = 0; i<ArrayTester2D.ROWS ;i++){
if (isRowFalse(i)) System.out.println("Row " + String.valueOf(i) + " is all false.");
}
}
/** prints the status of all columns.
*/
public void printColumnStatus(){
for (int j = 0; j<ArrayTester2D.ROWS ;j++){
if (isColumnFalse(j)) System.out.println("Column " + String.valueOf(j) + " is all false.");
}
}
/** Sets a single cell at (i,j) to false.
*/
public void setCellOff(int i, int j){
array[i][j] = false;
}
/** Sets a single cell at (i,j) to true.
*/
public void setCellOn(int i, int j){
array[i][j] = true;
}
/** Checks if all elements of row i are false.
*/
public boolean isRowFalse(int i){
boolean status = true;
for (int j = 0;j< ArrayTester2D.COLUMNS ;j++){
status = (status && (!array[i][j]));
}
return status;
}
/** Checks if all elements of column j are false.
*/
public boolean isColumnFalse(int j){
boolean status = true;
for (int i = 0;i<ArrayTester2D.ROWS ;i++){
status = (status && (!array[i][j]));
}
return status;
}
/** Reads a line of input from the user.
*/
public String getUserInput(){
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String lineread = "";
try{
lineread = br.readLine();
}catch(IOException ioe){
System.err.println("IOException: " + ioe.getMessage());
}
return lineread;
}
/** Displays message string to user.
*/
public void promptUser(String message){
System.out.println(message);
}
/** Converts the string to an integer.
* Returns -1 if not an integer.
*/
public int getInt(String s){
int n = -1;
try{
n = Integer.parseInt(s);
}catch(NumberFormatException nfe){
if (s.compareToIgnoreCase("Quit") == 0) System.exit(0);
System.err.println("NumberFormatException: " + nfe.getMessage());
}
return n;
}
}
|
|