0

I am a beginner in java and faced this error.

Write a method named pay that accepts a real number for a TA's salary and an integer for the number of hours the TA worked this week, and returns how much money to pay the TA. For example, the call pay(5.50, 6) should return 33.0.

The TA should receive "overtime" pay of 1 ½ normal salary for any hours above 8. For example, the call pay(4.00, 11) should return (4.00 * 8) + (6.00 * 3) or 50.0.

public double pay(double x,int y){
    int sum=0;
    double hours=8.0;
    if(y>hours){
       sum=(y-hours)*(1.5*x) + (hours*x);

    }
   return sum;
}

Error:

You have a mismatch between data types. This often occurs when you try to store a real number (double) into a variable or parameter that is an integer (int).
possible loss of precision
found   : double
required: int
       sum=(y-hours)*(1.5*x) + (hours*x);
                             ^
1 error
19 warnings

But the error is pointing at the + sign.What is wrong with it? It says found:double. But I want my output to be double. But it said as required int.

1
  • 1
    As an aside, when dealing with money it might be a good idea to use the BigDecimal class to avoid issues with floating-point arithmetic. See this answer. Apr 18, 2013 at 9:18

2 Answers 2

5

Since sum is an int and you're returning sum in you're method, that's why you get an error.

public double pay(double x,int y){
    double sum=0;
    double hours=8.0;
    if(y>hours){
       sum=(y-hours)*(1.5*x) + (hours*x);

    }
   return sum;
}

For the second error, if you take a look at the class Math, pi() does not exist, you have to call the static variable of the class Math, so it should be :

public double area(double x){
    x=Math.PI*Math.pow(x,2);
    return x;

}
0
0

data type of sum is int while your method expects return data type as double.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.