


Calculate interest on fixed deposits (FDs) and fixed deposits (RDs) using inherited Java program
Inheritance is a concept that allows us to access the properties and behavior of one class from another class. The class that inherits methods and member variables is called a superclass or parent class, and the class that inherits these methods and member variables is called a subclass or subclass. In Java, we use "extends" keyword to inherit a class.
In this article, we will discuss a Java program to calculate interest on term deposits and time deposits using inheritance. First, create these four Java files in your local machine IDE -
Acnt.java − This file will contain an abstract class ‘Acnt’ used to store account details such as interest rate and amount. It will also have an abstract method 'calcIntrst' with parameter 'amnt' for calculating the interest rate.
FDacnt.java − It will calculate the interest rate on Fixed Deposits (FDs). In it, 'FDacnt' class will inherit 'Acnt' class and override 'calcIntrst' method.
RDacnt.java − It will calculate the interest rate on Fixed Deposits (FDs). In it, 'RDacnt' class will inherit 'Acnt' class and override 'calcIntrst' method.
Intrst.java − This file will contain the main method.
Example
Acnt.java
public abstract class Acnt{ double intrstRate; double amnt; abstract double calcIntrst(double amnt); }
FDacnt.java
import java.util.*; public class FDacnt extends Acnt { double FDintrstRate; double FDAmnt; int period; int age; double Gen, SenCitizen; Scanner input = new Scanner(System.in); @Override double calcIntrst(double amnt){ this.FDAmnt = amnt; System.out.println("Enter your FD days"); period = input.nextInt(); System.out.println("Enter the age of account holder "); age = input.nextInt(); 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; } }
RDacnt.java
is:RDacnt.java
import java.util.*; public class RDacnt extends Acnt{ double RDIntrstRate; double RDamnt; int periods; double monthlyAmnt; double Gen, SenCitizen; Scanner input = new Scanner(System.in); @Override double calcIntrst(double amnt){ this.RDamnt = amnt; System.out.println("Enter your RD months"); periods =input.nextInt(); System.out.println("Enter the age of account holder"); int age =input.nextInt(); 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; } }
Intrst.java
is:Intrst.java
import java.util.*; public class Intrst{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.println("Choose from the Options: " + "\n1." + " FD Interest" + " \n2." + " RD Interest" + "\n3." + " Exit"); int choice = sc.nextInt(); switch (choice){ case 1: FDacnt fds = new FDacnt(); System.out.println("Enter your FD Amount"); double fAmnt = sc.nextDouble(); System.out.println("Interest gained on your FD Amount is: $ " + fds.calcIntrst(fAmnt)); break; case 2: RDacnt rds = new RDacnt(); System.out.println("Enter your RD amount"); double RAmnt = sc.nextDouble(); System.out.println("Interest gained on your RD Amount is: $ " + rds.calcIntrst(RAmnt)); break; default: System.out.println("Choose correct choice"); } } }
To compile this code, type the following command: javac Intrst.java
Now to run: java IntrstOutput
Choose from the Options: 1. FD Interest 2. RD Interest 3. Exit 1 Enter your FD Amount 56000 Enter your FD days 325 Enter the age of account holder 32 Interest gained on your FD Amount is: $ 4480.0
The above program is a menu driver. When we execute the program, three options will appear on the screen. Select 1 to calculate the interest rate for time deposits, and select 2 to calculate the interest rate for time deposits.
in conclusion
We use the concept of hierarchical inheritance to calculate fixed deposits and fixed deposit interest. Class "Acnt" is inherited by its two subclasses "FDacnt" and "RDacnt".
The above is the detailed content of Calculate interest on fixed deposits (FDs) and fixed deposits (RDs) using inherited Java program. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In function inheritance, use "base class pointer" and "derived class pointer" to understand the inheritance mechanism: when the base class pointer points to the derived class object, upward transformation is performed and only the base class members are accessed. When a derived class pointer points to a base class object, a downward cast is performed (unsafe) and must be used with caution.

Please consider the table below to know the eligibility criteria of different companies - The Chinese translation of CGPA is: GPA greater than or equal to 8 Eligible companies Google, Microsoft, Amazon, Dell, Intel, Wipro greater than or equal to 7 Tutorial points, accenture, Infosys , Emicon, Rellins greater than or equal to 6rtCamp, Cybertech, Skybags, Killer, Raymond greater than or equal to 5Patronics, Shoes, NoBrokers Let us enter the java program to check the eligibility of tpp students for interview. Method 1: Using ifelseif condition Normally when we have to check multiple conditions we use

The Java language is one of the most commonly used object-oriented programming languages in the world today. The concept of classes is one of the most important features of object-oriented languages. A class is like a blueprint for an object. For example, when we want to build a house, we first create a blueprint of the house, in other words, we create a plan that shows how we are going to build the house. According to this plan we can build many houses. Likewise, using classes, we can create many objects. Classes are blueprints for creating many objects, where objects are real-world entities like cars, bikes, pens, etc. A class has the characteristics of all objects, and the objects have the values of these characteristics. In this article, we will write a Java program to find the perimeter and faces of a rectangle using the concept of classes

Inheritance and polymorphism affect the coupling of classes: Inheritance increases coupling because the derived class depends on the base class. Polymorphism reduces coupling because objects can respond to messages in a consistent manner through virtual functions and base class pointers. Best practices include using inheritance sparingly, defining public interfaces, avoiding adding data members to base classes, and decoupling classes through dependency injection. A practical example showing how to use polymorphism and dependency injection to reduce coupling in a bank account application.

The size of a file is the amount of storage space that a specific file takes up on a specific storage device, such as a hard drive. The size of a file is measured in bytes. In this section, we will discuss how to implement a java program to get the size of a given file in bytes, kilobytes and megabytes. A byte is the smallest unit of digital information. One byte equals eight bits. One kilobyte (KB) = 1,024 bytes, one megabyte (MB) = 1,024KB, one gigabyte (GB) = 1,024MB and one terabyte (TB) = 1,024GB. The size of a file usually depends on the type of file and the amount of data it contains. Taking a text document as an example, the file size may be only a few kilobytes, while a high-resolution image or video file may be

Inheritance error debugging tips: Ensure correct inheritance relationships. Use the debugger to step through the code and examine variable values. Make sure to use the virtual modifier correctly. Examine the inheritance diamond problem caused by hidden inheritance. Check for unimplemented pure virtual functions in abstract classes.

Encapsulation technology and application encapsulation in PHP is an important concept in object-oriented programming. It refers to encapsulating data and operations on data together in order to provide a unified access interface to external programs. In PHP, encapsulation can be achieved through access control modifiers and class definitions. This article will introduce encapsulation technology in PHP and its application scenarios, and provide some specific code examples. 1. Encapsulated access control modifiers In PHP, encapsulation is mainly achieved through access control modifiers. PHP provides three access control modifiers,

Detailed explanation of C++ function inheritance: Master the relationship between "is-a" and "has-a" What is function inheritance? Function inheritance is a technique in C++ that associates methods defined in a derived class with methods defined in a base class. It allows derived classes to access and override methods of the base class, thereby extending the functionality of the base class. "is-a" and "has-a" relationships In function inheritance, the "is-a" relationship means that the derived class is a subtype of the base class, that is, the derived class "inherits" the characteristics and behavior of the base class. The "has-a" relationship means that the derived class contains a reference or pointer to the base class object, that is, the derived class "owns" the base class object. SyntaxThe following is the syntax for how to implement function inheritance: classDerivedClass:pu
