/* TwoDimArrayPractice
* Anderson, Franceschi
*/
import java.awt.*;
import javax.swing.*;
import javax.swing.JOptionPane;
import java.awt.event.*;
public class TwoDimArrayPractice extends JFrame
{
// GUI components
private JButton fillValues;
private JButton printArray;
private JButton setValues;
private JButton findMinimum;
private JButton countFrequency;
private ButtonHandler bh;
private static int [][] intArray;
private final int ROWS = 4;
private final int COLUMNS = 20;
private static int current1 = -1;
private static int current2 = -1;
private int key;
private int rowSelected = -1;
private int columnSelected = -1;
private BarChart bc;
private static int counter = 0;
private static TwoDimArrayPractice app;
private boolean firstTime = true;
private Image offscreen;
public TwoDimArrayPractice( )
{
super( "Choose your activity" );
Container c = getContentPane( );
c.setLayout( new FlowLayout( ) );
fillValues = new JButton( "Fill Values" );
c.add( fillValues );
printArray = new JButton( "Print Array" );
c.add( printArray );
setValues = new JButton( "Set Values" );
c.add( setValues );
findMinimum = new JButton( "Find Minimum" );
c.add( findMinimum );
countFrequency = new JButton( "Count Frequency" );
c.add( countFrequency );
bh = new ButtonHandler( );
fillValues.addActionListener( bh );
printArray.addActionListener( bh );
setValues.addActionListener( bh );
findMinimum.addActionListener( bh );
countFrequency.addActionListener( bh );
setSize( 500,550 );
intArray = new int[ROWS][COLUMNS];
// fill with random numbers between 50 and 80
for ( int i = 0; i < intArray.length; i++ )
{
for ( int j = 0; j < intArray[0].length; j++ )
{
intArray[i][j] = (int) ( Math.random( ) * 31 ) + 50;
}
}
bc = new BarChart( intArray );
// print the array values
System.out.println( "Element\tValue" );
for ( int i = 0; i < intArray.length; i++ )
{
System.out.print( i + "\t" );
for ( int j = 0; j < intArray[i].length; j++ )
{
System.out.print( intArray[i][j] + " " );
}
System.out.println( );
}
System.out.println( );
setVisible( true );
offscreen = this.createImage( getSize( ).width, getSize( ).height );
}
// ***** 1. This method has been coded as an example
/** Fills the array with random numbers between 50 and 80
* The instance variable named intArray is the integer array to be
* filled with values
*/
public void fillValues( )
{
for ( int row = 0; row < intArray.length; row++ )
{
System.out.print( row + "\t" );
for ( int column = 0; column < intArray[row].length; column++ )
{
intArray[row][column] = (int) ( Math.random( ) * 31 ) + 50;
animate( row, column ); // needed to create visual feedback
}
System.out.println( );
}
}
// end of fillValues method
// ***** 2. Student writes this method
/** Prints array to the console, elements are separated by a space
* The instance variable named intArray is the integer array to be printed
*/
public void printArray( )
{
// Note: To animate the algorithm, put this method call as the
// last element in your inner for loop
// animate( row, column );
// where row is the index of the array's current row
// and column is the index of the array's current column
// Write your code here:
}
// end of printArray method
// ***** 3. Student writes this method
/** Sets all the elements in the specified row to the specified value
* The instance variable named intArray is the integer array
* @param value the value to assign to the element of the row
* @param row the row in which to set the elements to value
*/
public void setValues( int value, int row )
{
// Note: To animate the algorithm, put this method call as the
// last element in your for loop
// animate( row, column );
// where row is the index of the array's current row
// and column is the index of the array's current column
// Write your code here:
}
// end of setValues method
// ***** 4. Student writes this method
/** Finds minimum value in the specified column
* The instance variable named intArray is the integer array
* @param column the column to search
* @return the minimum value found in the column
*/
public int findMinimum( int column )
{
// Note: To animate the algorithm, put this method call as the
// last element in your for loop
// animate( row, column, minimum );
// where row is the index of the array's current row,
// column is the index of the array's current column
// minimum is the local variable storing the current minimum
// Write your code here:
return 0; // replace this line with your return statement
}
// end of findMinimumn method
// ***** 5. Student writes this method
/** Finds the number of times value is found in the array
* The instance variable named intArray is the integer array
* @param value the value to count
* @return the number of times value was found
*/
public int countFound( int value )
{
// Note: To animate the algorithm, put this method call as the
// last element in your inner for loop
// animate( row, column, num );
// where row is the index of the array's current row,
// column is the index of the array's current column, and
// num is the local variable storing the current frequency count
// Write your code here:
return 0; // replace this line with your return statement
}
// end of countFound method
public void startActivity( int act )
{
bc.setActivity( act );
boolean goodInput = false;
String answer = "";
switch( act )
{
case( 0 ): fillValues( );
JOptionPane.showMessageDialog( null, "Array filled with new values" );
break;
case( 1 ): printArray( );
JOptionPane.showMessageDialog( null, "Array printed" );
break;
case( 2 ):
while ( !goodInput || key < 50 || key > 80 )
{
try
{
answer = JOptionPane.showInputDialog( null, "Enter a value between 50 and 80" );
if ( answer != null )
{
key = Integer.parseInt( answer );
goodInput = true;
}
else
{
goodInput = false;
break;
}
}
catch( Exception e )
{}
}
if ( goodInput )
{
goodInput = false;
while ( !goodInput || rowSelected < 0 || rowSelected > 3 )
{
try
{
answer = JOptionPane.showInputDialog( null, "Enter a row number between 0 and 3" );
if ( answer != null )
{
rowSelected = Integer.parseInt( answer );
goodInput = true;
}
else
{
goodInput = false;
break;
}
}
catch( Exception e )
{}
}
}
if ( goodInput )
{
bc.setKey ( key );
setValues( key, rowSelected );
String message = "";
if ( bc.getCheckNewValues( ) )
message = " correctly";
else
message = " incorrectly";
JOptionPane.showMess
评论0