Another Java Problem

Discussion in 'Tech Discussion' started by AMissingLinguist, Oct 28, 2017.

  1. AMissingLinguist

    AMissingLinguist [Not Here][Blank Sect][Nuffian #N]

    Joined:
    May 15, 2016
    Messages:
    2,297
    Likes Received:
    6,383
    Reading List:
    Link
    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?
    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[] aPetRecord[] b) {

          
    boolean match false;
          if (
    a.equals(b))
            
    match true;
    return 
    match;

    PHP:
    //Output for comparing arrays in main method.
    if (petArraysEqual(petsnewPets)) // 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.
    PHP:

    if (a.length != b.length)

              
    match false;

          else {

              
    match true;

              
    int i 0;

              while (
    match && (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.
     
    Last edited: Oct 28, 2017
  2. kjoke

    kjoke Well-Known Member

    Joined:
    Nov 20, 2015
    Messages:
    72
    Likes Received:
    111
    Reading List:
    Link
    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
     
    Cite and AMissingLinguist like this.
  3. KingOfDreams

    KingOfDreams [Master of Sleepiness]

    Joined:
    Jan 7, 2017
    Messages:
    210
    Likes Received:
    106
    Reading List:
    Link
    you need to declare name and age in the function or it'll return an unknown variable error
     
  4. asdf123

    asdf123 Well-Known Member

    Joined:
    Dec 22, 2016
    Messages:
    293
    Likes Received:
    174
    Reading List:
    Link
    i havent coded in a few years but idk why you have the same function twice, your equals thing is completely redundant lol
     
  5. AMissingLinguist

    AMissingLinguist [Not Here][Blank Sect][Nuffian #N]

    Joined:
    May 15, 2016
    Messages:
    2,297
    Likes Received:
    6,383
    Reading List:
    Link
    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.
     
  6. kjoke

    kjoke Well-Known Member

    Joined:
    Nov 20, 2015
    Messages:
    72
    Likes Received:
    111
    Reading List:
    Link
    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
     
    AMissingLinguist likes this.
  7. AMissingLinguist

    AMissingLinguist [Not Here][Blank Sect][Nuffian #N]

    Joined:
    May 15, 2016
    Messages:
    2,297
    Likes Received:
    6,383
    Reading List:
    Link
    Thanks. I'm still a beginner in this, so I didn't know about that.
     
  8. lnv

    lnv ✪ Well-Known Hypocrite

    Joined:
    Jan 24, 2017
    Messages:
    7,702
    Likes Received:
    9,044
    Reading List:
    Link
    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?
     
    AMissingLinguist likes this.
  9. AMissingLinguist

    AMissingLinguist [Not Here][Blank Sect][Nuffian #N]

    Joined:
    May 15, 2016
    Messages:
    2,297
    Likes Received:
    6,383
    Reading List:
    Link
    I'm trying to use the name.equals(object[] varName) from PetRecords, but I keep getting the wrong output.
     
  10. Vilidious

    Vilidious Well-Known Member

    Joined:
    Jul 28, 2017
    Messages:
    699
    Likes Received:
    892
    Reading List:
    Link
    By the way, you need to use the index on the arrays in addition to that size check...
     
    AMissingLinguist likes this.
  11. AMissingLinguist

    AMissingLinguist [Not Here][Blank Sect][Nuffian #N]

    Joined:
    May 15, 2016
    Messages:
    2,297
    Likes Received:
    6,383
    Reading List:
    Link
    Is this the correct answer?
    Code:
    
    for (int i = 0; i < a.length; i++) {
        if(a[i].equals(b[i])
            return true;
    }
    return false;
    
     
  12. asdf123

    asdf123 Well-Known Member

    Joined:
    Dec 22, 2016
    Messages:
    293
    Likes Received:
    174
    Reading List:
    Link
    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.
     
    AMissingLinguist likes this.
  13. asdf123

    asdf123 Well-Known Member

    Joined:
    Dec 22, 2016
    Messages:
    293
    Likes Received:
    174
    Reading List:
    Link
    looks fine
     
  14. Vilidious

    Vilidious Well-Known Member

    Joined:
    Jul 28, 2017
    Messages:
    699
    Likes Received:
    892
    Reading List:
    Link
    Also, would be easier if you used List instead of arrays. Its equals loops through contained elements.
     
  15. lnv

    lnv ✪ Well-Known Hypocrite

    Joined:
    Jan 24, 2017
    Messages:
    7,702
    Likes Received:
    9,044
    Reading List:
    Link
    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...
     
    AngelLoufi likes this.
  16. asdf123

    asdf123 Well-Known Member

    Joined:
    Dec 22, 2016
    Messages:
    293
    Likes Received:
    174
    Reading List:
    Link
    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
     
  17. kjoke

    kjoke Well-Known Member

    Joined:
    Nov 20, 2015
    Messages:
    72
    Likes Received:
    111
    Reading List:
    Link
    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;
    
     
    Vilidious likes this.
  18. Vilidious

    Vilidious Well-Known Member

    Joined:
    Jul 28, 2017
    Messages:
    699
    Likes Received:
    892
    Reading List:
    Link
    Switch the conditions. You want to find any not equal; i.e all equal.
     
    kjoke and AMissingLinguist like this.
  19. AngelLoufi

    AngelLoufi Well-Known Member

    Joined:
    Jul 29, 2016
    Messages:
    91
    Likes Received:
    52
    Reading List:
    Link
    Love the "java" and "JavaScript" title and tags when you describe the code as PHP :blob_grin::blob_grin:
     
    AMissingLinguist likes this.
  20. novalance

    novalance Well-Known Member

    Joined:
    Nov 28, 2015
    Messages:
    429
    Likes Received:
    338
    Reading List:
    Link
    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?
     
    AMissingLinguist likes this.