0% found this document useful (0 votes)
2 views2 pages

Exercises

Exercises

Uploaded by

esin tan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Exercises

Exercises

Uploaded by

esin tan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Question 1: Bank Account Management

Design an abstract class called “BankAccount” that declares an abstract method “withdraw(double
amount)” and has data members “accountNumber (String)” and “balance (double)”. Include a concrete
method “deposit(double amount)” that adds to the balance.

Create two concrete subclasses: “SavingsAccount” that prevents withdrawals exceeding the balance, and
“CheckingAccount” that allows overdrafts up to $500. Both should implement “withdraw()”
appropriately. Do not forget to implement a parameterized constructor and proper set/get methods.

In your main method:

a) Create a list containing one SavingsAccount and one CheckingAccount with initial balances.

b) Demonstrate polymorphism by processing withdrawals from both account types. Get the withdrawal
amount from the user.

c) Create a custom exception InsufficientFundsException and throw and catch when withdrawals fail.

d) Include “finally” blocks to print transaction completion messages.

Question 2: Geometric Shapes Calculator


Design an abstract base class “Shape” with:

●​ A data member “color” (String)


●​ Abstract methods “calculateArea()” and “calculatePerimeter()”
●​ A concrete method “displayColor()” that prints the shape's color

Create three concrete subclasses, do not forget to implement a parameterized constructor and proper
set/get methods:

●​ Circle with “radius” (double)


●​ Rectangle with “length” and “width” (double)
●​ Triangle with “base”, “height”, and three “sides” (double)


In main():

a) Create an ArrayList containing one of each shape type

b) Calculate and print areas and perimeters. Demonstrate run-time polymorphism.

c) Implement a custom InvalidDimensionException. If a negative value is entered for data attributes,


throw your custom exception from “set” methods.
Question 3: Employee Payment System
Create an abstract base class Employee with:

●​ Data members “name (String)” and “id (int)”.


●​ An abstract method “calculateSalary()”.
●​ A Concrete method “displayInfo()” that prints the data members and salary.


Create interfaces:

●​ “BonusEligible” with the “calculateBonus()” method.


●​ “Taxable” with the “calculateTax()” method.
●​ “Reportable” with the “generateReport()” method but makes it default. It calls the “displayInfo()”
and all subclasses implement this interface.


Implement three classes; do not forget to implement a parameterized constructor and proper set/get
methods:

●​ “FullTimeEmployee” implements “BonusEligible” and has following data members:


“salary(double)” and “bonus(double)”. “calculateSalary()” returns the
“salary”+“calculateBonus()”. ​
“calculateBonus()” asks user to enter extra hours and returns “extra*10”.​

●​ “PartTimeEmployee” with following data members: “hourly_wage(double)”.​


“calculateSalary()” asks user to enter the number of hours and returns the “hourly_wage*hours”​

●​ “Contractor” implements “Taxable” and has one data member: “fixedPayment(double)”.


“calculateSalary()” returns the “fixedPayment-calculateTax()”. ​
“calculateTax()” returns “fixedPayment*0.3”.


In main():

a) Create different employee types and store them in an ArrayList.

b) Calculate and display salaries.

c) Create and handle custom exception:

●​ “InvalidPaymentException” for negative values

d) Generate reports for all employees

You might also like