Same story, new to Java and need help please! :)
Hi!,
This is my first post here, and any help I can get would be greatly appreciated. As it is a class assignment, and I want to understand what is going on and not just get an outright solution, I will put my problem into a question that I will try to explain as well as possible. I hope this is ok as I really want to understand what I am doing.
I am a student (an old dog learning new tricks, so to say), and my assignment is this:
The Lab is modifying an animal interaction simulator (predator/prey). There is an abstract class that is called Animal, and it has been extended into two subclasses of two specific types of animal (a predator and its prey). The predator/prey classes have identical Boolean methods that check if the predator or prey are of breeding age (current age >= breeding age) and return true/false.
I am to create a new Boolean method in the Animal class that performs the same function as the methods in the predator/prey subclasses. This method will compare the current age and check if it is >= to the breeding age for that animal, same as the original methods in the subclasses.
I then modify the 'old' Boolean methods in the two subclasses to integer type (int) so that they return the breeding age for that animal to the method in the superclass that calls it.
I have the predator and prey subclasses modified int/return methods able to call the new method in the superclass, but how do I get the superclass to 'see' the modified methods in the predator and prey subclasses, call them and then differentiate between them?
I have tried to word this efficently and yet be thorough about what I am asking. I hope it is enough to go on, if not then please let me know!!! If you need to see the code, I will post it. I just wanted to try this method of asking for help as I am really trying to understand the underlying interaction of Java classes/subclasses/abstract classes and their constructors and methods.
Thank you!
[2014 byte] By [
DrDouga] at [2007-11-27 3:46:30]

I kind of thought the same thing. I am no expert on Java (by far...lol!), but the arrangement was just fine the way it was set up in the simulator. Here is a biit more about it:
The original code in both the Predator (and Prey) subClass (extends Animal):
/**
* The Predator can breed if it has reached the breeding age.
*/
private boolean canBreed()
{
return getAge() >= BREEDING_AGE;
}
First, I am to change the code in the above method(s, Predator & Prey) to:
/**
* The Predator can breed if it has reached the breeding age.
*/
public static int getBreedingAge()
{
return BREEDING_AGE;
}
Here is what the Lab (and course book) say to then add to the Animal superClass:
/**
* A Predator can breed if it has reached the breeding age.
*/
public boolean canBreed()
{
return age >= getBreedingAge();
}
Note: If it makes a difference, the Animal superClass is an Abstract Class.
After doing this, the book says to see if it will compile (which it will not), and to then find out why it will not work (interesting way to put it...lol!). The compilation error is in the Animal superClass:
cannot find symbol - method getBreedingAge()
And it is because that method (in the Animal superClass) can not 'see' the method it wants to call from the subClass(es), nor would it know which one to call if it could.
I tested this idea using the fact that the BREEDING_AGE variable in the Predator/Prey subClasses are static and final, and I changed the code (detailed below).
Animal superClass method change I made (from one the book says to enter above):
/**
* The Predator can breed if it has reached the breeding age.
*/
public boolean canPredatorBreed()
{
return age >= Predator.getBreedingAge();
}
/**
* The Prey can breed if it has reached the breeding age.
*/
public boolean canPreyBreed()
{
return age >= Prey.getBreedingAge();
}
I then modified the method code in the Predator subClass from what the book said (from above):
/**
* The Predator can breed if it has reached the breeding age.
*/
public static int getBreedingAge()
{
return BREEDING_AGE;
}
and then modified the method code in the Prey subClass from what the book said (from above):
/**
* The Prey can breed if it has reached the breeding age.
*/
public static int getBreedingAge()
{
return BREEDING_AGE;
}
I then changed the calls in the Predator and Prey subClasses so that they each look up the canPredatorBreed() (and canPreyBreed() ) method from the Animal superClass methods above, and those methods look in the respective getBreedingAge() methods in the Predator and Prey subClasses.
This works fine, but it is using static variables, and this is not what the book wants me to do (as detailed above in the Lab/book instructions/code). In this case, the objects variables are static. I need to address them as non-static variables, even though the BREEDING_AGE variables in Predator/Prey are static.
This is because the next step of the lab is to do the same thing with a non-static variable (currentAge), so it has to be able to be referred to as a non-static.
I hope this helps to explain my quandry here! Thanks in advance for any help offered! :)
Oops, small error! The second and third method code sections above should be:
/**
* The Predator can breed if it has reached the breeding age.
*/
public int getBreedingAge()
{
return BREEDING_AGE;
}
and:
/**
* The Prey can breed if it has reached the breeding age.
*/
public int getBreedingAge()
{
return BREEDING_AGE;
}
I removed the words 'static'. Then in the last two code sections above, I add 'static' to them to make the static object/variable method I created work.
Sorry! Too many windows open on too small of a screen...
Well, I got it figured out. While the result is more complex than the initial code that it replaced, it works. Here is what I did:
In the Animal Class, I added:
/**
* @return: Is animal of breeding age? (T / F)
*/
public boolean canBreed()
{
return getAge() >= getBreedingAge();
}
/**
* Abstract Method referred to by subClasses (Predator / Prey)
*/
public int getBreedingAge()
{
return getBreedingAge();
}
In the Predator Class, I added:
/**
* @return: Returns the breeding age for the Predator
*/
public int getBreedingAge()
{
return BREEDING_AGE;
}
In the Prey Class, I added:
/**
* @return: Returns the breeding age for the Prey
*/
public int getBreedingAge()
{
return BREEDING_AGE;
}
So it can be done, but it took a leap of faith (literally) on my part to set up the abstract method in the abstract Animal Class, with the concrete versions in the respective subclasses.
I am a Java greenhorn (squid? ;) ) but this is pretty interesting to learn. Learning Java is like learning about a newly discovered civilization. You have to learn all of the interactions in the society to understand how everything works. It is quite a challenge too.