Reputation: 37
When i compile this it keep giving the error "This method must return a result of type double", but i believe my pay is double. What went wrong?..................................
public double getNetPay (int totalHoursWorked)
{
int basePayRate = 15;
double overTime = 15*1.5;
double taxRate1 = 0.25;
double taxRate2 = 0.21;
double taxRate3 = 0.15;
if (totalHoursWorked > 40)
{
double pay =totalHoursWorked*overTime;
if (pay > 1200)
{
return pay*taxRate1;
}
if (pay >=500||pay <1199)
{
return pay*taxRate2;
}
if (pay <=499)
{
return pay*taxRate3;
}
}
else
{
double pay =totalHoursWorked*basePayRate;
if (pay > 1200)
{
return pay*taxRate1;
}
if (pay >=500||pay <1199)
{
return pay*taxRate2;
}
if (pay <=499)
{
return pay*taxRate3;
}
}
}
Upvotes: 1
Views: 196
Reputation: 10543
Use eclipse IDE which helps to find out compile time error.
return
statement always should be outside of any block. Avoid multiple return
statement in one method instead create a variable and return that variable at the end of the method.
Upvotes: 0
Reputation: 22710
You need to add a return statement outside the conditional blocks.
As conditional blocks are not guaranteed to execute always, Java needs to make sure that method always return something as promised.
You can do something like
double defaultTaxRate = 0.33; // put some desired value
if (totalHoursWorked > 40)
{
// All conditional blocks
}
return pay*defaultTaxRate;
Upvotes: 4
Reputation: 8323
A good practice in imperative language is too minimize number of return statements in a method.
public int get() {
int result = default_value;
if(condition) {
result = one_integer_value;
} else if (another_condition) {
result = another_integer_value;
}
return result;
}
It makes the code much clearer and avoid this kind of problem. Anyway there are some situation where it's not fully appropriated. Take this as an advice not as a strict rule, the idea itself makes debate : Should a function have only one return statement?
Upvotes: 1
Reputation: 14877
There is nothing wrong with your return type. Its double
only. The way you have created if
conditions are wrong. Here there are chances that your any of the if condition not get satisfy and return statement is not executed. thus Compiler is giving error that you This method must return a result of type double
public double getNetPay(int totalHoursWorked) {
int basePayRate = 15;
double overTime = 15 * 1.5;
double taxRate1 = 0.25;
double taxRate2 = 0.21;
double taxRate3 = 0.15;
double result = 0d;
if (totalHoursWorked > 40) {
double pay = totalHoursWorked * overTime;
if (pay > 1200) {
result = pay * taxRate1;
} else if (pay >= 500 || pay < 1199) {
result = pay * taxRate2;
} else if (pay <= 499) {
result = pay * taxRate3;
}
} else {
double pay = totalHoursWorked * basePayRate;
if (pay > 1200) {
result = pay * taxRate1;
} else if (pay >= 500 || pay < 1199) {
result = pay * taxRate2;
} else if (pay <= 499) {
result = pay * taxRate3;
}
}
return result;
}
Upvotes: 1
Reputation: 18489
Of course your pay is double, but you are returning it from inside a if condition.Java Compiler will give error because if that condition is false then it will not execute that return statement,So you have to see clearly that in any condition something(which is double here) must return.for example:
public int get() {
if(condition) {
return one_integer_value;
} else {
return another_integer_value;
}
Upvotes: 1