Ram Lal Anand College
University of Delhi
Department of Computer Science
Session :- April 2021 to August 2021
PROJECT
Submitted to : Ms. Shikha Verma
Program Name : B.Sc (H) Computer Science
Title of paper : Programming in JAVA
Semester : 2nd Semester
Submitted by : 1. Name : Sumit Kumar
Roll No. : 20058570040
2. Name : Tanisha Bisht
Roll No. : 20058570043
Page | 2
Simple & Compound Interest Calculator
Here we have designed a ‘Simple & Compound Interest Calculator’. To
make it we have used Java Swing and Java AWT (Abstract Window
Toolkit).
Location :
Page | 3
Formula Used :
Simple Interest = Principal * Rate * Time
Compound Interest = Principal * (1+Rate)Time - Principal
DIRECTORY NAME : calculator
FILE NAME : InterestCalc.java
Code :
package calculator;
import calc.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class InterestCalc implements ActionListener
{
public JFrame f1;
JButton b1,b2;
Page | 4
public InterestCalc()
{
f1 = new JFrame("Simple & Compound Interest Calculator");
f1.setBounds(10,50,700,320);
ImageIcon i = new ImageIcon("c.png");
f1.setIconImage(i.getImage());
JPanel p1 = new JPanel();
//Adding Labels
JLabel label1 = new JLabel("Simple & Compound Interest
Calculator",JLabel.CENTER);
label1.setBounds(40,30,600,40);
Font fnt11 = new Font ("Arial",Font.BOLD,30);
label1.setFont(fnt11);
p1.add(label1);
JLabel label2 = new JLabel("Calculate : ",JLabel.CENTER);
label2.setBounds(200,100,300,40);
Font fnt22 = new Font ("Arial",Font.BOLD,25);
label2.setFont(fnt22);
p1.add(label2);
Page | 5
//Creating Two Buttons
b1 = new JButton("Simple Interest");
b2 = new JButton("Compound Interest");
//Setting Size & Location of the Buttons
b1.setBounds(200,150,300,40);
b2.setBounds(200,200,300,40);
//Setting Fonts to Buttons
b1.setFont(fnt22);
b2.setFont(fnt22);
//Addding Buttons
p1.add(b1);
p1.add(b2);
//Add ActionListener
b1.addActionListener(this);
b2.addActionListener(this);
p1.setLayout(null);
f1.add(p1);
Page | 6
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setResizable(false);
f1.setVisible(true);
public void actionPerformed(ActionEvent e)
{
f1.dispose();
JButton btn = (JButton)e.getSource();
if(btn == b1)
{
simpleCalc ob = new simpleCalc();
}
else if(btn == b2)
{
compoundCalc obj = new compoundCalc();
}
Page | 7
public static void main(String args[])
{
DIRECTORY NAME : calc
FILE NAME : simpleCalc.java
Code:
package calc;
import calculator.InterestCalc;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class simpleCalc implements ActionListener
{
JFrame f2;
Page | 8
JLabel formula,res;
JLabel l1,l2,l3;
JTextField t1,t2,t3;
JButton btn1,btn2,btn3;
public simpleCalc()
{
f2 = new JFrame("Simple Interest");
f2.setBounds(720,50,500,600);
ImageIcon i = new ImageIcon("c.png");
f2.setIconImage(i.getImage());
JPanel p2 = new JPanel();
JLabel lbl1 = new JLabel("Simple Interest",JLabel.CENTER);
lbl1.setBounds(40,30,460,40);
Font fnt1 = new Font ("Arial",Font.BOLD,30);
lbl1.setFont(fnt1);
p2.add(lbl1);
//Label to show the Formula
formula = new JLabel("S.I. = (P * R * T)/100",JLabel.CENTER);
Page | 9
formula.setFont(fnt1);
formula.setBounds(40,80,410,70);
formula.setForeground(Color.BLUE);
formula.setBorder(BorderFactory.createLineBorder(Color.BLACK));
p2.add(formula);
//Creating Three Labels
l1 = new JLabel("Principal in Rs. (P): ");
l2 = new JLabel("Rate in % (R) : ");
l3 = new JLabel("Time in years (T) : ");
//Creating Three TextFields to Take the Input from the User
t1 = new JTextField();
t2 = new JTextField();
t3 = new JTextField();
//Setting Size & Location of the Labels
l1.setBounds(80,180,190,30);
l2.setBounds(80,220,190,30);
l3.setBounds(80,260,190,30);
//Setting Size & Location of the TextFields
t1.setBounds(270,180,150,30);
t2.setBounds(270,220,150,30);
Page | 10
t3.setBounds(270,260,150,30);
//Setting Fonts to Labels and TextFields
Font fnt2 = new Font ("Arial",Font.BOLD,20);
l1.setFont(fnt2);
l2.setFont(fnt2);
l3.setFont(fnt2);
t1.setFont(fnt2);
t2.setFont(fnt2);
t3.setFont(fnt2);
//Addding Labels and TextFields
p2.add(l1);
p2.add(l2);
p2.add(l3);
p2.add(t1);
p2.add(t2);
p2.add(t3);
//Adding Label
JLabel lbl2 = new JLabel("[ Please enter only Positive Values.
]",JLabel.CENTER);
lbl2.setBounds(50,300,400,30);
lbl2.setForeground(Color.BLUE);
Font fnt3 = new Font ("Arial",Font.BOLD,15);
Page | 11
lbl2.setFont(fnt3);
p2.add(lbl2);
//Label to show the Result
res = new JLabel("Result will be Shown Here.",JLabel.CENTER);
res.setFont(fnt1);
res.setBounds(30,350,440,30);
p2.add(res);
//Creating Two Buttons
btn1 = new JButton("Calculate");
btn2 = new JButton("Reset");
btn3 = new JButton("Back");
//Setting Size & Location of the Buttons
btn1.setBounds(150,400,170,30);
btn2.setBounds(150,440,170,30);
btn3.setBounds(170,480,130,30);
btn3.setForeground(Color.BLUE);
//Setting Fonts to Buttons
Font fnt4 = new Font ("Arial",Font.PLAIN,20);
btn1.setFont(fnt4);
btn2.setFont(fnt4);
Page | 12
btn3.setFont(fnt4);
//Addding Buttons
p2.add(btn1);
p2.add(btn2);
p2.add(btn3);
//Add ActionListener
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
p2.setLayout(null);
f2.add(p2);
f2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f2.setResizable(false);
f2.setVisible(true);
public void actionPerformed(ActionEvent e)
{
Page | 13
JButton b = (JButton)e.getSource();
if(b == btn1)
{
if(t1.getText().equals("") || t2.getText().equals("")
|| t3.getText().equals(""))
{
JOptionPane.showMessageDialog(f2,"You must enter all the
values.","Error Message",JOptionPane.INFORMATION_MESSAGE);
}
else
{
double p = Double.parseDouble(t1.getText());
double r = Double.parseDouble(t2.getText());
double t = Double.parseDouble(t3.getText());
double si = (p*r*t)/100;
String val = String.format("%.2f",si);
res.setText("Simple Interest = "+si);
}
}
else if(b == btn2)
{
Page | 14
t1.setText("");
t2.setText("");
t3.setText("");
res.setText("Result will be Shown Here.");
}
else if(b == btn3)
{
f2.dispose();
InterestCalc ch = new InterestCalc();
ch.f1.setVisible(true);
}
public static void main(String args[])
{
Page | 15
DIRECTORY NAME : calc
FILE NAME : compoundCalc.java
Code :
package calc;
import calculator.InterestCalc;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class compoundCalc implements ActionListener
{
JFrame f3;
JLabel formula1,result;
JLabel l11,l22,l33;
JTextField t11,t22,t33;
JButton btn11,btn22,btn33;
Page | 16
public compoundCalc()
{
f3 = new JFrame("Compound Interest");
f3.setBounds(720,50,500,600);
ImageIcon i = new ImageIcon("c.png");
f3.setIconImage(i.getImage());
JPanel p3 = new JPanel();
JLabel lbl11 = new JLabel("Compound Interest",JLabel.CENTER);
lbl11.setBounds(40,30,460,40);
Font font1 = new Font ("Arial",Font.BOLD,30);
lbl11.setFont(font1);
p3.add(lbl11);
//Label to show the Formula
formula1 = new JLabel("C.I. = P * (1+R/100) ^ T -
P",JLabel.CENTER);
formula1.setFont(font1);
formula1.setBounds(40,80,410,70);
formula1.setForeground(Color.BLUE);
formula1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
p3.add(formula1);
Page | 17
//Creating Three Labels
l11 = new JLabel("Principal in Rs. (P): ");
l22 = new JLabel("Rate in % (R) : ");
l33 = new JLabel("Time in years (T) : ");
//Creating Three TextFields to Take the Input from the User
t11 = new JTextField();
t22 = new JTextField();
t33 = new JTextField();
//Setting Size & Location of the Labels
l11.setBounds(80,180,190,30);
l22.setBounds(80,220,190,30);
l33.setBounds(80,260,190,30);
//Setting Size & Location of the TextFields
t11.setBounds(270,180,150,30);
t22.setBounds(270,220,150,30);
t33.setBounds(270,260,150,30);
//Setting Fonts to Labels and TextFields
Font font2 = new Font ("Arial",Font.BOLD,20);
l11.setFont(font2);
Page | 18
l22.setFont(font2);
l33.setFont(font2);
t11.setFont(font2);
t22.setFont(font2);
t33.setFont(font2);
//Addding Labels and TextFields
p3.add(l11);
p3.add(l22);
p3.add(l33);
p3.add(t11);
p3.add(t22);
p3.add(t33);
//Adding Label
JLabel lbl22 = new JLabel("[ Please enter only Positive Values.
]",JLabel.CENTER);
lbl22.setBounds(50,300,400,30);
lbl22.setForeground(Color.BLUE);
Font font3 = new Font ("Arial",Font.BOLD,15);
lbl22.setFont(font3);
p3.add(lbl22);
//Label to show the Result
result = new JLabel("Result will be Shown Here.",JLabel.CENTER);
Page | 19
result.setFont(font1);
result.setBounds(30,350,440,30);
p3.add(result);
//Creating Two Buttons
btn11 = new JButton("Calculate");
btn22 = new JButton("Reset");
btn33 = new JButton("Back");
//Setting Size & Location of the Buttons
btn11.setBounds(150,400,170,30);
btn22.setBounds(150,440,170,30);
btn33.setBounds(170,480,130,30);
btn33.setForeground(Color.BLUE);
//Setting Fonts to Buttons
Font font4 = new Font ("Arial",Font.PLAIN,20);
btn11.setFont(font4);
btn22.setFont(font4);
btn33.setFont(font4);
//Addding Buttons
p3.add(btn11);
p3.add(btn22);
Page | 20
p3.add(btn33);
//Add ActionListener
btn11.addActionListener(this);
btn22.addActionListener(this);
btn33.addActionListener(this);
p3.setLayout(null);
f3.add(p3);
f3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f3.setResizable(false);
f3.setVisible(true);
public void actionPerformed(ActionEvent e)
{
JButton button = (JButton)e.getSource();
if(button == btn11)
{
if(t11.getText().equals("") || t22.getText().equals("")
|| t33.getText().equals(""))
Page | 21
{
JOptionPane.showMessageDialog(f3,"You must enter all the
values.","Error Message",JOptionPane.INFORMATION_MESSAGE);
}
else
{
double p1 = Double.parseDouble(t11.getText());
double r1 = Double.parseDouble(t22.getText());
double t1 = Double.parseDouble(t33.getText());
double amount = p1 * Math.pow((1+r1/100),t1);
double ci = amount - p1;
String val = String.format("%.2f",ci);
result.setText("Compound Interest = "+val);
}
}
else if(button == btn22)
{
t11.setText("");
t22.setText("");
t33.setText("");
result.setText("Result will be Shown Here.");
Page | 22
}
else if(button == btn33)
{
f3.dispose();
InterestCalc op = new InterestCalc();
op.f1.setVisible(true);
}
public static void main(String args[])
{
Page | 23
FILE NAME : calculator.java
Code :
import calculator.InterestCalc;
public class calculator
{
public static void main(String args[])
{
InterestCalc disp = new InterestCalc();
}
To Compile : javac calculator.java
To Run : java calculator
Page | 24
Page | 25
1.When ‘Simple Interest’ button is clicked then ‘Simple Interest’ window
for calculating Simple Interest will be opened.
Page | 26
If all or one of the values(P, R & T) When all the values (P, R & T) are
are not entered and ‘Calculate’ entered and ‘Calculate’ button is
button is clicked then ‘You must clicked then Simple Interest will be
enter all the values’ message will calculated and the result will be
appear. displayed.
Page | 27
When ‘Reset’ button is clicked When ‘Back’ button is clicked then
then all the values entered by the ‘Simple Interest’ window will be
the user and the result of the closed and the earlier window ‘Simple
earlier calculation will be & Compound Interest Calculator’ will
erased. be opened.
Page | 28
2.When ‘Compound Interest’ button is clicked then ‘Compound Interest’
window for calculating Compound Interest will be opened.
Page | 29
If all or one of the values(P, R & T) When all the values (P, R & T) are
are not entered and ‘Calculate’ entered and ‘Calculate’ button is
button is clicked then ‘You must clicked then Compound Interest
enter all the values’ message will will be calculated and the result will
appear. be displayed.
Page | 30
When ‘Reset’ button is clicked When ‘Back’ button is clicked then
then all the values entered by the ‘Compound Interest’ window will
the user and the result of the be closed and the earlier window
earlier calculation will be ‘Simple & Compound Interest
erased. Calculator’ will be opened.
Page | 31
The project ‘Simple and Compound Interest Calculator’ is
a simple application of Java. It takes the input (values of
'P', 'R' and 'T') from the user and calculates as per the
option selected by the user ('Simple interest' or 'Compound
interest') and then displays the result with the formula.
This project is very suitable for calculating simple and
compound interest as the calculations are highly accurate.
Page | 32