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: Implementing a good .equals method?

  1. #1
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Implementing a good .equals method?

    I am making a programming project that implements terms for polynomials. For example, if I put "7x^3+4x^2", then the class has an inner array list that stores "7x^3" and "4x^2". However, I need to implement a method .equals that I don't know how to implement very well. here is the method:

    	@Override
    	public boolean equals(Polynomial P)
    	{
    		return true;
    	}

    It has as a parameter another Polynomial P. However, the class does not include a method get() or getTerm(), so I can't do the following:

    	@Override
    	public boolean equals(Polynomial P)
    	{
    		int counter = -1;
    		for(int i = 0; i <= this.terms.size() - 1; i++)
    		{
    			if(!(this.get(i).equals(P.get(i))))
    			{
    				counter = 1;
    				break;
    			}
    		}
    		return couter == -1;
    	}

    I think I am supposed to do it with a for each loop. Can anyone help me and give me a hint of how it should be done?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Implementing a good .equals method?

    What is the name of the arraylist? Have you tried using the get() method with that?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. create an equals() method that overrides the object equals() method
    By slinky29 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 3rd, 2013, 11:11 PM
  2. equals() method.
    By TP-Oreilly in forum Object Oriented Programming
    Replies: 2
    Last Post: February 11th, 2012, 11: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, 03:06 AM