007-004-008

Pet Owner Management

Medium

Problem Description

Pet Owner Management

In this problem, you will implement an aggregation relationship where an Owner class holds a Pet class instance.

Learning Objective: Understand aggregation relationship between classes (one class having another class)

Overview

Create Pet and Owner classes where Owner has a Pet. Learn the concept of "aggregation" where one object references another object.

Specifications

  1. Define the Pet class:

    • name field (String type, private)
    • constructor sets the name
    • getName() method returns the name
  2. Define the Owner class:

    • pet field (Pet type, private)
    • Constructor receives a Pet object
    • introduce() method outputs My pet is {pet name}

Output Example

When Pet name is "Max":

My pet is Max

Hints

  • A class can have another class as a field
  • Receive objects in constructors and store them in fields

Ready to Try Running Code?

Log in to access the code editor and execute your solutions for this problem.

Don't have an account?

Sign Up