Simple Interest Calculator using Jframe & Generating 30 Objects dynamically in Java
12.Assignment Submission
Student Name: Praveen Biradar
ID: KODD8KS8I
1. Simple Interest Calculator using Jframe
Code : simple_Interest_Calculator.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class simple_Interest_Calculator extends JFrame {
private static final double INTEREST_RATE = 5.0; // Static interest rate
public simple_Interest_Calculator() {
setTitle("SIMPLE INTEREST CALCULATOR");
setSize(500, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new GridBagLayout());
getContentPane().setBackground(new Color(44, 62, 80));
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10, 10, 10, 10);
gbc.fill = GridBagConstraints.HORIZONTAL;
JLabel titleLabel = new JLabel("SIMPLE INTEREST CALCULATOR", SwingConstants.CENTER);
titleLabel.setFont(new Font("Arial", Font.BOLD, 20));
titleLabel.setForeground(Color.WHITE);
gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 2;
add(titleLabel, gbc);
gbc.gridwidth = 1;
JLabel nameLabel = new JLabel("1. Name of the Business Man:");
nameLabel.setForeground(Color.WHITE);
gbc.gridx = 0; gbc.gridy = 1;
add(nameLabel, gbc);
JTextField nameField = new JTextField(15);
gbc.gridx = 1; gbc.gridy = 1;
add(nameField, gbc);
JLabel principalLabel = new JLabel("2. Principal Amount:");
principalLabel.setForeground(Color.WHITE);
gbc.gridx = 0; gbc.gridy = 2;
add(principalLabel, gbc);
JTextField principalField = new JTextField(15);
gbc.gridx = 1; gbc.gridy = 2;
add(principalField, gbc);
JLabel timeLabel = new JLabel("3. Time (years):");
timeLabel.setForeground(Color.WHITE);
gbc.gridx = 0; gbc.gridy = 3;
add(timeLabel, gbc);
JTextField timeField = new JTextField(15);
gbc.gridx = 1; gbc.gridy = 3;
add(timeField, gbc);
JLabel rateLabel = new JLabel("4. Interest Rate:");
rateLabel.setForeground(Color.WHITE);
gbc.gridx = 0; gbc.gridy = 4;
add(rateLabel, gbc);
JLabel rateValueLabel = new JLabel(INTEREST_RATE + "% (Fixed)");
rateValueLabel.setForeground(Color.YELLOW);
gbc.gridx = 1; gbc.gridy = 4;
add(rateValueLabel, gbc);
JButton calculateButton = new JButton("Calculate Interest");
calculateButton.setBackground(new Color(52, 152, 219));
calculateButton.setForeground(Color.WHITE);
gbc.gridx = 0; gbc.gridy = 5; gbc.gridwidth = 2;
add(calculateButton, gbc);
JLabel resultLabel1 = new JLabel(" ");
resultLabel1.setFont(new Font("Arial", Font.BOLD, 14));
resultLabel1.setForeground(Color.GREEN);
gbc.gridx = 0; gbc.gridy = 6; gbc.gridwidth = 2;
add(resultLabel1, gbc);
JLabel resultLabel2 = new JLabel(" ");
resultLabel2.setFont(new Font("Arial", Font.BOLD, 14));
resultLabel2.setForeground(Color.GREEN);
gbc.gridx = 0; gbc.gridy = 7; gbc.gridwidth = 2;
add(resultLabel2, gbc);
calculateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
String name = nameField.getText();
double principal = Double.parseDouble(principalField.getText());
double time = Double.parseDouble(timeField.getText());
double simpleInterest = (principal * INTEREST_RATE * time) / 100;
resultLabel1.setText("1. Simple Interest: Rs. " + String.format("%.2f",
simpleInterest));
resultLabel2.setText("2. " + name + " has taken Rs. " + principal + " amount
for " + time +
" year(s) duration at an interest rate of " + INTEREST_RATE +
"%.");
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Please enter valid inputs!", "Error",
JOptionPane.ERROR_MESSAGE);
}
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new simple_Interest_Calculator().setVisible(true));
}
}
Code Explanation
double simpleInterest = (principal * INTEREST_RATE * time) / 100;
• principal: User-inputted loan amount.
• INTEREST_RATE: Static value (5.0%) defined in the class.
• time: Number of years the money is borrowed for.
• Calculation: The formula computes simple interest by multiplying the principal, rate, and time, then
dividing by 100.
2.Generating 30 Objects dynamically in Java
BusinessMan Class
package StaticElements;
import java.util.Scanner;
public class BusinessMan {
int principle;
int time;
static float rate = 6.0f;
float si;
void takeInput(Scanner scanner) {
System.out.println("Enter Principle:");
principle = scanner.nextInt();
System.out.println("Enter Time: ");
time = scanner.nextInt();
}
void calculateSI() {
si = (principle * time * rate) / 100;
}
void displaySI() {
System.out.println("SI is: " + si);
}
}
Main Class to Generate 30 Objects
package StaticElements;
import java.util.Scanner;
public class SimpleInterest {
public static void main(String[] args) {
int numObjects = 30;
BusinessMan[] businessmen = new BusinessMan[numObjects];
Scanner scanner = new Scanner(System.in); // Scanner created once
for (int i = 0; i < numObjects; i++) {
businessmen[i] = new BusinessMan();
businessmen[i].takeInput(scanner); // Passing scanner to avoid closing it
businessmen[i].calculateSI();
businessmen[i].displaySI();
}
scanner.close(); // Close scanner after all inputs are taken
}
}
Output (Sample for 30 Objects) :
Enter Principle:
1000
Enter Time:
1
SI is: 60.0
Enter Principle:
2000
Enter Time:
2
SI is: 240.0
Enter Principle:
3000
Enter Time:
3
SI is: 540.0
Enter Principle:
4000
Enter Time:
4
SI is: 960.0
Enter Principle:
5000
Enter Time:
5
SI is: 1500.0
Enter Principle:
6000
Enter Time:
SI is: 2160.0
Enter Principle:
7000
Enter Time:
7
SI is: 2940.0
Enter Principle:
8000
Enter Time:
SI is: 3840.0
Enter Principle:
9000
Enter Time:
9
SI is: 4860.0
Enter Principle:
10000
Enter Time:
10
SI is: 6000.0
Enter Principle:
11000
Enter Time:
11
SI is: 7260.0
Enter Principle:
12000
Enter Time:
12
SI is: 8640.0
Enter Principle:
13000
Enter Time:
13
SI is: 10140.0
Enter Principle:
14000
Enter Time:
14
SI is: 11760.0
Enter Principle:
15000
Enter Time:
15
SI is: 13500.0
Enter Principle:
16000
Enter Time:
16
SI is: 15360.0
Enter Principle:
17000
Enter Time:
17
SI is: 17340.0
Enter Principle:
18000
Enter Time:
18
SI is: 19440.0
Enter Principle:
19000
Enter Time:
19
SI is: 21660.0
Enter Principle:
20000
Enter Time:
20
SI is: 24000.0
Enter Principle:
21000
Enter Time:
21
SI is: 26460.0
Enter Principle:
22000
Enter Time:
22
SI is: 29040.0
Enter Principle:
23000
Enter Time:
23
SI is: 31740.0
Enter Principle:
24000
Enter Time:
24
SI is: 34560.0
Enter Principle:
25000
Enter Time:
25
SI is: 37500.0
Enter Principle:
26000
Enter Time:
26
SI is: 40560.0
Enter Principle:
27000
Enter Time:
27
SI is: 43740.0
Enter Principle:
28000
Enter Time:
28
SI is: 47040.0
Enter Principle:
29000
Enter Time:
29
SI is: 50460.0
Enter Principle:
30000
Enter Time:
30
SI is: 54000.0
Explanation:
1. Class Usage:
o BusinessMan class has attributes principle, time, and a static rate.
o It contains methods to take input, calculate simple interest, and display results.
2. Dynamic Object Creation:
o Instead of writing 30 separate object declarations, an array of objects is used.
o A for loop dynamically initializes, processes, and displays interest calculations using new
BusinessMan().
3. Incrementing Values:
o principle starts at 1000 and increases by 1000 in each iteration.
o time starts at 1 and increases by 1 in each iteration.
4. Efficiency:
o The loop handles all objects, avoiding redundant code.
o The approach allows scalability, making it easy to modify for more objects.