0% found this document useful (0 votes)
17 views

Assignment 5 Ila Shukla Div C 093

The document describes a Java program that implements a basic calculator application. It defines classes for a frame containing input fields, operator buttons, and a result label. When an operator button is clicked, the first number is saved and the input cleared. When calculate is clicked, the second number is read and the operation is performed on the saved numbers, with the result displayed. The main method creates an instance of the frame class to launch the application.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Assignment 5 Ila Shukla Div C 093

The document describes a Java program that implements a basic calculator application. It defines classes for a frame containing input fields, operator buttons, and a result label. When an operator button is clicked, the first number is saved and the input cleared. When calculate is clicked, the second number is read and the operation is performed on the saved numbers, with the result displayed. The main method creates an instance of the frame class to launch the application.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Assignment 5

Name : Ila Shukla Division C PRN: 22030121093


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Practice extends JFrame implements ActionListener {


JTextField inputField;
JButton add, sub, multi, div, calculate;
JPanel jp;
JLabel resultLabel;

int num1 = 0;
int num2 = 0;
char operator;

public Practice(String s) {
super(s);
jp = new JPanel();
inputField = new JTextField(10);

add = new JButton("+");


sub = new JButton("-");
multi = new JButton("*");
div = new JButton("/");
calculate = new JButton("Calculate");

resultLabel = new JLabel();

add.addActionListener(this);
sub.addActionListener(this);
multi.addActionListener(this);

div.addActionListener(this);

calculate.addActionListener(this);

jp.add(inputField);
jp.add(add);
jp.add(sub);
jp.add(multi);
jp.add(div);
jp.add(calculate);
jp.add(resultLabel);

add(jp);
}

public void actionPerformed(ActionEvent ae) {


if (ae.getSource() == add) {
operator = '+';
num1 = Integer.parseInt(inputField.getText());
inputField.setText("");
}

else if (ae.getSource() == sub) {


operator = '-';
num1 = Integer.parseInt(inputField.getText());
inputField.setText("");
}
Assignment 5
Name : Ila Shukla Division C PRN: 22030121093
else if (ae.getSource() == multi) {
operator = '*';
num1 = Integer.parseInt(inputField.getText());
inputField.setText("");
}

else if (ae.getSource() == div) {


operator = '/';
num1 = Integer.parseInt(inputField.getText());
inputField.setText("");
}

else if (ae.getSource() == calculate) {


num2 = Integer.parseInt(inputField.getText());

int result = 0;

// Perform the selected operation


switch (operator) {
case '+':
result = num1 + num2;
break;

case '-':
result = num1 - num2;
break;

case '*':
result = num1 * num2;
break;

case '/':
if (num2 != 0) {
result = num1 / num2;
} else {
resultLabel.setText("Error: Division by zero");
return;
}
break;
}

resultLabel.setText("Result: " + result);


}
}

public static void main(String args[]) {


JFrame jf = new Practice("Calculator");
jf.setSize(300, 400);
jf.setVisible(true);
}
}
Assignment 5
Name : Ila Shukla Division C PRN: 22030121093

Output-

a) Entered 1st number in input tab.

b) Click on operator buttons. I clicked on +

c) Enter 2nd number in input tab. Then click on calculate button for result to be displayed.

You might also like