
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Calculate Interest for FDs and RDs using Inheritance
Inheritance is a concept that allows us to access the properties and behaviors of one class in another class.
The class whose methods and member variables are inherited is known as the super class or parent class, and the class that inherits these methods and member variables is known as the child class or subclass. In Java, we use the extends keyword to inherit a class.
Problem Statement
Write Java programs that calculate interest for Fixed Deposits (FDs) and Recurring Deposits (RDs) using inheritance. The program should prompt the user to choose between FD and RD, enter the amount and duration, and calculate the interest based on the age of the account holder.
Calculate Interest for FDs using Inheritance in Java
We will implement the inheritance to calculate interest for FDs. The full form of FD is Fixed deposit. It is a type of investment where a person invest a lump sum of money for a certain amount of time on a pre-determined interest rate.
Example
In the following Java program, we will create an abstract class Acnt to store account details like interest rate and amount. It will also have an abstract method 'calcIntrst()' which will calculate the interest rate.
To calculate the interest rate for Fixed deposits(FDs), we define FDacnt class. It will inherit the Acnt class and override its calcIntrst() method.
abstract class Acnt{ double intrstRate; double amnt; abstract double calcIntrst(double amnt); } class FDacnt extends Acnt { double FDintrstRate; double FDAmnt; int period; int age; double Gen, SenCitizen; @Override double calcIntrst(double amnt){ this.FDAmnt = amnt; period = 325; age = 32; System.out.println("FD Days: " + period); System.out.println("Age: " + age); if (amnt < 10000000) { if (period >= 7 && period <= 14) { Gen = 0.0450; SenCitizen = 0.0500; } else if (period >= 15 && period <= 29) { Gen = 0.0470; SenCitizen = 0.0525; } else if (period >= 30 && period <= 45) { Gen = 0.0550; SenCitizen = 0.0600; } else if (period >= 45 && period <= 60) { Gen = 0.0700; SenCitizen = 0.0750; } else if (period >= 61 && period <= 184) { Gen = 0.0750; SenCitizen = 0.0800; } else if (period >= 185 && period <= 365) { Gen = 0.0800; SenCitizen = 0.0850; } FDintrstRate = (age < 50) ? Gen : SenCitizen; } else { if (period >= 7 && period <= 14) { intrstRate = 0.065; } else if (period >= 15 && period <= 29) { intrstRate = 0.0675; } else if (period >= 30 && period <= 45) { intrstRate = 0.00675; } else if (period >= 45 && period <= 60) { intrstRate = 0.080; } else if (period >= 61 && period <= 184) { intrstRate = 0.0850; } else if (period >= 185 && period <= 365) { intrstRate = 0.10; } } return FDAmnt * FDintrstRate; } } public class Intrst{ public static void main(String[] args){ FDacnt fds = new FDacnt(); double fAmnt = 56000; System.out.println("FD Amount: " + fAmnt); System.out.println("Interest gained on your FD Amount is: " + fds.calcIntrst(fAmnt)); } }
ON running the above Java code, it will display the interest gained on FD:
FD Amount: 56000.0 FD Days: 325 Age: 32 Interest gained on your FD Amount is: 4480.0
Calculate Interest for RDs using Inheritance in Java
Here, we will implement inheritance to calculate interest for RDs. The full form of RD is Recurring Deposit. It is a type of investment where a person invests regular and fixed monthly deposits for a specific period, and earns interest on the accumulated amount.
Example
The following Java program will calculate the interest rate for Recurring deposits(RDs). In it, RDacnt class will inherit the same abstract class Acnt and override its calcIntrst() method.
abstract class Acnt{ double intrstRate; double amnt; abstract double calcIntrst(double amnt); } class RDacnt extends Acnt{ double RDIntrstRate; double RDamnt; int periods; double monthlyAmnt; double Gen, SenCitizen; @Override double calcIntrst(double amnt){ this.RDamnt = amnt; periods = 13; int age = 28; System.out.println("RD months: " + periods); System.out.println("Age: " + age); if (periods >= 0 && periods <= 6) { Gen = .0750; SenCitizen = 0.080; } else if (periods >= 7 && periods <= 9) { Gen = .0775; SenCitizen = 0.0825; } else if (periods >= 10 && periods <= 12) { Gen = .0800; SenCitizen = 0.0850; } else if (periods >= 13 && periods <= 15) { Gen = .0825; SenCitizen = 0.0875; } else if (periods >= 16 && periods <= 18) { Gen = .0850; SenCitizen = 0.0900; } else if (periods >= 22) { Gen = .0875; SenCitizen = 0.0925; } RDIntrstRate = (age < 50) ? Gen : SenCitizen; return RDamnt * RDIntrstRate; } } public class Intrst{ public static void main(String[] args){ RDacnt rds = new RDacnt(); double RAmnt = 5600; System.out.println("RD amount: " + RAmnt); System.out.println("Interest gained on your RD Amount is: " + rds.calcIntrst(RAmnt)); } }
The above code will print the interest gained on the RD amount.
RD amount: 5600.0 RD months: 13 Age: 28 Interest gained on your RD Amount is: 462.0