Hi im trying to use an equals method to check if two bargraphs are equal, but when I run the method, it always says that they are not equal. Could you please tell me how to fix this? Thanks

My code for the method

// equals method
	
	public boolean eqls(BarGraph object){
		boolean eqls = true;
		int [] grade2 = new int[object.getGraph().length];
		for(int i = 0; i < grade2.length; i++){
			grade2[i] = object.getGraph()[i];
		}
		if(grade2.length != grade.length){
			eqls = false;
		}
		else{
			for(int i = 0; i < grade.length; i++){
				grade2[i] = object.getGraph()[i];
				
				if(grade2[i] != grade[i]){
				eqls = false;
				break;
				}
			}
		}			
	return eqls;	
}

The client for this is

import java.util.Random;

public class BarGraphClient7
{
   public static void main(String[] args)
   {
      int[] someGrades = new int[ 400 ];
      Random rand = new Random( 42 );
      int i;
      
      for ( i = 0; i < 400; i++ ) {
         someGrades[i] = rand.nextInt(100) + 1;
      }

      BarGraph november, oscar, papa;
      november = new BarGraph( 50 );
      oscar = new BarGraph( 50 );
      papa = new BarGraph( 25 );
      
      if ( papa.equals(oscar) )
         System.out.println("papa and oscar are equal");
      else
         System.out.println("papa and oscar are NOT equal");

      if ( november.equals(oscar) )
         System.out.println("november and oscar are equal");
      else
         System.out.println("november and oscar are NOT equal");

      november.setGraph( someGrades );
      if ( november.equals(oscar) )
         System.out.println("november and oscar are equal");
      else
         System.out.println("november and oscar are NOT equal");

      oscar.setGraph( someGrades );
      if ( oscar.equals(november) )
         System.out.println("november and oscar are equal");
      else
         System.out.println("november and oscar are NOT equal");

      oscar.add(75);
      if ( oscar.equals(november) )
         System.out.println("november and oscar are equal");
      else
         System.out.println("november and oscar are NOT equal");

      november.add(75);
      if ( oscar.equals(november) )
         System.out.println("november and oscar are equal");
      else
         System.out.println("november and oscar are NOT equal");
   } // end of method main
} // end of class BarGraphClient7

Thanks!

You are calling the "equals" method and in your class you have created a "eqls" method.

Your class need to override the "equals" method:
http://java.sun.com/javase/6/docs/api/java/lang/Object.html#equals(java.lang.Object)

Since you haven't overridden the equals method in your class, in the main, the equals of the super class is called (Object class) that checks if 2 objects are the same instance, not if they have the same value

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.