Sam B.
Sam B.

Reputation: 15

2D Array Methods & Demo

I have an assignment to design and implement methods to process 2D Arrays.

It needs to have an implementation class (Array2DMethods) that has the following static methods: readInputs() to read the number of rows and columns fro the user then reads a corresponding entry to that size. Ex: if a user enters 3 for # of rows and 3 for # of columns it'll declare an array of 10 and reads 9 entries.

max(int [][] anArray) it returns the max value in the 2D parameter array anArray

rowSum(int[][] anArray) it returns the sum of the elements in row x of anArray

columnSum(int[][] anArray) it returns the sum of the elements in column x of anArray **careful w/ rows of different lengths

isSquare(int[][] anArray) checks if the array is square (meaning every row has the same length as anArray itself)

displayOutputs(int[][] anArray) displays the 2 Dim Array elements

It also needs a testing class (Arrays2DDemo) that tests the methods.

I've commented the parts I'm having problems with. I'm not sure how to test the methods besides the readInputs method and also not sure how to format the part where you ask the user to enter a number for each row.

Here's my code so far:

import java.util.Scanner;

class Array2DMethods {

    public static int [][] readInputs(){
        Scanner keyboard = new Scanner(System.in);

        System.out.print("      How many rows? ");
        int rows = keyboard.nextInt();

        System.out.print("      How many columns? ");
        int columns = keyboard.nextInt();

        int [][] ret = new int[rows][columns];

        for (int i = 0; i<ret.length; i++) {
            for (int j = 0; j < ret[i].length; j++) {
                System.out.print("please enter an integer: "); //Need to format like Enter [0][0]: ... Enter [0][1]: ...etc. 
                ret[i][j] = keyboard.nextInt();
            }
        }

        return ret;
    }

    public static int max(int [][] anArray) {
        int ret = Integer.MIN_VALUE;

        for (int i = 0; i < anArray.length; i++) {
            for (int j = 0; j < anArray[i].length; j++) {
                if (anArray[i][j] > ret) {
                    ret = anArray[i][j];
                }
            }
        }

        return ret;
    }

    public static void rowSum(int[][]anArray) {
        int ret = 0;

        for (int i = 0; i<anArray.length; i++) {
            for (int j = 0; j < anArray[i].length; j++) {
                ret = ret + anArray[i][j];
            }
        }
    }

    public static void columnSum(int[][]anArray) {
        int ret = 0;

        for (int i = 0; i < anArray.length; i++) {
            for (int j = 0; j < anArray[i].length; j++) {
                ret = ret + anArray[i][j];
            }
        }
    }

    public static boolean isSquare(int[][]anArray) {

        for (int i = 0, l = anArray.length; i < l; i++) {
            if (anArray[i].length != l) {
                return false;
            }
        }
        return true;
    }

    public static void displayOutputs(int[][]anArray) {
        System.out.println("Here is your 2Dim Array:");
        for(int i=0; i<anArray.length; i++) {
           for(int j=0; j<anArray[i].length; j++) {
                System.out.print(anArray[i][j]);
                System.out.print(", ");
            }
            System.out.println();
        }
    }
}

Class Arrays2DDemo:

public class Arrays2DDemo {

    public static void main(String[] args){

        System.out.println("Let's create a 2Dim Array!");

        int [][] anArray = Array2DMethods.readInputs();

        Array2DMethods.max(anArray);

        Array2DMethods.rowSum(anArray);
        //need to print out and format like this: Ex Sum of row 1 = 60 ...etc
        Array2DMethods.columnSum(anArray);
        //need to print out and format like this: Ex Sum of column 1 = 60 ...etc.
        Array2DMethods.isSquare(anArray);
        //need to print out is this a square array? true
        Array2DMethods.displayOutputs(anArray);
        //need it to be formatted like [10, 20, 30] etc
    }
}

Upvotes: 0

Views: 738

Answers (1)

Mshnik
Mshnik

Reputation: 7032

Assuming you want anArray to be the array you read in during your inputting, you should name that variable, as such...

public static void main(String[] args){

    System.out.println("Let's create a 2Dim Array!");

    int[][] anArray = Array2DMethods.readInputs();

    System.out.println("max " + Array2DMethods.max(anArray)); 

    Array2DMethods.rowSum(anArray);
    Array2DMethods.columnSum(anArray);

    System.out.println("Square " + Array2DMethods.isSquare(anArray));

    Array2DMethods.displayOutputs(anArray);

}

Say you have a function f which takes a single input x. The problem is you're asking the computer to evaluate f(x) without ever telling it what x is. If you give x a value, however, such as x = 3, then asking f(x) becomes legal, because it becomes f(3), which can be evaluated.

Upvotes: 2

Related Questions