create an equals() method that overrides the object equals() method
I'm having a little problem with trying to create an equals object that overrides the object equals method if the id numbers are the same.
It's the only problem i'm having, other than that, my program runs fine. Here's the code:
Code Java:
public class Movie
{
private String rating;
private int ID;
private String title;
public Movie()
{
rating = "No rating";
title = "No title";
ID = 00000;
}
public Movie(String theTitle,String theRating, int theID)
{
if (theRating == null || theTitle == null || theID < 0)
{
System.out.println("Fatal Error creating Movie.");
System.exit(0);
}
rating = theRating;
title = theTitle;
ID = theID;
}
public Movie(Movie originalObject)
{
rating = originalObject.rating;
title = originalObject.title;
ID = originalObject.ID;
}
public String getRating()
{
return rating;
}
public String getTitle()
{
return title;
}
public int getID()
{
return ID;
}
public void setRating(String newRating)
{
if (newRating == null)
{
System.out.println("Fatal Error setting Movie rating.");
System.exit(0);
}
else
rating = newRating;
}
public void setTitle(String newTitle)
{
if (newTitle == null)
{
System.out.println("Fatal Error setting Movie title.");
System.exit(0);
}
else
title = newTitle;
}
/**
Precondition newID is a non-negative number.
*/
public void setID(int theID)
{
if (theID < 0)
{
System.out.println("Fatal Error setting Movie ID.");
System.exit(0);
}
else
ID = theID;
}
public String toString()
{
return ("Title: " + title + "\nRating: " + rating + "\nID#: " + ID);
}
public double calcLateFee(int dayLate)
{
return 2.0*dayLate;
}
public int equals(int theID)
{
if(ID == theID)
{
System.out.print("These movies are identical");
}
return ID;
}
public boolean equals(Movie otherObject)
{
if(otherObject == null)
return false;
else if (getClass() != otherObject.getClass())
return false;
else
{
Movie otherMovie = (Movie)otherObject;
return ( (rating.equals(otherMovie.rating)) && (title.equals(otherMovie.title)) && (ID==otherMovie.ID) );
}
}
}
Re: create an equals() method that overrides the object equals() method
What problems are you having with your code?
Two problems I see:- You aren't using the @Override annotation
- If you did this, you'd know that your equals method signature isn't correct. Note that it must match that of Object exactly. I'll let you figure out how.