# Thread: Possible Loss of Precision?

1. Member
Join Date
Jul 2010
Posts
20
Rep Power
0

## Possible Loss of Precision?

Hey so I'm doing an assignment relating to that Comparable and compareTo() methods. I'll just give you the prompt I got, my solution and what the error I received was:
Prompt:
Implement a new compareTo method for that class. Enter the appropriate code in the space provided below, so that employee A is considered less than employee B if the salary of employee A is less than the salary of employee B. Also, if the salary of employee A is equal to that of employee B, then they should be equal.
Method:
Java Code:
```public int compareTo(Object obj)
{
//the header was given by the professor, everything below is mine

double b = ((Employee)obj).getSalary();
double a = this.salary;
return(a-b);
}```
The error message I got was for the
Java Code:
`return(a-b);`
line and it said:
"possible loss of precision
found : double
required: int"
The methods return type seems to be defined as an Int from the header but if it is a salary it should be a double. If knowing the employee class is relevant, let me know and I'll edit.
Thanks

2. There is another underlying problem that you should address (which will probably help you solve your current). If you have two employees with the same salary, ie double a and double b, a == b still might return false. This is due to how computers handle floating point numbers. If you want to learn more look it up on wikepdia. (this is probably part of what your professor wants you to learn anyways).

The general way to handle this is to check if the two doubles are within a certain range. ie, if a is no more than .001 larger or no less than .001 smaller, then a and b are pretty much the same (in terms of salary).

When using the compareTo method, you generally want to return 0 if they are equal, -1 if the parameter is larger, and 1 if the parameter is smaller.

Mr. Beans

3. Member
Join Date
Jul 2010
Posts
20
Rep Power
0
Thanks, so should I make an if statement for that? or is the compareTo statement basically filling in for an if statement? I'm just not sure what I need to edit in my code to get the right answer

4. All of the logic I said should be contained within the compareTo method, yes. How you implement it is up to you though. That is your homework for you to do!

If you come up with somethng and it doesn't work, feel free to post back for help though.

5. Member
Join Date
Jul 2010
Posts
20
Rep Power
0
Yeah I made an attempt, the one above. I think the thing that has been tripping me up is that return statement and the fact that the header is an int but the return statement should be a double.
I was able to do 3 other problems pertaining to the compareTo method; this one is messing me up though.

6. The return statement is not supposed to be a double. It is supposed to be an int. Generally it will be either -1, 0, or 1 (depending on the situation though). Given 10 objects, you should be able to sort them via using that scheme of the compareTo method.

Create an int. Assign it the corresponding value depending on the input. Then return it.

7. Member
Join Date
Jul 2010
Posts
20
Rep Power
0
ahh thank you! that was much easier than I was making it out to me. Over analyzing I guess?

Thanks

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•