简体   繁体   中英

BlueJ 2d Dimensional Array

For my class assignment we need to Write a class called Matrix that contains a private 2-dimensional int array called 'matrix' that can be up to 10 rows by 10 columns maximum. We need to use two constants MAXROWS=10 and MAXCOLS=10 to construct 'matrix.' The Matrix class will also need the following attributes:

  private int rows; // number of rows to use in matrix
  private int cols; // number of cols to use in matrix

The rows and cols will contains values that are less than equal to MAXROWS and MAXCOLS. Write a default Matrix class constructor that constructs the 'matrix' array with the following values:

  {{1,2,4,5},{6,7,8,9},{10,11,12,13}, {14,15,16,17}}

The constructor must also set the rows and cols variables to match the above matrix.

Write a method 'getSumMatrix' that returns the sum of all the integers in the array 'matrix'.

Write a method 'getSumMatrixDiag' that returns the sum of all the integers in the major diagonal of the array 'matrix'. A major diagonal is the diagonal formed from the top left corner to the bottom right corner of the matrix.

Would it be possible to have my code looked over(where i initialize the array and fill it with values to ensure it is correct. The part I am stuck on is the getSumMatrix and getSumMatrixDiag. The getSumMatrix method errors out on the line "for(i=0; i < a.matrix; i++)" and tells me " cannot find symbol - variable matrix". WOuld the getSumMatrixDiag be the same way.Why is that? Thank you so much.

Here is my code

/** Sophia Ali

  1. Matrix, getSumMatrix, getSumMatrixDiag:

    Email just Matrix.java.

    Write a class called Matrix that contains a private 2-dimensional int array called 'matrix' that can be up to 10 rows by 10 columns maximum. Use two constants MAXROWS=10 and MAXCOLS=10 to construct 'matrix.'

    The Matrix class will also need the following attributes:

    private int rows; // number of rows to use in matrix private int cols; // number of cols to use in matrix

    The rows and cols will contains values that are less than equal to MAXROWS and MAXCOLS.

    Write a default Matrix class constructor that constructs the 'matrix' array with the following values:

    {{1,2,4,5},{6,7,8,9},{10,11,12,13}, {14,15,16,17}}

    The constructor must also set the rows and cols variables to match the above matrix.

    Write a method 'getSumMatrix' that returns the sum of all the integers in the array 'matrix'.

    Write a method 'getSumMatrixDiag' that returns the sum of all the integers in the major diagonal of the array 'matrix'. A major diagonal is the diagonal formed from the top left corner to the bottom right corner of the matrix.

    You do not have to write a TestMatrix class to test the Matrix class. Just use the BlueJ object creation and testing feature.

    */ public class Matrix {

     final int MAXROWS = 10; final int MAXCOLS = 10; private int [][] matrix = new int [MAXROWS][MAXCOLS]; private int rows; private int cols; public Matrix() { int matrix[][] = { {1, 2, 4, 5}, {6, 7, 8, 9}, {10, 11, 12, 13}, {14, 15, 16, 17}}; getSumMethod(matrix); getSumMatrixDiag(matrix); } public double getSumMethod(int[][] a) { int i, result; result = 0; for(i=0; i < 10; i++) { result = result + i; } return result; } public double getSumMatrixDiag(int[][] m) { int sum = 0; for (int i =0; i< m.length; i++) { sum = (int)(sum + m[i][i]); } return sum; } 

    }

EDIT:

I am using BlueJ to run my program and when I run it I get the error "Error incompatible types". I am not sure if my coding is wrong (when I compile it, it compiles with no syntax errors) or if I am using BlueJ incorrectly.

In your code scope of matrix variable is just inside the constructor. So it is not visible in getSumMethod. Also a is an array as specified above a.matrix is not correct.

Please try following. Call method getSumMethod(matrix) passing matrix as an argument and replace a.matrix with a. Then see you can move forward at least something (may be another error). If you try below then you will error related to data types which you can resolve by putting correct logic. But this code needs lot of improvement to behave the way you need it.

 final int maxrows = 10;
    final int maxcols = 10;

    private int [][] matrix = new int [maxrows][maxcols];

    private int rows;
    private int cols;


    public Base64Test() {
        int matrix[][] = 
               {{1, 2, 4, 5},
                {6, 7, 8, 9},
                {10, 11, 12, 13},
                {14, 15, 16, 17}};
        getSumMethod(matrix);
    }

    public double getSumMethod(int[][] a) {
         int i, result;
         result = 0;
         for(i=0; i < a.length; i++) {
             result = result + a[i];
         }
         return result;
     }

Sorry to all, I don't know this is the one you wana now,, but this will helps you.

public class Matrix{ 

    private int matrix[][] ;    

    public Matrix ()
    {
        int[][] matrix =  {{1, 2, 4, 5},
                {6, 7, 8, 9},
                {10, 11, 12, 13},
                {14, 15, 16, 17}};
        this.matrix=matrix;
        int final_result=0;
        for(int i=0;i<matrix.length;i++)
        {
            final_result+=getSumMethod(matrix[i]);
        }

        System.out.println("Final result is..."+final_result);

    }


     public int getSumMethod(int[] a) {
         int i, result;
         result = 0;
         for(i=0; i < a.length; i++) {
             result = result + a[i];
         }
         return result;
     }
}

...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM