0% found this document useful (0 votes)
5 views

Assignment 1-1

Uploaded by

Universalredx
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Assignment 1-1

Uploaded by

Universalredx
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Chinhoyi University of Technology

School of engineering Sciences and Technology

ICT and Electronics Department


Object Oriented Programming
CUITM206

Dr. F. J. Kiwa

Assignment 1
Group of 10 people
1. Inheritance and Polymorphism

Write a program with the following requirements:

∙ Create a base class called Shape with a method getArea() that returns -1.
∙ Create two derived classes, Circle and Rectangle. Override the getArea() method in both to
compute the area based on the class's specific properties.
Challenge: Use a Shape[] array to store objects of Circle and Rectangle. Calculate and print
the total area of all shapes in the array.

2. Abstract Classes and Interfaces

Define an interface Device with methods powerOn() and powerOff().


Create an abstract class Appliance that implements Device and has an additional method
performFunction().

∙ Implement concrete classes WashingMachine and Microwave that inherit Appliance.

∙ Ensure the classes provide appropriate implementations for the performFunction() method.
Question: Why might it be better to use an interface for Device rather than a class?

3. Encapsulation and Mutability

Create a class ImmutablePoint representing a point in a 2D plane. Ensure the class is immutable by:

1. Making all fields private and final.

2. Providing a constructor to set the values.

3. Not providing any setters.

4. Providing getters to access the values.


Challenge: Explain how immutability benefits a multi-threaded program.

4. Exception Handling with Custom Exceptions

Create a custom exception called InvalidBalanceException.

∙ Write a class BankAccount with methods deposit(double amount) and withdraw(double


amount).

∙ If the withdrawal amount exceeds the current balance, throw the InvalidBalanceException.
Task: Write a program to test the exception handling and explain why custom exceptions are
useful.

5. Inner Classes and Static Classes

Write a program that has the following:


∙ An outer class Library with a static nested class Book and a non-static inner class Shelf. ∙

Book should store information like title and author.


∙ Shelf should manage a list of books and allow adding/removing books. Challenge: Explain the
difference between static nested classes and non-static inner classes in this scenario.

You might also like