How do I add two variables that are in two different methods in Java?

65 Views Asked by At

I need to make a program that uses two different methods. The first method adds a + b + c = d whilst the second adds x + y = z. Then it has to print out d and z as well as the sum of d + z. The program has to take input from the user.

I know that you can't call variables outside of their methods, so is there any workaround to this? It has to follow the instructions I previously stated

I'm missing the part where I add d and z, this is my current program:

import java.util.Scanner;

public class myProgram {
    
    static void methodAB(){
        
        Scanner myScan = new Scanner(System.in);
        System.out.println("Enter A: ");
        float A = myScan.nextFloat();
        System.out.println("Enter B: ");
        float B = myScan.nextFloat();
        System.out.println("Enter C: ");
        float C = myScan.nextFloat();
        
    float D = A + B + C;
    System.out.println("Sum of MethodAB: " + D);
    
    }
     static void methodXY(){
         
        Scanner myScan = new Scanner(System.in);
        System.out.println("Enter X: ");
        float X = myScan.nextFloat();
        System.out.println("Enter Y: ");
        float Y = myScan.nextFloat();  

    float Z = X + Y;
    System.out.println("Sum of MethodXY: " + Z);
    }


    public static void main(String[] args) {
    methodAB();
    methodXY();
    //this is the part im stuck on
    //i cant call variables d and z so i cant add them together

    }
}
3

There are 3 best solutions below

0
ishaan On BEST ANSWER

just return them and then add

public class MyProgram {
    
    static float methodAB() {
        Scanner myScan = new Scanner(System.in);
        System.out.println("Enter A: ");
        float A = myScan.nextFloat();
        System.out.println("Enter B: ");
        float B = myScan.nextFloat();
        System.out.println("Enter C: ");
        float C = myScan.nextFloat();
        
        float D = A + B + C;
        System.out.println("Sum of MethodAB: " + D);
        return D;
    }
    
    static float methodXY() {
        Scanner myScan = new Scanner(System.in);
        System.out.println("Enter X: ");
        float X = myScan.nextFloat();
        System.out.println("Enter Y: ");
        float Y = myScan.nextFloat();  

        float Z = X + Y;
        System.out.println("Sum of MethodXY: " + Z);
        return Z;
    }

    public static void main(String[] args) {
        float dResult = methodAB();
        float zResult = methodXY();

        float sumResult = dResult + zResult;
        
        System.out.println("Sum of D and Z: " + sumResult);
    }
}
0
Chaosfire On

There are few options:

  1. Have AB return D and XY return Z and work with those results.
static void methodAB(){
  //do stuff
  return d;
}
public static void main(String[] args) {
    float res = methodAB() + methodXY();
}
  1. Use class/instance variables
public class MyClass {

  private float d;
  private float z;

  void methodAB(){
    //do stuff
    float D = A + B + C;
    this.d = D;
  }
  void methodXY(){
    //do stuff
    float Z = X + Y;
    this.z = Z;
  }

  void sum() {
    System.out.println("sum - " + (this.d + this.z));
  }
  
  //or define getters for d and z and use them

  public static void main(String[] args) {
    MyClass myClass = new MyClass();
    myClass.methodAB();
    myClass.methodXY();
    myClass.sum();
  }
}
0
Swapnil Sharma On

You can also use static instance variable sum to store the sum of D and Z.

import java.util.Scanner;

public class StackAnswer {
    static float sum=0;
    static void methodAB(){

        Scanner myScan = new Scanner(System.in);
        System.out.println("Enter A: ");
        float A = myScan.nextFloat();
        System.out.println("Enter B: ");
        float B = myScan.nextFloat();
        System.out.println("Enter C: ");
        float C = myScan.nextFloat();

        float D = A + B + C;
        sum = sum + D;
        System.out.println("Sum of MethodAB: " + D);

    }
    static void methodXY(){

        Scanner myScan = new Scanner(System.in);
        System.out.println("Enter X: ");
        float X = myScan.nextFloat();
        System.out.println("Enter Y: ");
        float Y = myScan.nextFloat();

        float Z = X + Y;
        sum = sum + Z;
        System.out.println("Sum of MethodXY: " + Z);
    }


    public static void main(String[] args) {
        methodAB();
        methodXY();
        System.out.println("sum of D and Z is "+sum);
    }
}