I'm using Eclipse and it's giving me an error saying "Cannot invoke compareTo(int) on the primitive type int". Here is my code:
public class ReadingMaterial implements Comparable<ReadingMaterial> {
private int pages;
private String title;
private String author;
public ReadingMaterial(int pages, String title, String author)
{
super();
this.title = title;
this.pages = pages;
this.author = author;
}
public int getPages()
{
return pages;
}
public void setPages(int pages)
{
this.pages = pages;
}
public String getAuthor()
{
return author;
}
public void setAuthor(String author)
{
this.author = author;
}
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
@Override
public int compareTo(ReadingMaterial o) {
if(pages.compareTo(o.getPages()) > 0)
return -1;
else if(pages.compareTo(o.getPages()) < 0)
return 1;
else
{
return 0;
}
}
@Override
public String toString()
{
return "The reading material you selected is titled " + title + " with" + pages + "pages. It is written by " + author + ".";
}
}
The example we were given in class does a similar thing except it is using a double value, so I'm not sure why the file from class works, but mine doesn't. I'd appreciate any help.
Example from class:
import java.text.DecimalFormat;
public class Employee implements Comparable<Employee> {
private Double salary;
private int hoursWorked;
private String firstName;
private String lastName;
private static DecimalFormat df = new DecimalFormat("0.00");
public Employee(double salary, int hoursWorked, String firstName,
String lastName) {
this.salary = salary;
this.hoursWorked = hoursWorked;
this.firstName = firstName;
this.lastName = lastName;
}
@Override
public String toString() {
return firstName + " " +
lastName + " works " +
hoursWorked + " hours and makes $" +
df.format(salary);
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
public int getHoursWorked() {
return hoursWorked;
}
public void setHoursWorked(int hoursWorked) {
this.hoursWorked = hoursWorked;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public int compareTo(Employee o) {
// TODO Auto-generated method stub
if(salary.compareTo(o.getSalary()) > 0)
return -1;
else if(salary.compareTo(o.getSalary()) < 0)
return 1;
else
{
return lastName.compareTo(o.getLastName());
}
}
}