Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this exercise, we'll open a previously created project and further explore IS-A vs. HAS-A. Project Setup Open your IDE (Eclipse). Right-click on the Java

In this exercise, we'll open a previously created project and further explore IS-A vs. HAS-A.

Project Setup

  1. Open your IDE (Eclipse).
  2. Right-click on the Java Project, Lab-Inheritance and Select Copy.

image text in transcribed

3. Right-click anywhere in the blank space inside of the Package Explorer panel and select Paste.

image text in transcribed

4. Name the new project, Lab-HAS-A.

image text in transcribed

5. Create a new class Outfit within the com.revature.model package.

6. Within this Outfit class, create a single method, wear().

This method will not have any parameters nor will it return anything. It will simply print out. "I look fantastic".

Your new class should look like the following:

package com.revature.model; public class Outfit { public void wear() { System.out.println("I look fantastic"); } } 

5. Open the file for your Doll class.

Add in a single Outfit field.

By including the line:

Outfit outfit = new Outfit(); 

Now, take note of the class's structure.

What are the relationships that are HAS-A and which ones are IS-A?

image text in transcribed

Note that a Doll HAS-AN Outfit and a Doll IS-A Toy.

Edit the Doll's makeTalk() method to call Outfit's wear() method as its first statement:

 public void makeTalk() { this.outfit.wear(); System.out.println("Hi everyone! I AM-A doll"); }

Great. Now edit the same method to call the inherited play() method as the last statement.

 public void makeTalk() { this.outfit.wear(); System.out.println("Hi everyone! I AM-A doll"); this.play() }

Notice the difference between how we call Outfit's wear() method versus Toy's play() method. We have to refer to Doll's instance of Outfit first and then call the wear() method, whereas we can directly call Toy's play() method because Doll has inherited it.

This completes the exercise. It is a subtle detail, but an important one in understanding how classes relate to one another.

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_2

Step: 3

blur-text-image_3

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

Seven NoSQL Databases In A Week Get Up And Running With The Fundamentals And Functionalities Of Seven Of The Most Popular NoSQL Databases

Authors: Aaron Ploetz ,Devram Kandhare ,Sudarshan Kadambi ,Xun Wu

1st Edition

1787288862, 978-1787288867

More Books

Students also viewed these Databases questions

Question

8. Describe how cultural spaces are formed.

Answered: 1 week ago