Results 1 to 6 of 6
  1. #1
    nnn82189 is offline Member
    Join Date
    Jun 2012
    Posts
    2
    Rep Power
    0

    Question Problem comparing the data within objects

    I have to create a "counter" that has a min/max value, the issue I'm having is when I have to compare my counter object to another instance of the counter object. I can't figure out how to compare the value's within the counter (min/max). I highlighted the method in question in blue. Any tips or hints would be great.

    Java Code:
    public class Counter
    {
        int count;
        int maxCount;
        int minCount;
    	
    
        /**
         * The default constructor for objects of class Counter.  Minimum is 0 and the maximum
         * is the largest possible integer.
         */
        public Counter()
        {
            
            minCount = 0;
            maxCount = Integer.MAX_VALUE;
            
        
        }
        
        
        /**
         * The alternate constructor for objects of class Counter.  The minimum and maximum values are given as parameters.
         */
        public Counter(int min, int max)
        {
            minCount = min;
            maxCount = max;
            
            
        	if(minCount >= maxCount)
            {
            	throw new CounterInitializationException("Min is higher than Max. Invalid.");
            }
        	
        }
        
       /**
      [COLOR="#0000FF"]   * Determine if two counters are in the same state
         *
         * @param  otherObject   the object to test against for equality
         * @return     true if the objects are in the same state
         */
        public boolean equals(Object otherObject)
        {
        	boolean result = false;
        	
        	Counter otherCounter = new Counter(minCount, maxCount);
        	
            if (otherObject instanceof Counter)
            	
            	otherCounter = (Counter) otherObject;
            
            	
            
      
           
            
    		return result;
        }[/COLOR]
    Last edited by nnn82189; 06-29-2012 at 08:12 PM.

  2. #2
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Default Re: Problem comparing the data within objects

    hello! please add the code tags to your code, it makes it much easier to read! :-)

    add: [ CODE] before your code and [ \CODE] after the end of your code. make sure to remove the extra space I have in between the [ and the C so that it works properly. Thanks!!

    But anyways, when you use the "=" which is the assignment operator, you are setting the value of whatever variable is on the left side to whatever formula or calculation or data is on the right. From what i see, you assign the "otherCounter" to the values of your "(Counter)otherObject. Make sure to use "==" or ".equals()" if you are trying to compare like data. - Whcih brings to mind, are you making sure that the "otherObject" you pass in to your equals() method is actually the same data? Maybe try breaking the integer values out into plain old integers and compare those? just a suggestion... :-/

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Problem comparing the data within objects

    Can you explain what the problem(s) are? If you are getting errors, please copy and paste the full text here

    Please edit you post and wrap the code in code tags.
    BB Code List - Java Programming Forum
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    nnn82189 is offline Member
    Join Date
    Jun 2012
    Posts
    2
    Rep Power
    0

    Default Re: Problem comparing the data within objects

    Sorry guys, I edited my post to make it easier to read. Norm, my problem is that I can't figure out how to compare the data within two objects. In my equals method, the parameter is an Object otherObject, and I first have to compare it to my original Counter object, which I did with
    Java Code:
    if(otherObject instanceof Counter)
    , this checks if the two objects are the same type, now I need to check if their values are the same, which is where I'm stuck. I need to pass a test in which the two objects are purposely given two separate values, and the program must be able to recognize this.

    This is the test I am trying to "pass"

    Java Code:
    public static void testEquals()
        {
            Counter c1 = new Counter(10,20);
            Counter c2 = new Counter(10,20);        
            Counter c3 = new Counter(11,20);
            Counter c4 = new Counter(10,21);
            System.out.println();
            System.out.println();
            System.out.println("TESTING the equals method");
            System.out.println("trying two counters that should be in the same state");
            if(c1.equals(c2))
            {
                System.out.println("     passes");
            }
            else
            {
                System.out.println("**** fails");
            }
    
            System.out.println("trying two counters that should be in a different state");
            if(c1.equals(c3))
            {
                System.out.println("**** fails");
            }
            else
            {
                System.out.println("     passes");
            }
    
            System.out.println("trying two counters that should be in a different state");
            if(c1.equals(c4))
            {
                System.out.println("**** fails");
            }
            else
            {
                System.out.println("     passes");
            }
            
            
        }

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Problem comparing the data within objects

    how to compare the data within two objects
    Is the method in one of the two objects? Then it has access to the values to be compared as class variables.
    Then the problem is how to get the values from the other object so you can compare them.
    You have references to the two objects: this and otherObject. Use those references to access the variables to be compared.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    Ksharp is offline Banned
    Join Date
    Jun 2012
    Location
    Beijing,China
    Posts
    34
    Rep Power
    0

    Default Re: Problem comparing the data within objects

    One more thing, When you override method equals() , don't forget to override method hashcode() at the same time.
    Tutorial has already pointed it out.


    Ksharp

Similar Threads

  1. comparing cloned objects
    By bradoz in forum New To Java
    Replies: 1
    Last Post: 05-14-2012, 04:31 PM
  2. Comparing two objects
    By colerelm in forum Advanced Java
    Replies: 3
    Last Post: 12-06-2011, 02:07 AM
  3. Comparing objects: lists and maps
    By LucasH in forum Advanced Java
    Replies: 7
    Last Post: 05-24-2011, 06:41 PM
  4. Issues with comparing two generic objects
    By FOUAD-SPIDER in forum Advanced Java
    Replies: 0
    Last Post: 05-06-2011, 04:39 AM
  5. Homework help involving comparing Color objects to Strings
    By SergeantJoKer in forum New To Java
    Replies: 12
    Last Post: 09-25-2010, 06:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •