Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: create an equals() method that overrides the object equals() method

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:

    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) );
        	}
    	}
     
    }


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default 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.

Similar Threads

  1. problems with equals method.
    By mkarthik90 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 29th, 2012, 11:11 PM
  2. equals() method.
    By TP-Oreilly in forum Object Oriented Programming
    Replies: 2
    Last Post: February 11th, 2012, 10:59 AM
  3. Using the .equals method
    By TenaciousE in forum Java Theory & Questions
    Replies: 2
    Last Post: October 23rd, 2011, 05:24 PM
  4. Method Equals, Boolean...
    By boys8goods in forum Object Oriented Programming
    Replies: 1
    Last Post: June 25th, 2011, 03:34 PM
  5. Equals Method
    By Sninald in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 18th, 2010, 02:06 AM