Core Java Tutorial

Simple Interest Example in Java

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.

7 differences between HashMap and HashTable

Objectives:

  • What is HashMap?
  • What is HashMap in Java?
  • What is HashTable?
  • What is HashTable in Java?
  • What are the differences between HashMap and HashTable?
  • What are the differences between HashMap and HashTable in Java?

HashMap vs HashTable

1. HashMap is non-synchronized. HashTable is synchronized.

2. HashMap is not thread safe. HashTable is thread safe.

3. HashMap allows one null key and any number of null values while HashTable do not allow null keys and null values in HashTable Object.

4. HashMap object values are iterated using iterator. HashTable is only class other than Vector class which uses enumerator to iterate values of HashTable object.

5. The iterator in HashMap is fail-fast iterator while enumerator for HashTable is not.

6. HashMap is much faster and uses less memory than HashTable.

7. HashMap is subclass of AbstractMap class. HashTable is subclass of Dictionary class which is obsolete in 1.7 and not in use anymore.

Core Java Workshop at TGPCET

Conducted a Workshop on Core Java at Tulsiramji Gaikwad Patil College of Engineering & Technology, Nagpur on 25 Mar. 2013 & 26 Mar. 2013. It was nice to provide Classroom Training & Workshop on Core Java to students of TGPCET.

Have a look at glimpse of workshop here :

[gigya src=”http://player.videofy.me/player.swf?videoId=1100848″ width=”480″ height=”360″ quality=”high” wmode=”transparent” allowFullScreen=”true”]

Watch this as You Tube video:

[youtube http://www.youtube.com/watch?v=NhnIxMeX4Hc&w=420&h=315]