One of the topics covered this week is inheritance the


Inheritance

One of the topics covered this week is "Inheritance". The chapter shows the Person/Employee example, what are some other examples that could use Inheritance? Read section 12.3-12.4 and come up with your own example of inheritance. Do not copy the examples from the textbook or other sources. Explain your example in a paragraph or two, longer if you need to. After you do that, you must show how the inheritance relation will be reflected in Java code. In other words, you are not required to write a complete set of classes and their contents, but at the minimum you must show the inheritance relation between the various classes that make up your example.

Directions

The files must be called cLnFnUnitS.java> (driver program) LnFnAnimal.java

LnFnFox.java (which extends LnFnAnimal) LnFnChicken.java (which extends LnFnAnimal)

The files must be called as specified above, (LnFn = Your Last Name Your First Name. For example the Instructor will create GuillermoTonsmannFox.java and others similarly.)

Proper coding conventions required the first letter of the class start with a capital letter and the first letter of each additional word start with a capital letter.

Only submit the Java files needed to make the program run. Do not submit the .class files or any other files.

Basic Requirements and Driver class

Use this class hierarchy: Fox and Chicken are children classes of the Animal class. A Fox

• Will kill 1 chicken a day
• Does not reproduce

A Chicken

• Have a chance to reproduce as long as conditions are met
• Reproduction only happens when chickens are over 1 and 1 of each sex is present

The simulation control inside the main method on LnFnUnit5 will do the following:

• Simulation will continue as long as chicken population is greater than 1 and less than or equal 10 (if 1 or less, mating can't happen. If > 10, chickens will overrun the fox)

Driver main method should be as shown below: (replacing comment with missing output piece and replacing LnFn with your initials. Add prologue and additional comments to explain functionality.)

import java.util.ArrayList;
public class LnFnUnit5 {
public static void main(String args) { for(int count=0; count<10; count++) { LnFnFox foxy = new LnFnFox();
ArrayList< LnFnChicken > chickens = new ArrayList();

chickens.clear();

chickens.add(new LnFnChicken()); chickens.add(new LnFnChicken 0);

chickens.add(new LnFnChicken 0); chickens.get(0).setSex(true);
chickens.get(1).setSex(false);
chickens.get(2).setSex(false);
while (chickens.size() >1 && chickens.size() < 10) { for (LnFnChicken c:chickens) c.grow();
foxy.grow();
LnFnChicken.mate(chickens); foxy.eat(chickens);
}
//INCLUDE CODE FOR OUTPUT HERE. }II end of for
} // end of main
} // end of class

The program will output one of the following messages:

Chickens win - Chicken Population: 14

(if chicken population is greater than or equal to 10, t/c/4 is an integer value) OR

Fox wins - Fox Weight (in chickens): #14.144

(if chicken population is less than 10, *kg* is a double value, 2 decimal places)

Because the output will be inside the main for loop, the output will be repeated 10 times. See sample output below.

Remember to use get and set methods in all classes whenever you want to obtain (get) or change (set) any of the attributes of your objects.

LnFnAnimaljava class

The Animal class will have the following attributes:

• name

age (an integer number that represents the days of age)

weight (a double number that represents a weight in pounds)

maleSex (a boolean variable that is true, if the Animal is male, false otherwise)

All attributes should be private and each one should have public appropriate accessor (get) and mutator (set) methods.

A default constructor will initialize the age of every Animal to 1 day.

The Animal class should also have a method named grow with no parameters that increases the age of the Animal by one.

LnFnFox.java class

LnFnFox constructor: (default constructor)

It sets the fox's age to a random number between 400 to 900 days.

It sets the fox's weight to a random number between 30 to 60 pounds. It randomly sets the fox's sex.

The Fox class will include the method named eat to do the following: eat method: It receives an Arraylist of Chicken objects as argument.

It randomly removes a chicken from the population 70% of the time and increases fox weight by the chosen chicken weight. It only increases the fox's weight if the chicken is removed/eaten.

LnFnChicken.java class

LnFnChicken constructor: (default constructor)

It sets the chicken's age to 1 day and the chicken's weight to 1 pound.

It randomly sets the chicken's sex.

The Chicken class will include the following methods:

grow method:

It overrides the grow method from the Animal class to increase the chicken's age by 1 and the chcicken's weight by 1% of its current weight.
mate method: An static method that receives an Arraylist of Chicken objects as argument.

It randomly chooses 2 Chicken objects from the Arraylist and if conditions are correct, it proceeds with mating.
Successful mating conditions are:

- 1 male and 1 female chicken

- Both chickens older than 1 day

§ If successful mating occurs, create a random number (between 0 to 4 ) of new Chicken objects, and append them to the Arraylist received as argument.

Solution Preview :

Prepared by a verified Expert
Management Information Sys: One of the topics covered this week is inheritance the
Reference No:- TGS01365339

Now Priced at $20 (50% Discount)

Recommended (97%)

Rated (4.9/5)