Home > Software engineering >  How to send variable to method through an arraylist
How to send variable to method through an arraylist

Time:12-06

I'm not very good at explaining things but can someone try to explain to me why my user input variables are not being sent to my methods in another class?

This is my main class:

import java.util.Scanner; //import scanner class for user input


public static void main(String[] args) 
{
    Scanner input = new Scanner(System.in);//declare scanner object called input for user input
    char operation = '\u0000';
    double num1 = 0;
    double num2 = 0;
    
    Calculator allMath[] = new Calculator[4];
    allMath[0] = new Addition(num1, num2, operation);
    allMath[1] = new Subtraction(num1, num2, operation);
    allMath[2] = new Multiplication(num1, num2, operation);
    allMath[3] = new Division(num1, num2, operation);
    
    System.out.println("This calculator can perform basic mathematic functions such as "
                  "addition, subtraction, multiplication, and division. "
              "\nOnce you are finished using the calculator please enter a ! character.");
   
    System.out.println("Please enter in which operation you would like to perform. "
              "Enter   for addition, - for subtraction, * for multiplication "
              "and / for division.");
    operation = input.next().charAt(0);
    
    while (operation != '!')
    {

    System.out.println("Please enter the first number for your calculation.");
    num1 = input.nextDouble();
    
    System.out.println("Please enter the second number for your calculation.");
    num2 = input.nextDouble();
    
    switch (operation)  
   {
    case ' ':
        allMath[0].calculate();
        break;
    case '-':
        allMath[1].calculate();;
        break;
    case '*':
        allMath[2].calculate();
        break;
    case '/':
        allMath[3].calculate();
        break;
    default:
        
    }
    
    
}
}

}

This is an example of one of my subclasses that I am trying to send the variables to. All the other ones are the same with respect to the difference in calculation.

public class Addition extends Calculator
{ 

   public double num1; 

   public double num2;

   public char operation;

   
   public Addition (double num1, double num2, char operation)
   {

       this.num1 = num1;

       this.num2 = num2;

       this.operation = operation; 

   }
   @Override

   public void calculate()
   {

       System.out.println((num1)  "   "   (num2)   " is "   (num1   num2)); 

   }
   
}

My superclass is just an abstract class with an abstract method per my school project guiedlines. I am taking an OOP class that is focusing on the concepts of OOP through java so I only know the basics of java. I am confused on what else I need to do in order for my method to receive the user input numbers.

CodePudding user response:

When you initialise your Addition, Subtraction, Multiplication and Division classes, num1 and num2 are 0. In Java, variables are passed by value therefore in all the subclasses they have the value of 0. The fact that you change num1 and num2 later in the code doesn't change the variables in the subclasses stored in the allMath array.

Solution: Move your allMath array creation below when you change the values of num1 and num2.

  • Related