0% found this document useful (0 votes)
87 views12 pages

Areej Nasim Oop Assignment 03

The document is an assignment submission for an Object Oriented Programming theory course. It asks the student to create a simple calculator application using Netbeans. The student provides the code to build a calculator user interface with buttons and text fields. The code implements basic calculator functionality like number entry, arithmetic operations, and evaluating expressions.

Uploaded by

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

Areej Nasim Oop Assignment 03

The document is an assignment submission for an Object Oriented Programming theory course. It asks the student to create a simple calculator application using Netbeans. The student provides the code to build a calculator user interface with buttons and text fields. The code implements basic calculator functionality like number entry, arithmetic operations, and evaluating expressions.

Uploaded by

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

Object Oriented Programming (theory)

Assignment number 03

Submitted by:
Areej Nasim

Submitted to:
Mam Mehak Sheikh

Date: December 29th, 2021.


National University of Modern Languages (NUML)

---------------------------------------------------------------------------------------------------------------------

Course Title: Object Oriented Programming (Theory) Course Code: ACS-202

Class: BS (CS) Semester: 2 nd

Theory Assignment: 3 Total Marks: 10

---------------------------------------------------------------------------------------------------------------------

Theory Assignment # 03

Individual Assignment

------------------------------------------------------------------------------------------------------------------------------------------

Question:

Create a Simple Calculator e.g. Windows Calculator. Use Net-beans to create window forms with

single text fields and provide functionality for plus, minus multiply, equals to, C, 1,2, MR, M+,

MC. Write code behind buttons to take values from text field and implement the intended

functionality.

SOLUTION:

public class pnlCalculator extends javax.swing.JFrame {

public pnlCalculator() {

initComponents();

@SuppressWarnings("unchecked")

private void initComponents() {

jPanel1 = new javax.swing.JPanel();


txtResult = new javax.swing.JTextField();

jPanel2 = new javax.swing.JPanel();

btnPlusMinus = new javax.swing.JButton();

btnClear = new javax.swing.JButton();

btnPlus = new javax.swing.JButton();

btnMinus = new javax.swing.JButton();

btnMultiplication = new javax.swing.JButton();

btnDivision = new javax.swing.JButton();

btnEqual = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);

jPanel1.setLayout(jPanel1Layout);

jPanel1Layout.setHorizontalGroup(

jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createSequentialGroup()

.addGap(0, 0, Short.MAX_VALUE)

.addComponent(txtResult, javax.swing.GroupLayout.PREFERRED_SIZE, 411,


javax.swing.GroupLayout.PREFERRED_SIZE))

);

jPanel1Layout.setVerticalGroup(

jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(txtResult, javax.swing.GroupLayout.PREFERRED_SIZE, 150,
javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(0, 0, Short.MAX_VALUE))

);

jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

});

jPanel2.add(btn0, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 205, 89, 88));

btnPlusMinus.setText("+/-");

btnPlusMinus.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent evt) {

btnPlusMinusMouseClicked(evt);

});

jPanel2.add(btnPlusMinus, new org.netbeans.lib.awtextra.AbsoluteConstraints(96, 205, 95, 88));

btnClear.setText("CE");

btnClear.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent evt) {

btnClearMouseClicked(evt);

});

jPanel2.add(btnClear, new org.netbeans.lib.awtextra.AbsoluteConstraints(198, 205, 95, 88));


btnPlus.setText("+");

btnPlus.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent evt) {

btnPlusMouseClicked(evt);

});

jPanel2.add(btnPlus, new org.netbeans.lib.awtextra.AbsoluteConstraints(372, 0, -1, 50));

btnMinus.setText("-");

btnMinus.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent evt) {

btnMinusMouseClicked(evt);

});

jPanel2.add(btnMinus, new org.netbeans.lib.awtextra.AbsoluteConstraints(374, 57, -1, 50));

btnMultiplication.setText("*");

btnMultiplication.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent evt) {

btnMultiplicationMouseClicked(evt);

});

jPanel2.add(btnMultiplication, new org.netbeans.lib.awtextra.AbsoluteConstraints(372, 114, -1,


50));

btnDivision.setText("/");
btnDivision.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent evt) {

btnDivisionMouseClicked(evt);

});

jPanel2.add(btnDivision, new org.netbeans.lib.awtextra.AbsoluteConstraints(374, 171, -1, 50));

btnEqual.setText("=");

btnEqual.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent evt) {

btnEqualMouseClicked(evt);

});

jPanel2.add(btnEqual, new org.netbeans.lib.awtextra.AbsoluteConstraints(372, 228, -1, 65));

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()

.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12,
Short.MAX_VALUE)

.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addContainerGap())

);

pack();

}// </editor-fold>

else {

txtResult.setText(txtResult.getText() + " " + btn0.getText());

value2 = 0;

private void btnClearMouseClicked(java.awt.event.MouseEvent evt) {

txtResult.setText("");

private void btnPlusMinusMouseClicked(java.awt.event.MouseEvent evt) {

private void btnPlusMouseClicked(java.awt.event.MouseEvent evt) {

if(!(txtResult.getText().isEmpty())){

operator = "plus";
txtResult.setText(txtResult.getText()+ "+");

private void btnMinusMouseClicked(java.awt.event.MouseEvent evt) {

if(!(txtResult.getText().isEmpty())){

operator = "minus";

txtResult.setText(txtResult.getText()+ "-");

private void btnMultiplicationMouseClicked(java.awt.event.MouseEvent evt) {

if(!(txtResult.getText().isEmpty())){

operator = "multiplication";

txtResult.setText(txtResult.getText()+ "*");

private void btnDivisionMouseClicked(java.awt.event.MouseEvent evt) {

if(!(txtResult.getText().isEmpty())){

operator = "division";

txtResult.setText(txtResult.getText()+ "/");

}
private void btnEqualMouseClicked(java.awt.event.MouseEvent evt) {

double answer = 0;

if(operator == "plus")

answer = value1 + value2;

else if(operator=="minus")

answer = value1 - value2;

else if (operator =="multiplication")

answer = value1 * value2;

else if(operator == "division")

answer = value1/value2;

String Result = Double.toString(answer);

txtResult.setText(Result);

public static void main(String args[]) {

try {

for (javax.swing.UIManager.LookAndFeelInfo info :


javax.swing.UIManager.getInstalledLookAndFeels()) {

if ("Nimbus".equals(info.getName())) {

javax.swing.UIManager.setLookAndFeel(info.getClassName());

break;

} catch (ClassNotFoundException ex) {


java.util.logging.Logger.getLogger(pnlCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null,
ex);

} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(pnlCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null,
ex);

} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(pnlCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null,
ex);

} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(pnlCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null,
ex);

//</editor-fold>

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new pnlCalculator().setVisible(true);

});

static int value1;

static int value2;

static String operator;

private javax.swing.JButton btnClear;


private javax.swing.JButton btnDivision;

private javax.swing.JButton btnEqual;

private javax.swing.JButton btnMinus;

private javax.swing.JButton btnMultiplication;

private javax.swing.JButton btnPlus;

private javax.swing.JButton btnPlusMinus;

private javax.swing.JPanel jPanel1;

private javax.swing.JPanel jPanel2;

private javax.swing.JTextField txtResult;

// End of variables declaration

OUTPUT:

You might also like