Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

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:

1. Employee: Represents an employee with a name and an employee ID.

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:

 In the main method, create instances of Employee and Department.

 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:

1. Book: Represents a book with attributes title and author.

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:

 In the main method, instances of Book and Library are created.

 Books are added to the library using the addBook method.


 Information about the library and its books is displayed.

-----------------------------------

Task Description: Create a Java program to model a basic hierarchy of animals.(Composition)

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:

 In the main method, instances of Dog and Cat are created.

 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.

 Provides a constructor to initialize these attributes.

2. RAM class:

 Represents a RAM (Random Access Memory) module with attributes like sizeGB and
type.

 Provides a constructor to initialize these attributes.

3. Storage class:

 Represents a storage device (e.g., hard drive, SSD) with attributes like capacityGB and
type.

 Provides a constructor to initialize these attributes.

4. Computer class:

 Represents a computer that aggregates CPU, RAM, and Storage objects.


 Provides a constructor that takes instances of CPU, RAM, and Storage as parameters.

Execution:

 In the main method, create instances of CPU, RAM, and Storage.

 Create a Computer object and pass these instances as parameters to its constructor.

You might also like