简体   繁体   中英

How to pass the value of variables from main class to another class?

Hello everyone I'm currently creating a simple program. I have 2 classes the first one is calculator and the second one is parameters_return. What I want to happen is that when I read the values of x and y in my second class i want to use them in my first class Unfortunately, I can't run the program because it has an error. The code is below please help me with regards to this matter. I'm just self studying I really want to learn Java.

Code in (first class) calculator class is:

class calculator {
    //with parameters with return type
    int add(int a, int b) {
        return (a + b);
    }

    //with parameters without return type
    void sub(int a, int b) {
        System.out.print(a - b);
    }

    //without parameters with return type
    int mul() {
        parameters_return s1 = new parameters_return();
        int c = (s1.x) * (s1.y);
        return c;
    }

    //without parameters without return type
    void div() {
        parameters_return s2 = new parameters_return();
        int c = (s2.x) / (s2.y);
        System.out.println("Division = " + c);
    }
}

Code in my (second class) parameters_return class is:

class parameters_return {
    int x, y;
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        x = sc.nextInt();
        y = sc.nextInt();
        calculator perform = new calculator();

        //addition
        int z = perform.add(x, y);
        System.out.println("Added value = " + z);

        //subtraction
        System.out.println("Subtracted value = ");
        perform.sub(x, y);

        //multiplication
        z = perform.mul();
        System.out.println("Multiplication value = ");

        //division
        perform.div();
    }
}

Is there any way to get values from main class and can be used in another class?

import java.util.*;
class ParametersReturn {
    static int x, y;
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        x = sc.nextInt();
        y = sc.nextInt();
        Calculator perform = new Calculator();

        //addition
        int z = perform.add(x, y);
        System.out.println("Added value = " + z);

        //subtraction
        System.out.println("Subtracted value = ");
        perform.sub(x, y);

        //multiplication
        z = perform.mul();
        System.out.println("Multiplication value = " + z);

        //division
        perform.div();
    }
}

This should be your ParametersReturn Class and make sure you should start your class name with capital letter, you are using Scanner class to use it you have to import java.util package. And to use these variables in Calculator class make these variables static

class Calculator {
    //with parameters with return type
    int add(int a, int b) {
        return (a + b);
    }

    //with parameters without return type
    void sub(int a, int b) {
        System.out.print(a - b);
    }

    //without parameters with return type
    int mul() {
        ParametersReturn s1 = new ParametersReturn();
        int c = (s1.x) * (s1.y);
        return c;
    }

    //without parameters without return type
    void div() {
        ParametersReturn s2 = new ParametersReturn();
        int c = (s2.x) / (s2.y);
        System.out.println("Division = " + c);
    }
}

And in multiplication you forgot to print the value of z

Hello everyone I'm currently creating a simple program. I have 2 classes the first one is calculator and the second one is parameters_return. What I want to happen is that when I read the values of x and y in my second class i want to use them in my first class Unfortunately, I can't run the program because it has an error. The code is below please help me with regards to this matter. I'm just self studying I really want to learn Java.

Code in (first class) calculator class is:

class calculator {
    //with parameters with return type
    int add(int a, int b) {
        return (a + b);
    }

    //with parameters without return type
    void sub(int a, int b) {
        System.out.print(a - b);
    }

    //without parameters with return type
    int mul() {
        parameters_return s1 = new parameters_return();
        int c = (s1.x) * (s1.y);
        return c;
    }

    //without parameters without return type
    void div() {
        parameters_return s2 = new parameters_return();
        int c = (s2.x) / (s2.y);
        System.out.println("Division = " + c);
    }
}

Code in my (second class) parameters_return class is:

class parameters_return {
    int x, y;
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        x = sc.nextInt();
        y = sc.nextInt();
        calculator perform = new calculator();

        //addition
        int z = perform.add(x, y);
        System.out.println("Added value = " + z);

        //subtraction
        System.out.println("Subtracted value = ");
        perform.sub(x, y);

        //multiplication
        z = perform.mul();
        System.out.println("Multiplication value = ");

        //division
        perform.div();
    }
}

Is there any way to get values from main class and can be used in another class?

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