DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Experiment 1.3
Student Name: Sushmit Singh UID: 22BCS17177
Branch: CSE Section: IOT_630-B
Semester: 6th DOP: 13/01/25
Subject: Java Subject Code:22CSH-359
Aim: Create an application to calculate interest for FDs, RDs based on certain
conditions using inheritance.
For the description of the project to be implemented, kindly refer to the
Worksheet. Further, the concepts used to implement the project are discussed as
below:
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;
abstract class Deposit {
protected double principal;
protected double rate;
protected double time;
protected int compoundingFrequency;
public Deposit(double principal, double rate, double time, int compoundingFrequency) {
this.principal = principal;
this.rate = rate;
this.time = time;
this.compoundingFrequency = compoundingFrequency;
}
public abstract double calculateInterest();
}
class FixedDeposit extends Deposit {
public FixedDeposit(double principal, double rate, double time, int compoundingFrequency) {
super(principal, rate, time, compoundingFrequency);
}
@Override
public double calculateInterest() {
double r = rate / 100;
int n = compoundingFrequency;
double t = time;
double P = principal;
double A = P * Math.pow(1 + r / n, n * t);
double interest = A - P;
return interest;
}
}
class RecurringDeposit extends Deposit {
public RecurringDeposit(double principal, double rate, double time, int
compoundingFrequency) {
super(principal, rate, time, compoundingFrequency);
}
@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;
}
}
public class DepositInterestCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to the Deposit Interest Calculator!");
System.out.print("Enter deposit type (FD/RD): ");
String depositType = scanner.nextLine().trim().toUpperCase();
System.out.print("Enter principal amount: ");
double principal = scanner.nextDouble();
System.out.print("Enter annual interest rate (%): ");
double rate = scanner.nextDouble();
System.out.print("Enter time in years: ");
double time = scanner.nextDouble();
System.out.print("Enter compounding frequency per year: ");
int compoundingFrequency = scanner.nextInt();
Deposit deposit = null;
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;
}
double interest = deposit.calculateInterest();
System.out.printf("Interest earned: %.2f%n", interest);
scanner.close();
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Output:
Learning Outcomes:
1. Demonstrate: Apply key concepts to real-world scenarios to showcase understanding.
2. Analyze: Critically evaluate information, identify patterns, and draw meaningful
conclusions.
3. Create: Develop original work, including presentations, reports, or projects, to exhibit
comprehension and skills.
4. Communicate: Convey ideas and findings effectively through oral and written
communication.
5. Collaborate: Contribute to group projects and exhibit strong teamwork capabilities in a
collaborative environment.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING