Link to home
Start Free TrialLog in
Avatar of rockyisin
rockyisin

asked on

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();
    }

}








Avatar of rockyisin
rockyisin

ASKER

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));
    }


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));
      }
Avatar of Mick Barry
   public boolean equals(Pet otherObject)
    {
        return name.equalsIgnoreCase(otherObject.name)
                  && (age.equalsIgnoreCase(otherObject.age)
                && (population == otherObject.population)
                && (growthRate == otherObject.growthRate);
    }
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));
      }

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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;
}


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;
}
>>

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

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

(don't know, haven't checked it, just trying to talk myself out of it :))
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!)
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;
LOL - yes