Kara Dailey
Kara Dailey

Reputation: 15

Implementation help intro to java

I have a question about implementation in my intro course. I came up with an answer yet the compilation says that "compilation error (line 3, column 9) : possible loss of precision" I'm confused as to what this loss of precision is.
My homework question is as follows: Recall that the Person class implements the Comparable interface:

public class Person implements Comparable

Now suppose that we want to compare employees by their salary. Since Employee extends Person, Employee already implements Comparable, via the Person compareTo method, which compares Person objects by age. Now we want to override that compareTo method in the Employee class in order to make the salary comparisons.

For this assignment, modify the Employee class by implementing 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. Remember that the code you enter is located in the Employee class.

  /**
    * Compares this object with the specified object for order.
    * @param o the Object to be compared.
    */
    public int compareTo(Object obj) 
    {

Here is my code

    double b= ((Employee)obj).getSalary();
    double a= this.salary;
    return(a-b); 
    }

Here is the Employee class code:

class Employee extends Person
{

     private double salary;

     /**
  * constructor with five args.
  * @param n the name
  * @param ag the age
  * @param ht the height
  * @param p the phone number
  * @param the salary
  */
  public Employee(String n, int ag, int ht, String p, double s)
  {
    super( n, ag, ht, p );
    salary = s;
  }

  /**
   * Get the salary.
   * @return double the salary.
   */
  public double getSalary( )
  {
    return salary;
  }

  /**
   * Raise the employee's salary by a given percent.
   * @param percentRaise
   */
  public void raise(double percentRaise)
  {
    salary *= ( 1 + percentRaise );
  }

    /**
     * Compares this object with the specified object for order.
     * @param o the Object to be compared.
     */
    public int compareTo(Object obj) 
    {
  /* your code goes here */
    }

  /**
    * get a String representation of the employee's data.
    * @return String the representation of the data.
    */
  public String toString( )
  {
    return super.toString( ) + " $" + getSalary( );
  }

}

Any help to get me to answer correctly would be very much appreciated. I've been working on this singular question for over an hour, and that compilation error is just puzzling me to no end. Thank you!

Upvotes: 1

Views: 1673

Answers (2)

rgettman
rgettman

Reputation: 178303

The problem is that the compareTo method must return an int, but subtracting the salaries yields a double. Java will not let you implicitly convert the double to an int without a cast. While a cast will get the code to compile, the result could be wrong. For example, a difference of 0.4 will be cast to an int as 0, erroneously reporting an equality.

You can test the salaries for less than, equal to, or greater than, and return -1, 0, or 1 respectively. You could also return the result of calling Double.compare, passing the 2 salaries.

If you are a beginner, then you may not be aware that usually the Comparable interface is generic and is implemented by supplying a type parameter. In this case, this answers the question "Comparable to what?". The compareTo method's parameter is generic, so it takes the same type. This also avoids needing to cast obj to Person in the method body.

public class Person implements Comparable<Person>

and

public int compareTo(Person obj) 

Upvotes: 4

CubeJockey
CubeJockey

Reputation: 2219

I believe the loss in precision is because you're performing arithmetic on a pair of doubles and returning the result, but your method header is declared to return an int.

Try casting your subtraction:

public int compareTo(Object obj) 
{
    double b= ((Employee)obj).getSalary();
    double a= this.salary;
    return (int) (a-b); 
}

However, since it looks like your intention is to make a comparison between the salaries, try something like this:

public int compareTo(Object obj) 
{
    double b= ((Employee)obj).getSalary();
    double a= this.salary;
    return Double.compare(a, b); 
}

Upvotes: 2

Related Questions