java exp 3
java exp 3
Experiment 1.3
Aim: Create an application to calculate interest for FDs, RDs based on certain
conditions using inheritance.
Algorithm:
1. Start
2. Display a welcome message:
"Welcome to the Deposit Interest Calculator!"
3. Prompt the user to enter the deposit type (FD or RD):
"Enter deposit type (FD/RD): "
4. Read the deposit type input from the user.
5. Validate the deposit type:
o If the deposit type is neither FD nor RD, display an error message:
"Invalid deposit type!" and terminate the program.
6. Prompt the user to enter the principal amount:
"Enter principal amount: "
7. Read the principal amount input from the user.
8. Prompt the user to enter the annual interest rate:
"Enter annual interest rate (%): "
9. Read the annual interest rate input from the user.
10. Prompt the user to enter the time in years:
"Enter time in years: "
11. Read the time input from the user.
12. Prompt the user to enter the compounding frequency per year:
"Enter compounding frequency per year: "
13. Read the compounding frequency input from the user.
14. Based on the deposit type:
o If the deposit type is FD:
▪ Create an object of the FixedDeposit class using the provided inputs.
o If the deposit type is RD:
▪ Create an object of the RecurringDeposit class using the provided inputs.
15. Call the calculateInterest() method on the created object to compute the interest.
16. Display the calculated interest:
"Interest earned: <interest>" (rounded to 2 decimal places).
17. End
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Code:
import java.util.Scanner;
@Override
public double calculateInterest() {
double r = rate / 100;
int n = compoundingFrequency;
double t = time;
double P = principal;
@Override
public double calculateInterest() {
double r = rate / 100;
double t = time;
double P = principal;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
double A = P * (Math.pow(1 + r / n, n * t) - 1) / (1 - Math.pow(1 + r / n, -1.0 / 3));
double totalInvestment = P * n * t;
double interest = A - totalInvestment;
return interest;
}
}
if (depositType.equals("FD")) {
deposit = new FixedDeposit(principal, rate, time, compoundingFrequency);
} else if (depositType.equals("RD")) {
deposit = new RecurringDeposit(principal, rate, time, compoundingFrequency);
} else {
System.out.println("Invalid deposit type!");
scanner.close();
return;
}
scanner.close();
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Output:
Learning Outcomes: