I visit NUF a lot, and I believe that there are other programmers who visit NUF, too. Anyways, here's my problem. I don't know how to get the correct output after I run the boolean "equals" code. Any help? Spoiler: code PHP: //method in PetRecord class.public boolean equals(PetRecord otherPet) { boolean same = false; if ((this.name.equals(otherPet.getName())) && (this.age == otherPet.getAge())) { same = true; } return same; } PHP: //method in main method.public static boolean petArraysEqual(PetRecord[] a, PetRecord[] b) { boolean match = false; if (a.equals(b)) match = true;return match; PHP: //Output for comparing arrays in main method.if (petArraysEqual(pets, newPets)) // call this using the names of your pet record arrays System.out.println("Pet arrays are equal"); else System.out.println("Pet arrays are not equal"); Here's the code that doesn't use the PetRecord class method. Spoiler PHP: if (a.length != b.length) match = false; else { match = true; int i = 0; while (match && (i < a.length)) { if (a[i].getName() != b[i].getName() || a[i].getAge() != b[i].getAge() && a[i].getWeight() != b[i].getWeight()) match = false; i++; } } return match; Edit: Problem has been solved. No need for more answers.
a and b are arrays. You need to check all elements Code: for(int i=0; i < a.length; i++){ if(!a.equals(b)) return false; } return true; also check whether a and b have the same size
i havent coded in a few years but idk why you have the same function twice, your equals thing is completely redundant lol
I was suppose to use both a method from the main and a method from the class PetRecord. I thought moving the code from petArraysEquals would not give me a wrong output. I did declare a name and age. I left that part out.
You could also shorten your functions a bit if you write Code: return this.name.equals(otherPet.getName()) && (this.age == otherPet.getAge()) instead of Code: boolean same = false; if ((this.name.equals(otherPet.getName())) && (this.age == otherPet.getAge())) { same = true; } return same; It's somewhat more readable
Java and Javascript are not the same language (as far as tags go) I haven't coded java in a while but are you trying to overload the equals function?
I'm trying to use the name.equals(object[] varName) from PetRecords, but I keep getting the wrong output.
Is this the correct answer? Code: for (int i = 0; i < a.length; i++) { if(a[i].equals(b[i]) return true; } return false;
ya but you have 2 functions doing the exact same thing now, this is probably not what they meant, it is too dumb. unless equals isnt supposed to be a pet specific function and will be used later but i doubt it. nvm just get rid of it, it bugs me too much it cant be right. also in your 2nd bit of code just use a for loop it's good practice. i also dont like how you set match as true inside the if, just do it outside, again looks better that way.
Also, would be easier if you used List instead of arrays. Its equals loops through contained elements.
Well as others have said, you need to go through every record of the array and decide if they are equal. I just couldn't understand why create a separate function plus a separate class + overloading the equals. Seems a bit pointless...
also wtf is going on here public boolean equals(PetRecord otherPet) { boolean same = false; if ((this.name.equals(otherPet.getName())) && (this.age == otherPet.getAge())) { same = true; } return same; } ur gonna keep getting the name forever edit: lol nvm but maybe, why are they both named equals, are you calling it? again havent coded in years
Ugh I talk about arrays and then forget the indices myself -.-' It's important to check whether a and b are the same size. And switch the conditions Code: if(a.length != b.length) return false; for (int i = 0; i < a.length; i++) { if(!a[i].equals(b[i]) return false; } return true;
I'm kinda curious to see the whole object than just a the few methods he has written here... Like what is the .getName() method declaration and how is it constructed... Can we see the whole PetRecord object?