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!