Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Description: In this activity you will combine what you've learned about object casting, inheritance, and working with collections. You will create a family with grandparents,

image text in transcribedimage text in transcribedimage text in transcribed

Description: In this activity you will combine what you've learned about object casting, inheritance, and working with collections. You will create a family with grandparents, parents who inherit from grandparents, and children who inherit from the parents. Please follow the steps below: Steps: 1. Add in the following code into Person.java: public abstract class Person \{ public abstract void walk(); \} The purpose of this abstract class is to be the root of the inheritance chain we will create. It defines the walk0 method. This means any subclass is guaranteed to have this method through inheritance. The reason it is abstract is because almost all people have the ability to walk. So we want to be able to implement it differently depending on what type of object we are creating. 2. Add in the following code into Grandparent.java: class GrandParent extends Person \{ String name; public GrandParent(String name) \{ this.name = name; \} aoverride public void walk()\{ System.out.println(this.name + "staggers along"); \} Notice that this class defines a name instance variable, a parameterized constructor, and a walk0 method. The parameterized constructor takes in a String value and assigns it to the name variable. The inherited walk0 method is overridden and prints the object's name concatenated to a String to the console. 3. Create a Parent class in Parent.java. The class extends the GrandParent class and should have the following: - an instance variable of the type String named energy. - a parameterized constructor that takes in two parameters: String name and String energy. The constructor should first call super (name) to initialize its name and then in another statement, it should initalize its energy instance variable using the this keyword. - an overridden walk0 method that prints the following to the console: this. name + "walks normally with " + this.energy + " energy" 4. Create a Child class in Child.java. The class extends the Parent class and should have the following: - an instance variable of the type String named diaperBrand. - a parameterized constructor that takes in three parameters: String name, String energy, String diaperBrand. The constructor should first call super (name, energy ) to initialize its name and energy and then in another statement, it should initalize its diaperBrand instance variable using the th is keyword. - an overridden walk0 method that prints the following to the console: this. name + "crawls forward with " + this.energy + " energy while wearing " + this.diaperBrand + " diapers" 5. Create a FamilyTree.java file. 6. Create a method called addPeople. - create an Arraylist called family that can store Person objects. Subtype objects can be added to this list because of the IS-A relationship that we created with inheritance: A child is a Person, a Parent is a Person, and so on. - Below what you have so far in the addPeople method, create The following objects: - a GrandParent object with the name Mary - a GrandParent object with the name John - a Parent object with the name Amy who has medium energy - a Parent object with the name Andy who has low energy - a Child object with the name Annie who has high energy and uses branded diapers. - Below what you have so far in the addPeople method, add all these new family members to the family list. 7. To test what we've done so far, let's iterate over the list of family members and have every member walk. - In the addPeople method, below what you have created so far, create a for-each loop that iterates over the Person type objects in our ArrayList called family. - In the body of the for-each loop, invoke the walk( ) method on each Person object from the ArrayList // syntax of a for-each loop: for(Type variableName : collectionName)\{ //code for behavior variableName.methodName( ); Note: this code is an example of what a for-each loop looks like you are responsible for figuring out the logic the loop needs to follow. Run the program and note the output. We successfully added subtype objects into a parent type collection. We also iterated over the collection and for every object, we called the walk0 method, which every object is guaranteed to have. Test: Use the test provided. Sample output: Mary staggers along John staggers along Amy walks normally with medium energy Andy walks normally with low energy Annie crawls forward with high energy while wearing Johnson diapers

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions