Hi, I am trying to compare two objects, but it is not working correctly. Please help.

/**
 * Compares theObject with <CODE>this</CODE> object for 
 * equivalence. Do not concern yourself with runtime
 * efficiency.
 * @param theObject to be compared with this object
 * @return true if theObject is equivalent to this
 * object.
 */
@Override public  
boolean equals( Object theObject ){
	if (theObject == null) return false;
	if(!(theObject instanceof BoundedBag)) return false;
	if (this == theObject) return true;
	
	BoundedBag obj = (BoundedBag) theObject;

	if( this.size() != obj.size() ) return false;
	
	// Fill in the remainder of the code.
	
	BoundedBag extra = (BoundedBag)thisObject;
	if(thisObject.equals (theObject))
		return true;
	else
		return false;		
}

Dani AI

Generated

The compiler error you reported is a simple name mismatch: a variable named thisObject is never declared. As asked, check where you declared the casted reference — you cast the argument to a local like obj (or you meant to reuse theObject) but later refer to thisObject. Fix that and remove the redundant self-comparison; the this == theObject identity check is fine, but further logic must use the correctly named local.

For multiset-style equality (bags that care about element counts but not order) a clear approach is to count occurrences in one bag and then decrement for the other. Sketch in Java:

Map<Object,Integer> counts = new HashMap<>();
for (Object e : thisBagElements) counts.put(e, counts.getOrDefault(e,0) + 1);
for (Object e : otherBagElements) {
  Integer c = counts.get(e);
  if (c == null) return false;
  if (c == 1) counts.remove(e); else counts.put(e, c-1);
}
return counts.isEmpty();

Make sure element equality uses equals (handles nulls appropriately if your bag allows them). Also implement hashCode consistent with equals (for example, aggregate element hashCodes weighted by counts) so the contract in the JavaDoc is satisfied. See the standard equals/hashCode contract for details: Object.equals javadoc and Object.hashCode javadoc.

Include JUnit tests for: same instance, null argument, different class, same contents different order, differing multiplicities, and cases with null elements (if supported).

Recommended Answers

All 5 Replies

Help us out a little. What do you expect to see, and what are you seeing instead?

Stating your problem clearly won't just help me and others to solve it, it might actually lead you to a solution.

I need to implement "equals" then throughly test it using JUnit. Two BoundedBags are equivalent if their elements are the same regardless of order. For example, true is returned from each of the following

{1,2,3}.equals ({3,1,2} )
{1,1,1,3}.equals( {1,3,1,1} )
but {1,1,1,3}.equals( {1,1,3} ) returns false.

I am getting an error from lines 2 and below. It is saying that thisObject can not be resolved to a variable.

I meant line 21 and below

Where do you declare it?

401 with Hedlund?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.