0% found this document useful (0 votes)
10 views3 pages

Assignment 02

The document outlines a programming assignment that requires writing recursive functions in Java for summing digits, reversing a string, and checking if a number is even using indirect recursion. It also details the creation of a Bank Account Management System with exception handling for deposit, withdrawal, and balance checks, including a custom exception for insufficient funds. The submission guidelines include delivering source code and a README file, with grading criteria focusing on correctness and code quality.

Uploaded by

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

Assignment 02

The document outlines a programming assignment that requires writing recursive functions in Java for summing digits, reversing a string, and checking if a number is even using indirect recursion. It also details the creation of a Bank Account Management System with exception handling for deposit, withdrawal, and balance checks, including a custom exception for insufficient funds. The submission guidelines include delivering source code and a README file, with grading criteria focusing on correctness and code quality.

Uploaded by

Abdul Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Recursion

Write recursive code for the following. You can write all 3 functions in a single Java file
1. A function that returns the sum of the digits of a given integer
a. Signature: public static int sumOfDigits(int n)
b. Example:
i. Input: 1234
ii. Output: 10
2. A function to reverse a string
a. Signature: public static String reverseString(String str)
b. Example:
i. Input: “Java”
ii. Output: “avaJ”
3. Write 2 functions using ‘Indirect recursion’ to check if a given integer is Even
a. Signature public static boolean isEven (int n)
b. Example:
i. Input: 3
ii. Output: False

Exception handling
Create a simple Bank Account Management System where users can deposit, withdraw, and
check their balance. The system should handle exceptions that may arise during these
operations.

Requirements
Create a BankAccount class that models a simple bank account with the following attributes
and methods:

Class BankAccount:
● Attributes:
○ accountNumber: A unique identifier for the account (int).
○ accountHolderName: The name of the account holder (String).
○ balance: The current balance in the account (double).
● Methods:
○ Constructor:
■ Initializes the account with an account number, holder name, and initial
balance.
■ Ensure the initial balance is not negative.
■ Throws: IllegalArgumentException if initial balance is negative.
■ Method signature: public BankAccount(int accountNumber, String
accountHolderName, double initialBalance)
○ deposit(double amount):
■ Adds the specified amount to the account balance.
■ Ensure the amount is positive.
■ Throws: IllegalArgumentException if the deposit amount is negative or
zero.
■ Method signature: public void deposit(double amount)
○ withdraw(double amount):
■ Subtracts the specified amount from the account balance.
■ Ensure the withdrawal amount does not exceed the current balance and
is positive.
■ Throws:
● InsufficientFundsException (a custom exception) if the withdrawal
amount exceeds the current balance.
● IllegalArgumentException if the withdrawal amount is negative or
zero.
■ Signature: public void withdraw(double amount) throws
InsufficientFundsException

Custom Exception: InsufficientFundsException

Create a custom exception called InsufficientFundsException that is thrown when an attempt is


made to withdraw more than the available balance.

Main program
Create a main method to simulate the following scenarios and handle exceptions appropriately:
● Valid Scenario:
○ Create an account with a valid initial balance.
○ Deposit a positive amount and withdraw an amount within the balance.
● Error Scenarios:
○ Try creating an account with a negative balance.
○ Try depositing a negative or zero amount.
○ Try withdrawing an amount greater than the current balance.
○ Handle and print appropriate error messages for all exceptions.
Additional instructions
● After each exception scenario, the program should continue running and allow the user
to make further transactions.

Submission guidelines
1. Deliverables
a. Source code (.java files). Please don’t submit rar or zip files.
b. A text or markdown file (README.md) describing:
i. Your approach to solving the problem.
ii. Any additional information you want me to be aware of
2. Grading Criteria:
a. Correctness: The program should work according to the requirements.
b. Code Quality: Code should be well-structured, clean, and properly
commented.
3. You can use any IDE for writing the code.
4. Make sure your code compiles and runs without errors before submission.
5. This is an individual assignment. Discussions are allowed and encouraged,
copying someone else’s work is not.
6. Stay away from AI tools while attempting the assignment.

You might also like