I have been working on this for a while and keep getting error after error. It seams like every time I fix something, I get an error for something else. This is the main class.
import java.util.Scanner; //for scanner class
public class ArrayOps
{
private int[][] numbers; //Holds a 2D array
/*This is an overloaded constructor that will set the numbers
field
@param n The value to store in numbers.
*/
public ArrayOps(int n[][])
{
// Create a new array.
n = new int[4][3];
// Copy the argument's elements to the
// new array.
for (int row = 0; i < n.length; i++)
{
for (int col = 0; j < n.length; j++)
{
numbers[i][j] = n[i][j];
}
}
}
/* The following methed is to return the sum of the numbers in the array
to the program as the arrayTotal variable
*/
public int getTotal()
{
int arrayTotal = 0;
for (int row = 0; row < n.length; row++)
{
for (int col = 0; col < n[row].length; col++)
{
total += n[row][col];
}
}
return arrayTotal;
}
/* The following method is to return the average of the numbers int the
array as the arrayAverage variable
*/
public int getAverage()
{
return average = getTotal() / n.length;
}
/* The following method is to return the total for one row in the numbers
array as rowTotal
*/
public int getRowTotal()
{
for (int row = 0; row < n.length; row++)
{
rowTotal = 0; //row total accumulator
for (int col = 0; col < n[row].length; col++)
{
rowTotal += n[row][col];
}
}
return rowTotal; //returns row total to program
}
/* The following method is to return the total for one column in the
numbers array as colTotal
*/
public int getColumnTotal(int n[][])
{
int colTotal = 0; //accumulator
for (int row = 0; row < n.length; row++)
{
colTotal += n[row][col];
}
return colTotal;
}
/* The following method is to return the highest number in a given row
in the numbers arrat as rowHighest
*/
public int getHighestInRow(int n[][])
{
int highest = n[0][0];
for (int col = 0; col < n[row].length; col++)
{
if (n[row][col] >= highest)
{
highest = n[row][col];
}
}
return highest;
}
/* The following method is to return the lowest number in a given row
in the numbers arrat as rowLowest
*/
public int getLowestInRow(int n[][])
{
int lowest = n[0][0];
for (int col = 0; col < n[row].length; col++)
{
if (n[row][col] <= lowest)
{
lowest = n[row][col];
}
}
return lowest;
}
}
This is the demo class. I just need to know where it is wrong and how to fix it.
import java.util.Scanner; //for scanner class
/* This program assigns value to the array and uses those
values to test the methods in ArrayOps class
*/
public class ArrayOpsDemo
{
/*The following variables and for loop should
allow the user to input numbers into a 4 by 3
2d array.
*/
Scanner keyboard = new Scanner(System.in);
final int ROWS = 4;
final int COLS = 3;
int[][] myNumbers = new int[ROWS][COLS];
for (int row = 0; row < ROWS; row++)
{
for (int col = 0; col < COLS; col++)
{
System.out.print("Enter a number: ");
myNumbers[row][col] = keyboard.nextInt();
}
}
ArrayOps n = new numbers(myNumbers);
/*The following variables will use the methods from
the ArrayOps class and display output.
*/
int total, average, rowTotal, colTotal, highRow, lowRow;
total = n.getTotal();
average = n.getAverage();
rowTotal = n.getRowTotal();
colTotal = n.getColumnTotal();
highRow = n.getHighestInRow();
lowRow = n.getLowestInRow();
//Display output
System.println("The total of numbers entered is " + total);
System.println("The average of numbers entered is " + average);
System.println("The total for row " + row + " is " + rowTotal);
System.println("The total for column " + col + " is " + colTotal);
System.println("The highest number in row " + row + " is " + highRow);
System.println("The lowest number in row " + row + " is " + lowRow);
}