Method Problem using Double
hello java masters,
I'm having problems with understanding how method work and how does it being pulled. I don't want the answer to my hw but I wish you guys could give me some clues and better understanding to my confusion. Just recently did method with boolean expression, i did that one fine, method had return with either true or false.
What I can't understand is how would you apply method when you are dealing with double. Basic program asks user to input their annual income and program will calculate their taxes. (done)
COPY OF GOOD PROGRAM WITHOUT METHOD:
Code:
import java.util.Scanner;
public class state_income_tax
{
public static void main(String [] args)
{
//prep
Scanner kybd = new Scanner(System.in);
final double LIMIT = 20000.00;
final double REG_RATE = 0.02;
final double HIGH_RATE = 0.025;
final double FIXED = 400.00;
double userInput;
double tax;
System.out.print("Please enter your income amount: ");
userInput = kybd.nextInt();
System.out.println("");//spacing
//calculation
if(userInput<=LIMIT)
{
tax = REG_RATE*userInput;
System.out.print("You owe to state of New Jersey $" + tax);
}
else
{
tax = HIGH_RATE*userInput+FIXED;
System.out.print("You owe to state of New Jersey $" + tax);
}
}
}
NOW, when I'm trying to add method getting bunch of errors(12)
Code:
import java.util.Scanner;
public class state_income_tax_wMethod
{
public static void main(String [] args)
{
//prep
Scanner kybd = new Scanner(System.in);
final double LIMIT = 20000.00;
final double REG_RATE = 0.02;
final double HIGH_RATE = 0.025;
final double FIXED = 400.00;
double userInput;
double tax;
double income; //added for method
System.out.print("Please enter your income amount: ");
userInput = kybd.nextInt();
income = calcStateTax(LIMIT, REG_RATE, HIGH_RATE, FIXED, userInput, tax);
System.out.println("");//spacing
//calculation
System.out.print("You owe to state of New Jersey $" + tax);
}//end of main
public static double calcStateTax(double income)
{
if(userInput<=LIMIT)
{
tax = REG_RATE*userInput; // I know this is wrong, if I would use boolean I could use return true OR false. [B]I DON'T know how to apply method when using double[/B]
}
else
{
tax = HIGH_RATE*userInput+FIXED;
}
}//end of method
}
Any help on the method portion is greatly appreciated!
Bart
Re: method problem *double
So I thought about it. Method needs return so this is what I have(method):
Code:
System.out.print("You owe to state of New Jersey $" + tax);
}//end of main
public static double calcStateTax(double income)
{
if(userInput<=LIMIT)
{
tax = REG_RATE*userInput;
}
else
{
tax = HIGH_RATE*userInput+FIXED;
}
return tax;
But I'm still getting error code saying that this method cannot be applied:
state_income_tax_wMethod.java:24: error: method calcStateTax in class state_income_tax_wMethod cannot be applied to given types;
income = calcStateTax(LIMIT, REG_RATE, HIGH_RATE, FIXED, userInput, tax);
^
required: double
found: double,double,double,double,double,double
reason: actual and formal argument lists differ in length
Re: method problem *double
reason: actual and formal argument lists differ in length
This is how you call the method:
Code:
calcStateTax(LIMIT, REG_RATE, HIGH_RATE, FIXED, userInput, tax);
and this is the declaration: Code:
public static double calcStateTax(double income)
Count the arguments and compare types.