The Artima Developer Community
Sponsored Link

Java Answers Forum
A little help getting started!!

4 replies on 1 page. Most recent reply: Mar 5, 2002 6:24 PM by Hoody

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Hoody

Posts: 33
Nickname: hoodlum
Registered: Feb, 2002

A little help getting started!! Posted: Mar 4, 2002 5:22 PM
Reply to this message Reply
Advertisement
I always have problems getting started but once i do i am alright. I gotta make an array of booleans I am using BlueJ but i can use textpad. Since i started using bluej i am using the simpleinput class that was created to get input from user. I just dont know how to set up the array of booleans so it prints it out in rows and columns? Create a new project called ArrayTester2D. In it create a class "ArrayTester2D."

? Create a two-dimensional array (10 row, 14 columns) of booleans. Initialize the array so that every array element is true. Create this array as an instance variable and initialize it in the constructor.

? Write a method that printouts the contents of the two dimensional array, row by row.

? Then write a method that lets you set a single cell to false. The method should take the row/column numbers from the user. Test this method by setting cells and then printing the array.

? Then write a method that tells you which rows and which columns are false. A column or a row is said to be false if applying an AND operation to all entries in it results in false.

this is all i could start out with and dont know if its on the right path...

public class ArrayTester2D
{
// instance variables - replace the example below with your own
private SimpleInput input;
private int[][] array;
private static final int ROWS = 10;
private static final int COLUMNS = 14;

/**
* Constructor for objects of class ArrayTester2D
*/
public ArrayTester2D()
{
// initialise instance variables
input = new SimpleInput();

int[][] array= new int[ROWS][] ;

for (int row =0; row < ROWS; row++)
{
for (int col = 0; col < COLUMNS; col++)
{
array[row] = 10;
array[col] = 14;
}

}
}

/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public void printTable()
{

printTable(array, COLUMNS);
}
/**
* print an array
*/
private void printTable(int[][]table, int array)
{
for (int i = 0; i < array; i++)
{
for (int j = 0; j < array; j++)
{
System.out.print(array);

}
System.out.println();
}
}
}


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: A little help getting started!! Posted: Mar 5, 2002 8:02 AM
Reply to this message Reply
/*	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;
	}
}

Hoody

Posts: 33
Nickname: hoodlum
Registered: Feb, 2002

Re: A little help getting started!! Posted: Mar 5, 2002 8:14 AM
Reply to this message Reply
damn you didnt have to do all that i just wanted a little help getting started with it but thanx

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: A little help getting started!! Posted: Mar 5, 2002 3:46 PM
Reply to this message Reply
Hey Hoody,

Just make yours yours

Hoody

Posts: 33
Nickname: hoodlum
Registered: Feb, 2002

Re: A little help getting started!! Posted: Mar 5, 2002 6:24 PM
Reply to this message Reply
i did im looking at that and helping me to make it into bluej form now...thanx for the help i got a good idea how to do it now

Flat View: This topic has 4 replies on 1 page
Topic: pgm image Previous Topic   Next Topic Topic: Stars Applet

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use