Java Eclipse

Description

bro I have a money issue, just give me a favor on this assignment you know,

Don't use plagiarized sources. Get Your Custom Assignment on
Java Eclipse
From as Little as $13/Page

Unformatted Attachment Preview

OOPDA Lab07B — move age [Adapted from Barnes & Kolling, BlueJ 6th ed, ch 12, ex 12.44-12.48]
➔Use foxes-and-rabbits-v2

this version has made Animal abstract with an abstract act() method
– Note that any class with an abstract method is an abstract class,
even if class is not explicitly declared “abstract”
• Briefly, examine the implementation of act() for each subclass
When we created the Animal superclass in our last lab, we did this by identifying common
elements of the subclasses, but we chose not to move the age field and the methods associated
with it. This might be overly conservative. We could have easily moved the age field to Animal
and provided for it there an accessor and a mutator that were called by subclass methods, such
as incrementAge. Why didn’t we move incrementAge and canBreed into Animal ?
The reason for not moving these is that, although several of the remaining method bodies in
Fox and Rabbit contain textually identical statements, their use of class variables with different
values means that they cannot be moved directly to the superclass.
In the case of canBreed, the problem is the BREEDING_AGE variable, while breed depends on
BREEDING_ PROBABILITY and MAX_ LITTER _SIZE . If canBreed is moved to Animal , for
instance, then the compiler will need to have access to a value for the subtype -specific
breeding age in class Animal. It is tempting to define a BREEDING_AGE field in the Animal
class and assume that its value will be overridden by similarly named fields in the subclasses .
However, fields are handled differently from methods in Java: they cannot be overridden by
subclass versions. This means that a canBreed method in Animal would use a meaningless
value defined in that class, rather than one that is specific to a particular subclass.
But, the fact that the field’s value would be meaningless gives us a clue as to how we can get
around this problem and, as a result, move more of the similar methods from the subclasses
to the superclass.
We defined act as abstract in Animal because having a body for the method
would be meaningless. If we access the breeding age with a method rather than a field, we
can get around the problems associated with the age-dependent properties. [So, the superclass
method can access the subclass data!] This approach is shown in the code below
/**
* An animal can breed if it has reached the breeding age.
* @return true if the animal can breed
*I
public boolean canBreed()
{
return age>= getBreedingAge();
}
/**
* Return the breeding age of this animal .
* @return The breeding age of this animal.
*/
abstract protected int getBreedingAge(); //Prof W: this is sooo cool!!
OOPDA Lab7B SP24 move age
p. 1
2/28/2024 5:55 PM
The canBreed method should be moved to Animal and rewritten to use the value returned
from a method call rather than the value of a class variable. For this to work, a method
getBreedingAge must be defined in class Animal . Because we cannot specify a breeding
age for animals in general, we can again use an abstract method in the Animal class
and concrete redefinitions in the subclasses. Both Fox and Rabbit will define their own
versions of getBreedingAge to return their particular values of BREEDING _AGE:
/**
* @return The age at which a rabbit starts to breed.
*I
public int getBreedingAge()
{
return BREEDING_AGE;
}
So, even though the call to getBreedingAge originates in the code of the superclass, the
method called is defined in the subclass. This may seem mysterious at first but it is based
on the same principles previously described using the dynamic type of an object
to determine which version of a method is called at runtime. The technique illustrated here
makes it possible for each instance to use the value appropriate to its subclass type. Using
the same approach, we can move the remaining methods, incrementAge and breed, to
the superclass.
1. Create a WORD doc called Lab7B_LOG for screen shots and notes to document testing
and screen shots as you proceed. [Don’t get carried away, as we have limited time] At start
of the lab, note names of other students who collaborated in your group.
2. Using the foxes-and-rabbits-v2 project, record the number of foxes and rabbits over a small
number of steps, to prepare for regression testing of the changes to follow.
3. Move the age field from Fox and Rabbit to Animal . Initialize it to zero in the constructor.
Provide accessor and mutator methods for it and use these in Fox and Rabbit rather
than in direct accesses to the field. Make sure the program compiles and runs as before .
4.
Move the canBreed method from Fox and Rabbit to Animal, and rewrite it as shown in the
code listing above. Provide appropriate versions of getBreedingAge in Fox and Rabbit that
return the distinctive breeding age values.
5.
Move the incrementAge method from Fox and Rabbit to Animal by providing an abstract
getMaxAge method in Animal and concrete versions in Fox and Rabbit.
6.
Can the breed method be moved to Animal? If so, make this change .
7.
In light of all the changes you have made to these three classes, reconsider the visibility of
each method and make any changes you feel are appropriate. Summarize your findings
and the changes you made in the log document.
Deliverable: zip of Eclipse project folder and your Log file with screen shots and notes.
OOPDA Lab7B SP24 move age
p. 2
2/28/2024 5:55 PM

Purchase answer to see full
attachment