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
Define the
Petclass:namefield (String type, private)- constructor sets the name
getName()method returns the name
Define the
Ownerclass:petfield (Pet type, private)- Constructor receives a Pet object
introduce()method outputsMy 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