Simple Interest Program in Java using Methods | Inheritance
Simple interest program in java using methods and inheritance.
In this article, you will learn how to make a simple interest program in java using methods and inheritance.
Example
Enter the principal (amount), time, and rate::
5
7
9
Simple Interest = 3.15
You should have the knowledge of the following topics in java programming to understand these programs:
- Java
java.util.Scanner
package - Java
main()
method - Java Methods
- Java Inheritance
Simple Interest Formula in Java
SI = (P * T * R) / 100
Where
P = principal amount
T = time
R = rate
SI = simple interest
In this article, we solve this problem in three methods:
Source Code
// Simple Interest Program in Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the principal (amount), time, and rate::\n");
float p = in.nextFloat(); // value of the principal
float t = in.nextFloat(); // value of the time
float r = in.nextFloat(); // value of the rate
float SI; // value of the simple interest
// It will calculate the simple interest
SI = (p * t * r) / 100;
// It will produce the final output
System.out.println("\nSimple Interest = " + SI);
}
}
Output
Enter the principal (amount), time, and rate::
5
7
9
Simple Interest = 3.15
Explanation
In this given program, we have taken inputs Amount, Time, and Rate amount following: 5
, 7
, and 9
from the user via the system console. Then we applied the standard formula to calculate simple interest.
Then this program will return the 3.15
simple interest value of the above calculation.
Simple Interest Program in Java using Methods
// Simple Interest Program in Java using Methods
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the principal (amount), time, and rate::\n");
float p = in.nextFloat(); // value of the principal
float t = in.nextFloat(); // value of the time
float r = in.nextFloat(); // value of the rate
CalculateSimpleInterest(p, t, r);
}
// This method will calculate the simple interest
public static void CalculateSimpleInterest(float x, float y, float z) {
float SI; // Value of the simple interest
// It will calculate the simple interest
SI = (x * y * z) / 100;
// It will produce the final output
System.out.println("\nSimple Interest = " + SI);
}
}
Simple Interest Program in Java using Inheritance
// Simple Interest Program in Java using Inheritance
import java.util.Scanner;
// This class will calculate the simple interest
class CalcSimpleInterest {
public float CalculateInterest(float x, float y, float z) {
return (x * y * z) / 100;
}
}
// This class will inherit `CalcSimpleInterest` class and print the simple interest
class PrintSimpleInterest extends CalcSimpleInterest {
public void PrintInterest(float SI) {
System.out.println("\nSimple Interest = " + SI);
}
}
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the principal (amount), time, and rate::");
float p = in.nextFloat(); // value of the principal
float t = in.nextFloat(); // value of the time
float r = in.nextFloat(); // value of the rate
PrintSimpleInterest SimpleInterest = new PrintSimpleInterest();
// It will calculate the simple interest
float SI = SimpleInterest.CalculateInterest(p, t, r);
// It will produce the final output
SimpleInterest.PrintInterest(SI);
}
}
Output
Enter the principal (amount), time, and rate::
54
67
90
Simple Interest = 3256.2
Simple Interest Program in Java using Constructor
// Simple Interest Program in Java using Constructor
import java.util.Scanner;
// It's the main constructor class
public class Main {
Main() { }
void CalculateSimpleInterest(float a, float b, float c) {
float SI; // value of the simple interest
// It will calculate the simple interest
SI = (a * b * c) / 100;
System.out.println("\nSimple Interest = " + SI);
}
}
class MainClass {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Main M = new Main();
System.out.println("Enter the principal (amount), time, and rate::\n");
float p = in.nextFloat(); // value of the principal
float t = in.nextFloat(); // value of the time
float r = in.nextFloat(); // value of the rate
// It will produce the final output
M.CalculateSimpleInterest(p, t, r);
}
}
You should also read these things
Java Program to Check Even or Odd Number using If-Else Statement | Ternary Operator | Function
Find Factors of a Number in Java using While loop | For | Recursion | Functions
Java Program to Enter Marks of Five Subjects and Calculate Total, Percentage, and Grade