Professional Documents
Culture Documents
Practise Questions
Practise Questions
Task: Create a Java program to represent a Department and Employee using aggregation.
Task Description: Create a Java program that models a Department and an Employee relationship using
aggregation.
Classes:
2. Department: Represents a department with a name and a department head, where the
department head is an employee.
Aggregation:
The Department class aggregates the Employee class by having a department head that is an
instance of the Employee class.
The setDepartmentHead method in the Department class sets the department head.
The getDepartmentHead method in the Department class retrieves the department head.
Execution:
Set the department head for the department using the setDepartmentHead method.
Display the department's name and the name of the department head.
------------------------
Task Description: Create a Java program to model a Library and Book using aggregation.
Classes:
2. Library: Represents a library with attributes name, an array of books, and bookCount to keep
track of the number of books in the library.
Aggregation:
The Library class aggregates the Book class by having an array of books (Book[] books) as one of
its attributes.
The addBook method in the Library class allows you to add books to the library.
Execution:
-----------------------------------
Classes:
1. Animal: This is the base class that represents an animal. It has attributes name and sound. It
also has a method makeSound() to print the sound of the animal.
2. Dog: This is a subclass of Animal that represents a dog. It sets the name and sound attributes
specific to dogs in its constructor.
3. Cat: This is another subclass of Animal that represents a cat. It sets the name and sound
attributes specific to cats in its constructor.
Execution:
The makeSound() method is called on each instance, which produces the sound of the
respective animal.
----------------------------------
Classes:
1. CPU class:
Represents a central processing unit (CPU) with attributes like manufacturer and speed.
2. RAM class:
Represents a RAM (Random Access Memory) module with attributes like sizeGB and
type.
3. Storage class:
Represents a storage device (e.g., hard drive, SSD) with attributes like capacityGB and
type.
4. Computer class:
Execution:
Create a Computer object and pass these instances as parameters to its constructor.