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

University of Engineering and Technology, Taxila: Assignment No.4

This document contains source code for a basic calculator application built using Java Swing. The code defines classes and methods to create a graphical user interface with buttons for numbers and operators and displays output in a text field. Calculations can be performed for basic math operations like addition, subtraction, multiplication and division.

Uploaded by

Zain iqbal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

University of Engineering and Technology, Taxila: Assignment No.4

This document contains source code for a basic calculator application built using Java Swing. The code defines classes and methods to create a graphical user interface with buttons for numbers and operators and displays output in a text field. Calculations can be performed for basic math operations like addition, subtraction, multiplication and division.

Uploaded by

Zain iqbal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

University of Engineering and Technology, Taxila

(Software Engineering Department)

ASSIGNMENT NO.4

Subject: Object Oriented Programming

Submitted By: Zain Iqbal

Registration No: 19-SE-28

Section: Omega (Ω)

Submitted To: Madam Madiha Liaqat


Calculator
Source Code:
package Calculator;

import java.awt.*;
import java.awt.event.*;

public class Calculator implements ActionListener {


Frame fr;
TextField t1;
Font f;
Button b, b1, b2, b3, b4,b4b, b5, b6, b7, b8,b8b, b9, b10, b11, b12,b12b, b13,
b14, b15, b16,b16b;
double num1 = 0, num2 = 0, result = 0;
char op;

Calculator() {
f = new Font("SansSerif", Font.PLAIN, 18);

/* Frame Body */
fr = new Frame("Calculator");
Color clr = new Color(0, 0, 0);
fr.setBackground(clr);
fr.setSize(330, 270);
fr.setLayout(null);
fr.setVisible(true);
fr.addWindowListener(new WindowAdapter() { // Window Closing
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

/* 1st Row */
/* Clear Button */
b = new Button("C");
Color clr2 = new Color(255, 51, 51);
b.setBackground(clr2);
b.setBounds(20, 40, 75, 30);
setFont_ActionListener(b);

/* TextField */
t1 = new TextField(10);
t1.setBounds(110, 40, 200, 30);
Color color = new Color(240, 255, 240);
color.darker();
t1.setBackground(color);
t1.setFont(f);
fr.add(t1);
/* Buttons */
/* 2nd Row */
b1 = new Button("7");
b1.setBounds(20, 80, 50, 30);
setFont_ActionListener(b1);

b2 = new Button("8");
b2.setBounds(80, 80, 50, 30);
setFont_ActionListener(b2);

b3 = new Button("9");
b3.setBounds(140, 80, 50, 30);
setFont_ActionListener(b3);

b4 = new Button("+");
b4.setBounds(200, 80, 50, 30);
setFont_ActionListener(b4);

b4b = new Button("√");


b4b.setBounds(260, 80, 50, 30);
setFont_ActionListener(b4b);

/* 3rd Row */
b5 = new Button("4");
b5.setBounds(20, 120, 50, 30);
setFont_ActionListener(b5);

b6 = new Button("5");
b6.setBounds(80, 120, 50, 30);
setFont_ActionListener(b6);

b7 = new Button("6");
b7.setBounds(140, 120, 50, 30);
setFont_ActionListener(b7);

b8 = new Button("-");
b8.setBounds(200, 120, 50, 30);
setFont_ActionListener(b8);

b8b = new Button("3√");


b8b.setBounds(260, 120, 50, 30);
setFont_ActionListener(b8b);

/* 4th Row */
b9 = new Button("1");
b9.setBounds(20, 160, 50, 30);
setFont_ActionListener(b9);

b10 = new Button("2");


b10.setBounds(80, 160, 50, 30);
setFont_ActionListener(b10);
b11 = new Button("3");
b11.setBounds(140, 160, 50, 30);
setFont_ActionListener(b11);

b12 = new Button("/");


b12.setBounds(200, 160, 50, 30);
setFont_ActionListener(b12);

b12b = new Button("^");


b12b.setBounds(260, 160, 50, 30);
setFont_ActionListener(b12b);

/* 5th Row */
b13 = new Button("0");
b13.setBounds(20, 200, 50, 30);
setFont_ActionListener(b13);

b14 = new Button(".");


b14.setBounds(80, 200, 50, 30);
setFont_ActionListener(b14);

b15 = new Button("=");


b15.setBounds(140, 200, 50, 30);
Color clr3 = new Color(65, 222, 26);
b15.setBackground(clr3);
setFont_ActionListener(b15);

b16 = new Button("x");


b16.setBounds(200, 200, 50, 30);
setFont_ActionListener(b16);

b16b = new Button("!");


b16b.setBounds(260, 200, 50, 30);

setFont_ActionListener(b16b);

/* Sets Font and ActionListener to the Buttons */


public void setFont_ActionListener(Button button) {
button.setFont(f);

button.addActionListener(this);
fr.add(button);

static int factorial(double num12){


if (num12 == 0)
return 1;
else
return (int) (num12 * factorial(num12-1));
}

/* Main Events i.e Plus, Minus, Subtract, Divide */


public void actionPerformed(ActionEvent event) {

String str = event.getActionCommand();

if (str.equals("+")) {

op = '+';
num1 = Double.parseDouble(t1.getText());
t1.setText("");
} else if (str.equals("-")) {
op = '-';
num1 = Double.parseDouble(t1.getText());
t1.setText("");
} else if (str.equals("*")) {
op = '*';
num1 = Double.parseDouble(t1.getText());
t1.setText("");
} else if (str.equals("/")) {
op = '/';
num1 = Double.parseDouble(t1.getText());
t1.setText("");
} else if (str.equals("√")) {
op = '√';
num1 = Double.parseDouble(t1.getText());

} else if (str.equals("!")) {
op='!';
num1 = Integer.parseInt(t1.getText());

} else if (str.equals("^")) {
op = '^';
num1 = Double.parseDouble(t1.getText());
t1.setText("");
}
else if (str.equals("=")) {

num2 = Double.parseDouble(t1.getText());

switch (op) {

case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
case '√':
result = Math.sqrt(num1);
case '!':
result = factorial(num1);
case '^':
result = Math.pow(num1, num2);

break;
}
t1.setText(result + "");
result = 0;
} else if (str.equals("C")) {

t1.setText("");
num1 = num2 = result = 0;
} else {
t1.setText(t1.getText() + str);
}
}

/* Main Method */
public static void main(String[] args) {
new Calculator();
}
}

Output:

You might also like