Passing returned variables to other static methods for calculations


Passing returned variables to other static methods for calculations



Good evening all,



I have to create a program that utilizes two classes. One that will contain the main method for output and the other class will be for static methods to call to the main method. I'm getting a little better at this, but the problem now is passing returned variables in one static method to another static method to be used in calculations. Now I could be phrasing that incorrectly, if so let me know.



So far, I have created two of the four methods. The methods will input a total, the second method will consider what the commission rate will be using an if, else statement, the third method will be used to calculate the commission amount using the rate determined in the previous method, and the fourth will display the results.



The problem I get is in the first static method in the second class, it will display the question and I can enter the amount. However, the input does not transfer to the next method although I have it passing as an argument to the second method. I feel like once I understand why it is not passing through I shouldn't have a problem with composing the rest of the program. The main class is called TapCoCommission. The class with the static methods is TapCoCommissionCalc (Confusing, teacher wanted these named this way though.)



I am thinking it has something to do with either the way the return value is set up or the way the parameters have to be. Any clarification is much appreciated!


//Main class begins here
import java.util.Scanner;
import static java.lang.System.out;

public class TapCoCommission
{
public static final int COMMISSION_2 = 2;
public static final int COMMISSION_5 = 5;
public static void main(String args)
{
double inputTotalMonthlySales = 0;
double commRate = 0;

TapCoCommissionCalc.inputTotalSales(inputTotalMonthlySales);
TapCoCommissionCalc.setCommRate(inputTotalMonthlySales);


}
}




// Class with static methods begin here

import java.util.Scanner;
import static java.lang.System.out;

public class TapCoCommissionCalc
{

public static final double COMMISSION_2 = 0.02;
public static final double COMMISSION_5 = 0.05;

public static double inputTotalSales(double inputTotalMonthlySales)
{
Scanner keyboard = new Scanner(System.in);
out.println("Enter the total sales for this month.");
inputTotalMonthlySales = keyboard.nextDouble();
return inputTotalMonthlySales;
}

public static double setCommRate(double inputTotalMonthlySales)
{
double commRate = 0;

if (inputTotalMonthlySales <= 10000)
{
commRate = COMMISSION_2;
return commRate;
}

else if (inputTotalMonthlySales > 10000)
{
commRate = COMMISSION_5;
return commRate;
}

else
{
out.println("Please enter amount a positive numerical amount in dollars and cents.");

}

return commRate;
}

public static double calcCommAmount(double commRate, double inputTotalMonthlySales)
{
double commAmount = commRate * inputTotalMonthlySales;

return commAmount;
}

public static void displayCommResults()
{

}
}




3 Answers
3



You don't need to pass inputTotalMonthlySales into the first method, only the second one. In fact, inputTotalMonthlySales is the value you get out of the first method.


inputTotalMonthlySales


inputTotalMonthlySales



So you should change main to have lines like this.


main


double inputTotalMonthlySales = TapCoCommissionCalc.inputTotalSales();
double commRate = TapCoCommissionCalc.setCommRate(inputTotalMonthlySales);



And you should change inputTotalSales to look like this.


inputTotalSales


public static double inputTotalSales(){
Scanner keyboard = new Scanner(System.in);
out.println("Enter the total sales for this month.");
double inputTotalMonthlySales = keyboard.nextDouble();
return inputTotalMonthlySales;
}



The idea is that when you have a return in the method that's being called, then that's the value that's available in the calling method. You can assign that value to a variable, when you write


return


something = method call ( .... );



In the second class


public static double inputTotalSales(double inputTotalMonthlySales)
{
Scanner keyboard = new Scanner(System.in);
out.println("Enter the total sales for this month.");
inputTotalMonthlySales = keyboard.nextDouble();
return inputTotalMonthlySales;
}



The input parameter inputTotalMonthlySales is not needed. The correct way to write this method is:


inputTotalMonthlySales


public static double inputTotalSales()
{
Scanner keyboard = new Scanner(System.in);
out.println("Enter the total sales for this month.");
double inputTotalMonthlySales = keyboard.nextDouble();
return inputTotalMonthlySales;
}



inputTotalMonthlySales is a local variable within the method, which is returned to the caller.


inputTotalMonthlySales



In the first class


double inputTotalMonthlySales = 0;
....
TapCoCommissionCalc.inputTotalSales(inputTotalMonthlySales);
TapCoCommissionCalc.setCommRate(inputTotalMonthlySales);



change the last 2 lines to


inputTotalMonthlySales = TapCoCommissionCalc.inputTotalSales();
commRate = TapCoCommissionCalc.setCommRate(inputTotalMonthlySales);



This stores the value returned by the method into the local variable. This local variable is different from the one declared in the second class' method, even though it has the same name.



Also, a strong WARNING: You should never use floating point (i.e. double) to store currency values, due to the fact that floating point cannot represent all decimal values exactly, leading to errors in calculation. Currency should always use BigDecimal. For all the gory details, read What Every Computer Scientist Should Know About Floating-Point Arithmetic and
Is Floating Point Broken?


double


BigDecimal





I will have to keep that warning noted. It's odd that the book never explained that, but money is definitely one of the biggest things that need to be accurate.
– Cody Tapp
Jun 30 at 3:40


public static final int COMMISSION_2 = 2;
public static final int COMMISSION_5 = 5;
public static void main(String args)
{
double inputTotalMonthlySales = 0;
double commRate = 0;

inputTotalMonthlySales=VipCustomer.inputTotalSales();
commRate=VipCustomer.setCommRate(inputTotalMonthlySales);
System.out.println(commRate);


}



Try this in your main method. It will work fine.



You really don't have to pass the parameter inputTotalMonthlySales for the first method because you are taking user input for that value. The problem with your code is you are not storing the return value of the method in any variable and you are not even printing them. That's why you don't see any output.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV