Results 1 to 7 of 7
Thread: Possible Loss of Precision?
- 12-02-2010, 04:51 AM #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); }
Java Code:return(a-b);
"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
- 12-02-2010, 05:02 AM #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
- 12-02-2010, 05:04 AM #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
- 12-02-2010, 05:13 AM #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.
- 12-02-2010, 05:20 AM #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.
- 12-02-2010, 05:25 AM #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.
- 12-02-2010, 05:28 AM #7
Member
- Join Date
- Jul 2010
- Posts
- 20
- Rep Power
- 0
Similar Threads
-
Possible loss of precision
By jankidudel in forum New To JavaReplies: 2Last Post: 08-07-2010, 11:15 AM -
possible loss of precision?
By gandalf5166 in forum New To JavaReplies: 6Last Post: 04-13-2010, 09:21 PM -
Precision
By c_walker in forum New To JavaReplies: 1Last Post: 10-18-2009, 12:36 PM -
help me- loss of precision error??
By j2vdk in forum New To JavaReplies: 6Last Post: 09-01-2008, 10:23 AM -
Connection Loss
By CrazyShells Slam in forum New To JavaReplies: 0Last Post: 05-15-2008, 03:56 PM
Bookmarks