Hello, I am working on an assignment for my Java class and I just can't figure out the last two methods. I was hoping someone here can point me in the right direction, as searching for an answer online hasn't been helpful.
I am supposed to build a class that will fill an array and use a GUI to display different function sin the array, such as returning true if the number 7 is included. The last two methods are to return the sum of a column and a row, which column or row is not specified. I am at a loss on how to do this frankly, so hopefully I can get some help.
//Program Name: StatsArray.java
//Author: Joshua Rowley
//Class: CSC110AA
//Date: 17Oct11
//Description: This program returns information based on a multiple dimension array.
// Needs to be able to return sum, average, minimum, maximum, search for a specific number and generate a new array.
import java.awt.*;
import java.util.Random; //for our random number generator
public class MultiStatsArray
{
private int rowSize; // how many rows
private int columnSize; // how many columns
private int[][] stats; // an array of integers
MultiStatsArray()
{
rowSize = 10;
columnSize = 5;
stats = new int[rowSize][columnSize] ; //instantiate the array called stats
}
public void display(Graphics g)
{
int x = 50; //coordinates for displaying
int y = 40; // displaying the array
for(int row = 0; row < stats.length; row++)
{
for (int column = 0; column < stats[row].length; column++)
{
g.drawString("" + stats[row][column], x + column * 25, (y + 15 * row));
}
}
}
public void fillArray()
{
Random random = new Random();
for(int row = 0; row < 10; row++)
{
for(int column = 0; column < 5; column++)
{
stats[row][column] = random.nextInt(100);
}
}
}
public int getSum() //add up all the values in the array
{
int sum = 0;
Random random = new Random();
for(int row = 0; row < 10; row++)
{
for(int column = 0; column < 5; column++)
{
sum += stats[row][column];
}
}
return sum;
}
public int getMax() //return the maximum value in the array
{
int maximum = stats[9][4];
for(int row = 0; row < 10; row++)
{
for(int column = 0; column < 5; column++)
{
if(maximum < stats[row][column])
{
maximum = stats[row][column];
}
}
}
return maximum;
}
public int getMin() //return the minimum value in the array
{
int minimum = stats[9][4];
for(int row = 0; row < 10; row++)
{
for(int column = 0; column < 5; column++)
{
if(minimum > stats[row][column])
{
minimum = stats[row][column];
}
}
}
return minimum;
}
public double getAverage()
{
return (double) this.getSum() / (rowSize * columnSize);
}
public int countValues(int lowRange, int highRange) //count how many numbers are >= lowRange and <= highRange
{
int count = 0;
for(int row = 0; row < 10; row++)
{
for(int column = 0; column < 5; column++)
{
if((stats[row][column] >= lowRange) && (stats[row][column] <= highRange))
count++;
}
}
return count;
}
public boolean isValueFound(int someNumber)
{
for(int row = 0; row < 10; row++)
{
for(int column = 0; column < 5; column++)
{
if (stats[row][column] == someNumber)
{
return true;
}
}
}
return false;
}
public int getRowSum(int row)
{
return 0;
}
public int getColumnSum(int column)
{
return 0;
}
}
------------------------------------------------------------------------------------------------------------------------------------------
/* MultiStatsArrayGUI
This is the GUI tester/controller for the MultiStatsArray class.
The MultiStatsArray is used to hold 50 grades.
Patricia Baker
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MultiStatsArrayGUI extends JFrame implements ActionListener
{
//set up buttons to control the MultiStatsArray
private JButton maxButton;
private JButton minButton;
private JButton sumButton;
private JButton avgButton;
private JButton aButton;
private JButton luckyButton;
private JButton startOverButton;
private JButton rangeButton;
private JButton specificColButton;
private JButton specificRowButton;
private JPanel panel;
/*instantiate your MultiStatsArray object called grades
creates array of size 10 rows and 5 columns */
private MultiStatsArray grades = new MultiStatsArray( );
public static void main(String[] args)
{
MultiStatsArrayGUI demo = new MultiStatsArrayGUI();
demo.setSize(400,400);
demo.createGUI();
demo.setVisible(true);
}
private void createGUI()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new FlowLayout());
panel = new JPanel();
panel.setPreferredSize(new Dimension(300, 200));
panel.setBackground(Color.white);
window.add(panel);
//buttons
startOverButton = new JButton("New Array");
window.add(startOverButton);
startOverButton.addActionListener(this);
maxButton = new JButton("Maximum");
window.add(maxButton);
maxButton.addActionListener(this);
minButton = new JButton("Minimum");
window.add(minButton);
minButton.addActionListener(this);
sumButton = new JButton("Sum");
window.add(sumButton);
sumButton.addActionListener(this);
avgButton = new JButton("Average");
window.add(avgButton);
avgButton.addActionListener(this);
aButton = new JButton("Number of A's");
window.add(aButton);
aButton.addActionListener(this);
luckyButton = new JButton("Lucky 7");
window.add(luckyButton);
luckyButton.addActionListener(this);
rangeButton = new JButton("Range Test");
window.add(rangeButton);
rangeButton.addActionListener(this);
specificColButton = new JButton("2nd column");
window.add(specificColButton);
specificColButton.addActionListener(this);
specificRowButton = new JButton("3rd row");
window.add(specificRowButton);
specificRowButton.addActionListener(this);
/* invokes method to fill array with random numbers between 1 and 100 */
grades.fillArray();
}
public void actionPerformed(ActionEvent event)
{
Graphics g = panel.getGraphics();
Object source = event.getSource();
/*cover up old display before drawing the new values. */
g.setColor(Color.white);
g.fillRect(0, 0, 300, 200);
g.setColor(Color.black);
if (source == minButton)
g.drawString("Minimum is: " + grades.getMin(), 50, 20);
if (source == maxButton)
g.drawString("Maximum is: " + grades.getMax(), 50, 20);
if (source == sumButton)
g.drawString("Sum is: " + grades.getSum(), 50, 20);
if (source == avgButton)
g.drawString("Average is: " + grades.getAverage(), 50, 20);
if (source == aButton)
g.drawString("Number of A's = " + grades.countValues(90, 100), 50, 20);
if (source == luckyButton)
g.drawString("Lucky 7 Y or N: " + ( ( grades.isValueFound( 7 ) )? 'Y' : 'N' ), 50, 20);
if (source == specificColButton)
g.drawString("The total of the 2nd column is: " + grades.getColumnSum(1), 50, 20);
if (source == specificRowButton)
g.drawString("The total of the 3rd row is: " + grades.getRowSum(2), 50, 20);
if (source == startOverButton)
{
grades.fillArray();
g.drawString("New Values" , 50, 20);
}
if (source == rangeButton)
{
g.drawString("Number of Values in Range 25 - 50: " + grades.countValues(25,50), 50, 20);
}
grades.display(g);
}
}