Solved

Problem with:  public boolean equals(Pet otherObject)

Posted on 2003-11-15
15
408 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jdbc error in jsp application 20 91
Android development question 2 109
Detect Closed Loops (circles, figure-8s, etc) in PNG Images 6 96
Java syntax, or is it Selenium 6 61
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

739 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