Transfer an object within a class method to another class

Asked

Viewed 200 times

0

Well, I know I’m doing it in the least practical way, but I’d like to know how I transfer an instance from one class to another class. I’m creating an algorithm in which I’m going to call a method that’s in class A to take four notes, and they’re all going to be stored inside a vector, but I wanted to call another method right after class B that would divide those four notes, but I don’t even know how to start. follows the code:

MAIN class in which I will call the two methods:

public class Calculadora 
{
    public static void main(String[] args) 
    {
    CalculadoraExec.metodoNotas();      
    }
}

Class in which I will use the method to pick up the grades:

    static double metodoNotas () 
    {
        Scanner teclado = new Scanner(System.in);
        double[] notas = new double [4];
        int c2 = 1;
        for (int c=0;c<=notas.length-1; c++) 
        {
            System.out.println("NOTA "+c2);
            double n = teclado.nextDouble();
            notas[c] = n;
            c2++;
        }
        teclado.close();
        System.out.println(Arrays.toString(notas));
    return 0; 
    }
}

Now as I do to pick up the 4 typed notes and play for the Media class?

1 answer

0


To read the various entries you can follow a another thread about that. It could all be in the same class, but as you necessarily want to work with two classes you can have the first to read and the second to calculate. Having the main class that controls all that:

public class Calculadora {

    public static void main(String[] args) {
        int[] notas = A.read();
        B.divide(notas);
    }

}

class A {

    public static int[] read() {
        Scanner in = new Scanner(System.in);
        int[] vars = new int[4];

        System.out.println("Enter " + vars.length + " vars: ");

        for (int i = 0; i < vars.length; i++) {
            vars[i] = in.nextInt();
        }

        return vars;
    }
}

class B {
    public static void divide(int[] notas){
        // TODO
        System.out.println(Arrays.toString(notas));
    }
}

If you want to work instantaneously as you said in the topic, just take the statics and work with new creating instances and passing as parameter.

public class Calculadora {
    public static void main(String[] args) {      
        A a = new A();
        B b = new B(a);
        b.divide();
    }
}

class A {

    public int[] read() {
        Scanner in = new Scanner(System.in);
        int[] vars = new int[4];

        System.out.println("Enter " + vars.length + " vars: ");

        for (int i = 0; i < vars.length; i++) {
            vars[i] = in.nextInt();
        }

        return vars;
    }
}

class B {

    private A a;

    public B(A a) {
        this.a = a;
    }

    public void divide(){
        int[] notas = a.read();
        // TODO
        System.out.println(Arrays.toString(notas));
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.