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);
}

Recommended Answers

All 4 Replies

Honestly, I opened it up in Eclipse but the sheer amount of errors means it would take me a lot of chopping and changing to get it to compile.

Here's a list of things you should do to get rid of some of those errors:

  1. Wherever you reference a variable called n, change that to numbers, which is the multidimensional array you created in the constructor.
  2. Be consistent in your for loops. I notice on line 18 you declare an integer row = 0 and then start referencing an uninitiated variable i. That line should read for(int row = 0; row < numbers.length; row++) {
  3. If you are trying to get the total number of rows, you only need one for loop. If you need to access the total number of columns, you'll need nested for loops.

My advice would be, if you haven't done this already, open it up in Eclipse and go through the errors one-by-one. Eclipse gives you detailed descriptions of your errors and recommends fixes for you. Though most of your errors are from simple mistakes like referencing the wrong variables.

I did that yesterday with netBeans. I have fixed the code for ArrayOps down to line 59 The book does not give any examples on how to do the methods the assignment calls for and it is an online class. My instructor has been MIA. The methods getColumnTotal, getRowTotal, getLowestInRow, and getHighesInRow are madentory for the assignment. It is supposed to have a variable for the array and a variable for the column or row.

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; row < n.length; row++)
         {  
            for (int col = 0; col < n.length; col++)
            {
                            numbers = n;
            }
        }   
    }



    /* 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 < numbers.length; row++)
       {
            for (int col = 0; col < numbers.length; col++)
            {
                arrayTotal += numbers[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(int average)
    {
        return average = getTotal() / numbers.length;
    }

    /* The following method is to return the total for one row in the numbers 
        array as rowTotal
    */  

    public int getRowTotal()
    {
        int rowTotal = 0;
        for (int row = 0; row < numbers[row].length; row++) 
        {
           rowTotal += row; 

        }
        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 colTotal = 0; //accumulator
      for (int col = 0; col < numbers[col].length; col++)
        {
          colTotal += numbers[0][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 highest = numbers[0][0];
        for (int row = 0; row < numbers[row].length; row++)
        {
          if (numbers[row][0] >= highest)
          {
              highest = numbers[row][0];
          } 
       }
       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 lowest = numbers[0][0];  
        for (int row = 0; row < numbers[row].length; row++)
        {
            if (numbers[row][0] <= lowest)
            {
                lowest = numbers[row][0];
            }
        }
        return lowest;
    }
}

This is compiling now. All I need to do is fix the demo class. Thank you for the tip. I had just downloaded netBeans and Eclipse for an assignment last week and have not used either until now.

In the demo class, the main error I can see is ArrayOps n = new numbers(myNumbers);

This should probably be ArrayOps n = new ArrayOps(myNumbers);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.