Solved

Problem with:  public boolean equals(Pet otherObject)

Posted on 2003-11-15
15
401 Views
Last Modified: 2010-03-31
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();
    }

}








0
Comment
Question by:rockyisin
  • 3
  • 3
  • 2
  • +3
15 Comments
 

Author Comment

by:rockyisin
ID: 9757860
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
 
LVL 5

Expert Comment

by:lwinkenb
ID: 9757895
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
 
LVL 92

Expert Comment

by:objects
ID: 9757896
   public boolean equals(Pet otherObject)
    {
        return name.equalsIgnoreCase(otherObject.name)
                  && (age.equalsIgnoreCase(otherObject.age)
                && (population == otherObject.population)
                && (growthRate == otherObject.growthRate);
    }
0
 
LVL 5

Expert Comment

by:lwinkenb
ID: 9757899
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
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 9757907
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 9758153
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Expert Comment

by:FesterWim
ID: 9762075
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 9763340
>>

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

The above is redundant (instanceof returns false if passed null)
0
 
LVL 1

Expert Comment

by:FesterWim
ID: 9763353
true, but it's a bit faster maybe?

(don't know, haven't checked it, just trying to talk myself out of it :))
0
 
LVL 7

Expert Comment

by:grim_toaster
ID: 9763451
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
 
LVL 1

Expert Comment

by:FesterWim
ID: 9763767
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 9763786
LOL - yes
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now