Problem with: public boolean equals(Pet otherObject)

Before I edited this program the items for the user to enter were:  name, population and growthrate.  I wanted to add "age" as an item for the user to enter .  I added the "age" as a string not an int.  Everything compiles great untill I enter:  ((age.equalsignoreCase(otherObject.age)) below.  If I comment this part out  it compiles fine.  What am I doing wrong?  Untill I get this entry in correct the code wont execute correctly... it does compile if I comment this part out as I did in Exibit B below.  

Exibit (A)  (Code Exerpt)The following code gives me this error when I compile it:  C:\Documents and Settings\Owner\Desktop\java\CHP4\FINAL-PET\Pet.java:96: ')' expected

public boolean equals(Pet otherObject)
    {
      return ((name.equalsIgnoreCase(otherObject.name))
       ((age.equalsIgnoreCase(otherObject.age)) //this entry causes me to get:C:\Documents and Settings\Owner\Desktop\java\CHP4\FINAL-PET\Pet.java:96: ')' expected
            ((age.equalsIgnoreCase(otherObject.age))                
                && (population == otherObject.population)
                && (growthRate == otherObject.growthRate));
    }

Exibit (B) (Code Exerpt)  IF I COMMENT IT OUT LIKE THIS IT COMPILES FINE:

public boolean equals(Pet otherObject)

 {
      return ((name.equalsIgnoreCase(otherObject.name))
       //((age.equalsIgnoreCase(otherObject.age))
            ((age.equalsIgnoreCase(otherObject.age))                
                && (population == otherObject.population)
                && (growthRate == otherObject.growthRate));
    }

Exibit (C) FOLLOWING IS THE FULL CODE I AM TRYING TO GET COMPILED.  IF I COMMENT OUT ((age.equalsIgnoreCase(otherObject.age)) IT WORKS.  I NEED TO
KNOW HOW TO CORRECTLY ENTER ((age.equalsIgnoreCase(otherObject.age)) .

import java.io.*;

/*************************************************************************
 *Class for data on endangered species. This is a new, improved definition
 *of the class Species, which replaces the definition in Chapter 4.
 *************************************************************************/
public class Pet
{
   private String name;
   private String age;
   private int population;
   private double growthRate;

   public Pet()
    {
        name = null;
        age = null;
        population = 0;
        growthRate = 0;
    }

    public Pet(String initialName, String initialAge, int initialPopulation,
                                   double initialGrowthRate)
    {
        name = initialName;
        age = initialAge;
        if (initialPopulation >= 0)
            population = initialPopulation;
        else
        {
            System.out.println("ERROR: Negative population.");
            System.exit(0);
        }
        growthRate = initialGrowthRate;
    }

    public void set(String newName, String newAge, int newPopulation,
                                    double newGrowthRate)
    {
        name = newName;
        age = newAge;
        if (newPopulation >= 0)
            population = newPopulation;
        else
        {
            System.out.println("ERROR: Negative population.");
            System.exit(0);
        }
        growthRate = newGrowthRate;
    }

    /*******************************************************
     *Precondition: years is a nonnegative number.
     *Returns the projected population of the calling object
     *after the specified number of years.
     *******************************************************/
    public int projectedPopulation(int years)
    {
        double populationAmount = population;
        int count = years;

        while ((count > 0) && (populationAmount > 0))
        {
            populationAmount = (populationAmount +
                         (growthRate/100) * populationAmount);
            count--;
        }
        if (populationAmount > 0)
            return (int)populationAmount;
        else
            return 0;
    }

    public String getName()
    {
        return name;
    }
    public String getAge()
 {
     return age;
    }

    public int getPopulation()
    {
        return population;
    }

    public double getGrowthRate()
    {
        return growthRate;
    }

    public boolean equals(Pet otherObject)
    {
        return ((name.equalsIgnoreCase(otherObject.name))
            ((age.equalsIgnoreCase(otherObject.age))
                && (population == otherObject.population)
                && (growthRate == otherObject.growthRate));
    }

    /***************************
     *Sends output to the screen.
     ***************************/
    public void writeOutput()
    {
         System.out.println("Name = " + name);
         System.out.println("Age = " + age);
         System.out.println("Population = " + population);
         System.out.println("Growth rate = " + growthRate + "%");
    }

    /*********************************************************
     *Precondition: The stream outputStream has been connected
     *to a file.
     *Action: A record of the pet is written to the file
     *that is connected to outputStream. The record is written
     *as three items, IN THIS ORDER: a String for the name, an
     *int for the population, and a double for the growth rate.
     *********************************************************/
    public void writeOutput(DataOutputStream outputStream)
                                             throws IOException
    {
         outputStream.writeUTF(name);
         outputStream.writeUTF(age);
         outputStream.writeInt(population);
         outputStream.writeDouble(growthRate);
    }


     /*******************************
      *Takes input from the keyboard.
      *******************************/
     public void readInput()
     {
        System.out.println("What is the pet' name?");
        name = SavitchIn.readLine();
        //System.out.println("Enter the pet's age in years (a whole number)");
        //name = SavitchIn.readLine();
        //System.out.println("What is the population of the pet?");
        //population = SavitchIn.readLineInt();
        //System.out.println("Enter growth rate (percent increase per year):");
        //growthRate = SavitchIn.readLineDouble();
    }

    /*************************************************************
     *Precondition: The stream inputStream is connected to a file.
     *Each pet record appears in the file as three items,
     *IN THIS ORDER: a String for the name, an int for the
     *population, and a double for the growth rate.
     *Action: Reads a record from the stream and resets the data
     *for the calling object. An attempt to read past the end
     *of the file will throw an EOFException.
     ************************************************************/
    public void readInput(DataInputStream inputStream)
                                               throws IOException
    {
        name = inputStream.readUTF();
        age = inputStream.readUTF();
        //population = inputStream.readInt();
        //growthRate = inputStream.readDouble();
    }

}








rockyisinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rockyisinAuthor Commented:
Correction

 I accidentally included an extra entry in my example.  There should only be one AGE entry in the example.  So when I pound it out the reaming AGE entry shouldn't be there.

Corrected Examples:


Exibit (A)  (Code Exerpt)The following code gives me this error when I compile it:  C:\Documents and Settings\Owner\Desktop\java\CHP4\FINAL-PET\Pet.java:96: ')' expected

public boolean equals(Pet otherObject)
    {
      return ((name.equalsIgnoreCase(otherObject.name))
       ((age.equalsIgnoreCase(otherObject.age))
                && (population == otherObject.population)
                && (growthRate == otherObject.growthRate));
    }

Exibit (B) (Code Exerpt)  IF I COMMENT IT OUT LIKE THIS IT COMPILES FINE:

public boolean equals(Pet otherObject)

 {
      return ((name.equalsIgnoreCase(otherObject.name))
       //((age.equalsIgnoreCase(otherObject.age))
               && (population == otherObject.population)
                && (growthRate == otherObject.growthRate));
    }


0
lwinkenbCommented:
You just had a mix up with your parenthesis.  Change it to the following:

      public boolean equals(Equals otherObject)
      {
            return       ((name.equalsIgnoreCase(otherObject.name)) &&
                        (age.equalsIgnoreCase(otherObject.age)) &&
                        (age.equalsIgnoreCase(otherObject.age)) &&             
                        (population == otherObject.population) &&
                        (growthRate == otherObject.growthRate));
      }
0
objectsCommented:
   public boolean equals(Pet otherObject)
    {
        return name.equalsIgnoreCase(otherObject.name)
                  && (age.equalsIgnoreCase(otherObject.age)
                && (population == otherObject.population)
                && (growthRate == otherObject.growthRate);
    }
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

lwinkenbCommented:
oops, obviously you only need to compare to age once :)

      public boolean equals(Equals otherObject)
      {
            return       ((name.equalsIgnoreCase(otherObject.name)) &&
                        (age.equalsIgnoreCase(otherObject.age)) &&           
                        (population == otherObject.population) &&
                        (growthRate == otherObject.growthRate));
      }

0
objectsCommented:
woops left out a bracket.
You have left out the &&, thats why commenting out the line fixes it:
And all those brackets are not really necessary:

    public boolean equals(Pet otherObject)
    {
        return name.equalsIgnoreCase(otherObject.name)
                  && age.equalsIgnoreCase(otherObject.age)
                && population == otherObject.population
                && growthRate == otherObject.growthRate;
    }

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
Be careful if you imagine you've overridden equals - you haven't - you've overloaded it. This means that any API method that needs to call the equals method will not call your present equals method. If it is simply for your own convenience, then that's OK. Otherwise it should be:

public boolean equals(Object o)
{
      if (this == o)
            return true;
      if (o instanceof Pet)
      {
            Pet otherObject == (Pet)o;
            return name.equalsIgnoreCase(otherObject.name)
                  && age.equalsIgnoreCase(otherObject.age)
                  && population == otherObject.population
                  && growthRate == otherObject.growthRate;
      }
      return false;
}


0
FesterWimCommented:
and even more better:

public boolean equals(Object o)
{
     if( o == null)
         return false;

     if (this == o)
          return true;
     if (o instanceof Pet)
     {
          Pet otherObject == (Pet)o;
          return name.equalsIgnoreCase(otherObject.name)
               && age.equalsIgnoreCase(otherObject.age)
               && population == otherObject.population
               && growthRate == otherObject.growthRate;
     }
     return false;
}
0
CEHJCommented:
>>

     if( o == null)
         return false;
>>

The above is redundant (instanceof returns false if passed null)
0
FesterWimCommented:
true, but it's a bit faster maybe?

(don't know, haven't checked it, just trying to talk myself out of it :))
0
grim_toasterCommented:
If you want things to be faster, then you should change the ordering of the items in the if clause:

return population == otherObject.population
          && growthRate == otherObject.growthRate
          && name.equalsIgnoreCase(otherObject.name)
          && age.equalsIgnoreCase(otherObject.age);

As the == will be performed quicker than the other method calls (but we are now getting into micro-tuning!)
0
FesterWimCommented:
There is a problem in CEHJ's code that nobody seems to have noticed:

if (o instanceof Pet)
     {
          Pet otherObject == (Pet)o;
....


last line should be:

Pet otherObject = (Pet)o;
0
CEHJCommented:
LOL - yes
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.