Starting a series of some Java programs as many students were asking to share the programs. Here is the first program of the series.
Objectives:
- Write a Program that calculates and prints the simple interest using the formula : Simple Interest = PTR/100
- How to calculate Simple Interest in Java?
- Write a program to calculate Simple Interest in Java.
- Write a Java program that calculates and prints the simple interest using the formula : “SimpleInterest = PTR/100” and input values P, T, R should be accepted as command line.
- How to use Command Line Arguments in Java?
- How to pass values from command line in Java?
- How to take input in Java from console?
This particular program can be written in various ways, we will try to write the solution program in different ways here and one can try to understand the difference between them.
Method 1 :
[sourcecode lang=”java”]
import java.util.Scanner;
public class SimpleInterestExample {
public static void main(String[] args) {
int p,t,r, result;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Value of P : ");
p = sc.nextInt();
System.out.println("Enter the Value of T : ");
t = sc.nextInt();
System.out.println("Enter the Value of R : ");
r = sc.nextInt();
result = (p*t*r)/100;
System.out.println("Interest is : " + result);
}
}
[/sourcecode]
This one is the simplest way to write the program to find simple interest in Java. Here we have simply created the object of Scanner class for taking input. Scanner class was introduced in Java 6. To use Scanner class we will have to import the class from java.util package.
Method 2:
Now we will write a Java program that calculates and prints the simple interest using the Scanner class again but with object oriented programming approach.
[sourcecode lang=”java”]
import java.util.Scanner;
public class SimpleInterestExample
{
double principalAmount = 0;
double interestRate = 0;
double term = 0;
double simpleInterest = 0;
public void calculateSimpleInterest()
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the Principal amount : ");
principalAmount = input.nextDouble();
System.out.print("Enter the Rate As a decimal : ");
interestRate = input.nextDouble();
System.out.print("Enter the amount of time in years : ");
term = input.nextDouble();
simpleInterest = (principalAmount * interestRate * term) / 100;
}
public void displaySimpleInterest()
{
System.out.println("The Simple Interest is : " + simpleInterest);
}
}
[/sourcecode]
Mock Test Program for above code so that we can test the code. keep both the classes in same package and run the Mock Test Program or MockTestProgram.java
[sourcecode lang=”java”]
public class MockTestProgram{
public static void main(String[]args);
{
SimpleInterestExample simpleInt = new SimpleInterestExample();
simpleInt.calculateSimpleInterest();
simpleInt.displaySimpleInterest();
}
}
[/sourcecode]
Method 3:
Now we will write a Java program that calculates and prints the simple interest using the formula : “SimpleInterest = PTR/100” and input values P, T, R should be accepted as command line.
[sourcecode lang=”java”]
import java.util.Scanner;
public class SimpleInterestExample {
public static void main(String[] args) {
double p,t,r, result;
p = Double.parseDouble(args[0]);
t = Double.parseDouble(args[1]);
r = Double.parseDouble(args[2]);
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Value of P : ");
p = sc.nextDouble();
System.out.println("Enter the Value of T : ");
t = sc.nextDouble();
System.out.println("Enter the Value of R : ");
r = sc.nextDouble();
result = (p*t*r)/100;
System.out.println("Interest is : " + result);
}
}
[/sourcecode]
I will be sharing more programs soon as many students were asking for programs which were carried out during Java training and were asked in assignments after Java training. By the time enjoy coding in Java and use NetBeans, it will ease your life.