user2179615
user2179615

Reputation: 127

loss of precision-->Required double but compiler required int?

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.

Upvotes: 0

Views: 1917

Answers (2)

Alexis C.
Alexis C.

Reputation: 93842

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;

}

Upvotes: 5

Alpesh Gediya
Alpesh Gediya

Reputation: 3794

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

Upvotes: 0

Related Questions