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(oth erObject.a ge)) 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\jav a\CHP4\FIN AL-PET\Pet .java:96: ')' expected
public boolean equals(Pet otherObject)
{
return ((name.equalsIgnoreCase(ot herObject. name))
((age.equalsIgnoreCase(oth erObject.a ge)) //this entry causes me to get:C:\Documents and Settings\Owner\Desktop\jav a\CHP4\FIN AL-PET\Pet .java:96: ')' expected
((age.equalsIgnoreCase(oth erObject.a ge))
&& (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(ot herObject. name))
//((age.equalsIgnoreCase(o therObject .age))
((age.equalsIgnoreCase(oth erObject.a ge))
&& (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(oth erObject.a ge)) IT WORKS. I NEED TO
KNOW HOW TO CORRECTLY ENTER ((age.equalsIgnoreCase(oth erObject.a ge)) .
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(ot herObject. name))
((age.equalsIgnoreCase(oth erObject.a ge))
&& (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("Popula tion = " + 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(DataOutputStre am outputStream)
throws IOException
{
outputStream.writeUTF(name );
outputStream.writeUTF(age) ;
outputStream.writeInt(popu lation);
outputStream.writeDouble(g rowthRate) ;
}
/************************* ******
*Takes input from the keyboard.
************************** *****/
public void readInput()
{
System.out.println("What is the pet' name?");
name = SavitchIn.readLine();
//System.out.println("Ente r 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("Ente r 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();
}
}
Exibit (A) (Code Exerpt)The following code gives me this error when I compile it: C:\Documents and Settings\Owner\Desktop\jav
public boolean equals(Pet otherObject)
{
return ((name.equalsIgnoreCase(ot
((age.equalsIgnoreCase(oth
((age.equalsIgnoreCase(oth
&& (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(ot
//((age.equalsIgnoreCase(o
((age.equalsIgnoreCase(oth
&& (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(oth
KNOW HOW TO CORRECTLY ENTER ((age.equalsIgnoreCase(oth
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:
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:
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(ot
((age.equalsIgnoreCase(oth
&& (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("Popula
System.out.println("Growth
}
/*************************
*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(DataOutputStre
throws IOException
{
outputStream.writeUTF(name
outputStream.writeUTF(age)
outputStream.writeInt(popu
outputStream.writeDouble(g
}
/*************************
*Takes input from the keyboard.
**************************
public void readInput()
{
System.out.println("What is the pet' name?");
name = SavitchIn.readLine();
//System.out.println("Ente
//name = SavitchIn.readLine();
//System.out.println("What
//population = SavitchIn.readLineInt();
//System.out.println("Ente
//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();
}
}
You just had a mix up with your parenthesis. Change it to the following:
public boolean equals(Equals otherObject)
{
return ((name.equalsIgnoreCase(ot herObject. name)) &&
(age.equalsIgnoreCase(othe rObject.ag e)) &&
(age.equalsIgnoreCase(othe rObject.ag e)) &&
(population == otherObject.population) &&
(growthRate == otherObject.growthRate));
}
public boolean equals(Equals otherObject)
{
return ((name.equalsIgnoreCase(ot
(age.equalsIgnoreCase(othe
(age.equalsIgnoreCase(othe
(population == otherObject.population) &&
(growthRate == otherObject.growthRate));
}
public boolean equals(Pet otherObject)
{
return name.equalsIgnoreCase(othe rObject.na me)
&& (age.equalsIgnoreCase(othe rObject.ag e)
&& (population == otherObject.population)
&& (growthRate == otherObject.growthRate);
}
{
return name.equalsIgnoreCase(othe
&& (age.equalsIgnoreCase(othe
&& (population == otherObject.population)
&& (growthRate == otherObject.growthRate);
}
oops, obviously you only need to compare to age once :)
public boolean equals(Equals otherObject)
{
return ((name.equalsIgnoreCase(ot herObject. name)) &&
(age.equalsIgnoreCase(othe rObject.ag e)) &&
(population == otherObject.population) &&
(growthRate == otherObject.growthRate));
}
public boolean equals(Equals otherObject)
{
return ((name.equalsIgnoreCase(ot
(age.equalsIgnoreCase(othe
(population == otherObject.population) &&
(growthRate == otherObject.growthRate));
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(othe rObject.na me)
&& age.equalsIgnoreCase(other Object.age )
&& population == otherObject.population
&& growthRate == otherObject.growthRate;
}
return false;
}
public boolean equals(Object o)
{
if (this == o)
return true;
if (o instanceof Pet)
{
Pet otherObject == (Pet)o;
return name.equalsIgnoreCase(othe
&& age.equalsIgnoreCase(other
&& 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(othe rObject.na me)
&& age.equalsIgnoreCase(other Object.age )
&& population == otherObject.population
&& growthRate == otherObject.growthRate;
}
return false;
}
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(othe
&& age.equalsIgnoreCase(other
&& population == otherObject.population
&& growthRate == otherObject.growthRate;
}
return false;
}
>>
if( o == null)
return false;
>>
The above is redundant (instanceof returns false if passed null)
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 :))
(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(othe rObject.na me)
&& age.equalsIgnoreCase(other Object.age );
As the == will be performed quicker than the other method calls (but we are now getting into micro-tuning!)
return population == otherObject.population
&& growthRate == otherObject.growthRate
&& name.equalsIgnoreCase(othe
&& age.equalsIgnoreCase(other
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;
if (o instanceof Pet)
{
Pet otherObject == (Pet)o;
....
last line should be:
Pet otherObject = (Pet)o;
LOL - yes
ASKER
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\jav
public boolean equals(Pet otherObject)
{
return ((name.equalsIgnoreCase(ot
((age.equalsIgnoreCase(oth
&& (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(ot
//((age.equalsIgnoreCase(o
&& (population == otherObject.population)
&& (growthRate == otherObject.growthRate));
}