Results 1 to 7 of 7
  1. #1
    jwb4291 is offline Member
    Join Date
    Jul 2010
    Posts
    20
    Rep Power
    0

    Default 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. #2
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    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. #3
    jwb4291 is offline Member
    Join Date
    Jul 2010
    Posts
    20
    Rep Power
    0

    Default

    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. #4
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    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. #5
    jwb4291 is offline Member
    Join Date
    Jul 2010
    Posts
    20
    Rep Power
    0

    Default

    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. #6
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    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. #7
    jwb4291 is offline Member
    Join Date
    Jul 2010
    Posts
    20
    Rep Power
    0

    Default

    ahh thank you! that was much easier than I was making it out to me. Over analyzing I guess?

    Thanks

Similar Threads

  1. Possible loss of precision
    By jankidudel in forum New To Java
    Replies: 2
    Last Post: 08-07-2010, 10:15 AM
  2. possible loss of precision?
    By gandalf5166 in forum New To Java
    Replies: 6
    Last Post: 04-13-2010, 08:21 PM
  3. Precision
    By c_walker in forum New To Java
    Replies: 1
    Last Post: 10-18-2009, 11:36 AM
  4. help me- loss of precision error??
    By j2vdk in forum New To Java
    Replies: 6
    Last Post: 09-01-2008, 09:23 AM
  5. Connection Loss
    By CrazyShells Slam in forum New To Java
    Replies: 0
    Last Post: 05-15-2008, 02:56 PM

Posting Permissions

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