简体   繁体   中英

How to use Multiple classes and methods in Java in which each class gets values from user

In this program i created simple addition, multiplication and transpose of given matrix a & b. I use three classes, in first class i get row and columns values from user and in second class, i created 7 methods. In first method i get values for matrix a & b from user. I get error from this first method of second class. i enclosed with output what error i get earlier.

import java.util.*;

class InitialValues
{
    int row,col;
    int taa = 0, tbb = 0, sum = 0, mul = 0;

    void init()
    {

    Scanner ip1 = new Scanner (System.in);
    System.out.print("Enter Row and column size  :   ");
    row = ip1.nextInt();
    col = ip1.nextInt();
    System.out.println();

    System.out.println("Row & Column :  "+row+ " & " + col);
    }
}

class GetdValue extends InitialValues
{
    int [][] a = new int [row][col];
    int [][] b = new int [row][col];

    int i=0,j=0;
Scanner ip = new Scanner (System.in);

void getVal()
    {
    System.out.println("Enter    A    &     B  values"+" which having "+row+ " rows & " + col+" columns\n");

    for (i=0;i<row;i++)
        {
        for(j=0;j<col;j++)
            {
            a[i][j] = ip.nextInt();
            b[i][j] = ip.nextInt();
            //aa[i][j] = a [i][j];
            //bb[i][j] = b [i][j];
            }
        System.out.println("\n");
        }
    }
{
int [][] aa = a;
int [][] bb = b;
}

void displayA()
    {
    for (i=0;i<col;i++)
        {
        for(j=0;j<row;j++)
            {
            System.out.print(a[i][j]+"  " );
            }
        System.out.println("\n");
        }
    }

void displayB()
    {
    for (i=0;i<col;i++)
        {
        for(j=0;j<row;j++)
            {
            System.out.print(b[i][j]+"  " );
            }
        System.out.println("\n");
        }
    }

/*void displayAdd()
    {
    for (i=0;i<col;i++)
        {
        for(j=0;j<row;j++)
            {
            sum = a[i][j]+b[i][j];
            System.out.print(sum+"  " );
            }
        System.out.println("\n");
        }
    }

void displayMul()
    {
    for (i=0;i<col;i++)
        {
        for(j=0;j<row;j++)
            {
            mul = a[i][j]*b[i][j];
            System.out.print(mul+"  " );
            }
        System.out.println("\n");
        }
    }


/*void displayTransposeA()
    {
    for (i=0;i<col;i++)
        {
        for(j=0;j<row;j++)
            {
            taa = aa[j][i];
            System.out.print(taa+"  " );
            }
        System.out.println("\n");
        }
    }

void displayTransposeB()
    {
    for (i=0;i<col;i++)
        {
        for(j=0;j<row;j++)
            {
            tbb = bb[j][i];
            System.out.print(tbb+"  " );
            }
        System.out.println("\n");
        }
    }*/
}
class Matrixx
{
public static void main (String arg[])
    {
    //InitialValues in = new InitialValues();
    //in.init();
    System.out.println();
    GetdValue ob = new GetdValue();
    ob.init();
    ob.getVal();
    //ob.displayA();
    //ob.displayB();
    }
}

output:

Enter Row and column size  :   2 2

Row & Column :  2 & 2
Enter    A    &     B  values which having 2 rows & 2 columns

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
        at GetdValue.getVal(Matrixx.java:37)
        at Matrixx.main(Matrixx.java:136)

The row and col (defined in the super class) are zeros
at the moment when you allocate space for the a and b arrays.
So actually you create two arrays a and b of sizes [0][0].
And so you get this exception when you try accessing a[0][0].

You should give some non-zero values to row and col.

See your fixed code below.
Note the init method in GetdValue .

import java.util.*;

class InitialValues {
    int row, col;
    int taa = 0, tbb = 0, sum = 0, mul = 0;

    void init() {

        Scanner ip1 = new Scanner(System.in);
        System.out.print("Enter Row and column size  :   ");
        row = ip1.nextInt();
        col = ip1.nextInt();
        System.out.println();

        System.out.println("Row & Column :  " + row + " & " + col);
    }
}

class GetdValue extends InitialValues {

    int[][] a;
    int[][] b;
    int i = 0, j = 0;
    Scanner ip = new Scanner(System.in);

    public void init(){
        super.init();
        a = new int[row][col];
        b = new int[row][col];
    }

    void getVal() {
        System.out.println("Enter    A    &     B  values" + " which having "
                + row + " rows & " + col + " columns\n");

        for (i = 0; i < row; i++) {
            for (j = 0; j < col; j++) {
                a[i][j] = ip.nextInt();
                b[i][j] = ip.nextInt();
                // aa[i][j] = a [i][j];
                // bb[i][j] = b [i][j];
            }
            System.out.println("\n");
        }
    }

    {
        int[][] aa = a;
        int[][] bb = b;
    }

    void displayA() {
        for (i = 0; i < col; i++) {
            for (j = 0; j < row; j++) {
                System.out.print(a[i][j] + "  ");
            }
            System.out.println("\n");
        }
    }

    void displayB() {
        for (i = 0; i < col; i++) {
            for (j = 0; j < row; j++) {
                System.out.print(b[i][j] + "  ");
            }
            System.out.println("\n");
        }
    }

    /*
     * void displayAdd() { for (i=0;i<col;i++) { for(j=0;j<row;j++) { sum =
     * a[i][j]+b[i][j]; System.out.print(sum+"  " ); } System.out.println("\n");
     * } }
     * 
     * void displayMul() { for (i=0;i<col;i++) { for(j=0;j<row;j++) { mul =
     * a[i][j]*b[i][j]; System.out.print(mul+"  " ); } System.out.println("\n");
     * } }
     * 
     * 
     * /*void displayTransposeA() { for (i=0;i<col;i++) { for(j=0;j<row;j++) {
     * taa = aa[j][i]; System.out.print(taa+"  " ); } System.out.println("\n");
     * } }
     * 
     * void displayTransposeB() { for (i=0;i<col;i++) { for(j=0;j<row;j++) { tbb
     * = bb[j][i]; System.out.print(tbb+"  " ); } System.out.println("\n"); } }
     */
}

class Matrixx {
    public static void main(String arg[]) {
        // InitialValues in = new InitialValues();
        // in.init();
        System.out.println();
        GetdValue ob = new GetdValue();
        ob.init();
        ob.getVal();
        // ob.displayA();
        // ob.displayB();
    }
}

在初始化行和列之后,在InitValues类中声明a和b数组,并在您的init方法中对其进行初始化。

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