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

Java

The document describes a Java class for a basic calculator application. It contains code to initialize the graphical user interface components like buttons and text fields. Button click events are handled to perform calculation operations and display results.

Uploaded by

W P
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views

Java

The document describes a Java class for a basic calculator application. It contains code to initialize the graphical user interface components like buttons and text fields. Button click events are handled to perform calculation operations and display results.

Uploaded by

W P
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package calculation;

/**

* @author Bhavesh,Sachin,Dhruvank

*/

public class calculators extends javax.swing.JFrame {

double firstnum;

double secondnum;

double result;

String operations;/**

* Creates new form calculators

*/

public calculators() {

initComponents();

/**

* This method is called from within the constructor to initialize the form.

1
* WARNING: Do NOT modify this code. The content of this method is always

* regenerated by the Form Editor.

*/

@SuppressWarnings("unchecked")

// <editor-fold defaultstate="collapsed" desc="Generated Code">

private void initComponents() {

jBtn6 = new javax.swing.JButton();

jtxtDisplay = new javax.swing.JTextField();

jBtn1 = new javax.swing.JButton();

jBtn2 = new javax.swing.JButton();

jBtn3 = new javax.swing.JButton();

jBtn4 = new javax.swing.JButton();

jBtn5 = new javax.swing.JButton();

jBtn7 = new javax.swing.JButton();

jBtn8 = new javax.swing.JButton();

jBtn9 = new javax.swing.JButton();

jBtn10 = new javax.swing.JButton();

jBtn11 = new javax.swing.JButton();

jBtn12 = new javax.swing.JButton();

jBtn13 = new javax.swing.JButton();

jBtn16 = new javax.swing.JButton();

jBtn66 = new javax.swing.JButton();

jButton1 = new javax.swing.JButton();

2
jBtn0 = new javax.swing.JButton();

jBtn6.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N

jBtn6.setText("1");

jBtn6.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jBtn6ActionPerformed(evt);

});

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jtxtDisplay.setFont(new java.awt.Font("Yu Gothic Light", 1, 18)); // NOI18N

jtxtDisplay.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

jtxtDisplay.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jtxtDisplayActionPerformed(evt);

});

jBtn1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N

jBtn1.setText("1");

jBtn1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

3
jBtn1ActionPerformed(evt);

});

jBtn2.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N

jBtn2.setText("2");

jBtn2.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jBtn2ActionPerformed(evt);

});

jBtn3.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N

jBtn3.setText("3");

jBtn3.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jBtn3ActionPerformed(evt);

});

jBtn4.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N

jBtn4.setText("4");

jBtn4.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

4
jBtn4ActionPerformed(evt);

});

jBtn5.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N

jBtn5.setText("5");

jBtn5.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jBtn5ActionPerformed(evt);

});

jBtn7.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N

jBtn7.setText("7");

jBtn7.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jBtn7ActionPerformed(evt);

});

jBtn8.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N

jBtn8.setText("8");

jBtn8.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

5
jBtn8ActionPerformed(evt);

});

jBtn9.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N

jBtn9.setText("9");

jBtn9.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jBtn9ActionPerformed(evt);

});

jBtn10.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N

jBtn10.setText("+");

jBtn10.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jBtn10ActionPerformed(evt);

});

jBtn11.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N

jBtn11.setText("-");

jBtn11.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

6
jBtn11ActionPerformed(evt);

});

jBtn12.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N

jBtn12.setText("*");

jBtn12.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jBtn12ActionPerformed(evt);

});

jBtn13.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N

jBtn13.setText("/");

jBtn13.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jBtn13ActionPerformed(evt);

});

jBtn16.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N

jBtn16.setText("=");

jBtn16.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

7
jBtn16ActionPerformed(evt);

});

jBtn66.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N

jBtn66.setText("6");

jBtn66.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jBtn66ActionPerformed(evt);

});

jButton1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N

jButton1.setText("Clear");

jButton1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton1ActionPerformed(evt);

});

jBtn0.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N

jBtn0.setText("0");

jBtn0.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

8
jBtn0ActionPerformed(evt);

});

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

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

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

.addGroup(layout.createSequentialGroup()

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

.addGroup(layout.createSequentialGroup()

.addGap(30, 30, 30)

.addComponent(jtxtDisplay, javax.swing.GroupLayout.PREFERRED_SIZE, 380,


javax.swing.GroupLayout.PREFERRED_SIZE))

.addGroup(layout.createSequentialGroup()

.addGap(80, 80, 80)

.addComponent(jBtn1, javax.swing.GroupLayout.PREFERRED_SIZE, 80,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(20, 20, 20)

.addComponent(jBtn2, javax.swing.GroupLayout.PREFERRED_SIZE, 80,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(20, 20, 20)

.addComponent(jBtn3, javax.swing.GroupLayout.PREFERRED_SIZE, 80,


javax.swing.GroupLayout.PREFERRED_SIZE))

.addGroup(layout.createSequentialGroup()

.addGap(80, 80, 80)

9
.addComponent(jBtn4, javax.swing.GroupLayout.PREFERRED_SIZE, 80,
javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(20, 20, 20)

.addComponent(jBtn5, javax.swing.GroupLayout.PREFERRED_SIZE, 80,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(20, 20, 20)

.addComponent(jBtn66, javax.swing.GroupLayout.PREFERRED_SIZE, 80,


javax.swing.GroupLayout.PREFERRED_SIZE))

.addGroup(layout.createSequentialGroup()

.addGap(80, 80, 80)

.addComponent(jBtn7, javax.swing.GroupLayout.PREFERRED_SIZE, 80,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(20, 20, 20)

.addComponent(jBtn8, javax.swing.GroupLayout.PREFERRED_SIZE, 80,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(20, 20, 20)

.addComponent(jBtn9, javax.swing.GroupLayout.PREFERRED_SIZE, 80,


javax.swing.GroupLayout.PREFERRED_SIZE))

.addGroup(layout.createSequentialGroup()

.addGap(80, 80, 80)

.addComponent(jBtn12, javax.swing.GroupLayout.PREFERRED_SIZE, 80,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(20, 20, 20)

.addComponent(jBtn0, javax.swing.GroupLayout.PREFERRED_SIZE, 80,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(20, 20, 20)

.addComponent(jBtn16, javax.swing.GroupLayout.PREFERRED_SIZE, 80,


javax.swing.GroupLayout.PREFERRED_SIZE))

10
.addGroup(layout.createSequentialGroup()

.addGap(80, 80, 80)

.addComponent(jBtn11, javax.swing.GroupLayout.PREFERRED_SIZE, 80,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(20, 20, 20)

.addComponent(jBtn10, javax.swing.GroupLayout.PREFERRED_SIZE, 80,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(20, 20, 20)

.addComponent(jBtn13, javax.swing.GroupLayout.PREFERRED_SIZE, 80,


javax.swing.GroupLayout.PREFERRED_SIZE))

.addGroup(layout.createSequentialGroup()

.addGap(132, 132, 132)

.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 172,


javax.swing.GroupLayout.PREFERRED_SIZE)))

.addContainerGap(33, Short.MAX_VALUE))

);

layout.setVerticalGroup(

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

.addGroup(layout.createSequentialGroup()

.addGap(10, 10, 10)

.addComponent(jtxtDisplay, javax.swing.GroupLayout.PREFERRED_SIZE, 65,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(15, 15, 15)

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

.addComponent(jBtn1, javax.swing.GroupLayout.PREFERRED_SIZE, 57,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jBtn2, javax.swing.GroupLayout.PREFERRED_SIZE, 57,

11
javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jBtn3, javax.swing.GroupLayout.PREFERRED_SIZE, 57,


javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(13, 13, 13)

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

.addComponent(jBtn4, javax.swing.GroupLayout.PREFERRED_SIZE, 57,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jBtn5, javax.swing.GroupLayout.PREFERRED_SIZE, 57,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jBtn66, javax.swing.GroupLayout.PREFERRED_SIZE, 57,


javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(13, 13, 13)

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

.addComponent(jBtn7, javax.swing.GroupLayout.PREFERRED_SIZE, 57,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jBtn8, javax.swing.GroupLayout.PREFERRED_SIZE, 57,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jBtn9, javax.swing.GroupLayout.PREFERRED_SIZE, 57,


javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(23, 23, 23)

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

.addComponent(jBtn12, javax.swing.GroupLayout.PREFERRED_SIZE, 57,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jBtn0, javax.swing.GroupLayout.PREFERRED_SIZE, 57,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jBtn16, javax.swing.GroupLayout.PREFERRED_SIZE, 57,


javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(23, 23, 23)

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

.addComponent(jBtn11, javax.swing.GroupLayout.PREFERRED_SIZE, 60,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jBtn10, javax.swing.GroupLayout.PREFERRED_SIZE, 57,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jBtn13, javax.swing.GroupLayout.PREFERRED_SIZE, 57,


javax.swing.GroupLayout.PREFERRED_SIZE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 77,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addContainerGap())

);

pack();

}// </editor-fold>

private void jBtn8ActionPerformed(java.awt.event.ActionEvent evt) {

String Enternumber = jtxtDisplay.getText() + jBtn8.getText();

jtxtDisplay.setText(Enternumber); // TODO add your handling code here:

private void jBtn16ActionPerformed(java.awt.event.ActionEvent evt) {

String answer;

secondnum=Double.parseDouble(jtxtDisplay.getText());

if(operations=="+")

13
result=firstnum+secondnum;

answer=String.format("%.0f",result);

jtxtDisplay.setText(answer);

else if(operations=="-")

result=firstnum-secondnum;

answer=String.format("%.0f",result);

jtxtDisplay.setText(answer);

else if(operations=="*")

result=firstnum*secondnum;

answer=String.format("%.0f",result);

jtxtDisplay.setText(answer);

else if(operations=="/")

result=firstnum/secondnum;

answer=String.format("%.0f",result);

jtxtDisplay.setText(answer);

14
}

else if(operations=="pow")

result=1;

while(secondnum!=0)

result=firstnum*result;

secondnum--;

answer=String.format("%.0f",result);

jtxtDisplay.setText(answer);

else if(operations=="!")

{ double c=1;

while(firstnum!=0)

c=c*firstnum;

result=c;

firstnum--;

15
answer=String.format("%.0f",result);

jtxtDisplay.setText(answer);

}// TODO add your handling code here:

private void jBtn6ActionPerformed(java.awt.event.ActionEvent evt) {

jtxtDisplay.setText("");

private void jBtn1ActionPerformed(java.awt.event.ActionEvent evt) {

String Enternumber = jtxtDisplay.getText() + jBtn1.getText();

jtxtDisplay.setText(Enternumber);

// TODO add your handling code here:

private void jtxtDisplayActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

private void jBtn2ActionPerformed(java.awt.event.ActionEvent evt) {

String Enternumber = jtxtDisplay.getText() + jBtn2.getText();

jtxtDisplay.setText(Enternumber); // TODO add your handling code here:

16
private void jBtn3ActionPerformed(java.awt.event.ActionEvent evt) {

String Enternumber = jtxtDisplay.getText() + jBtn3.getText();

jtxtDisplay.setText(Enternumber); // TODO add your handling code here:

private void jBtn4ActionPerformed(java.awt.event.ActionEvent evt) {

String Enternumber = jtxtDisplay.getText() + jBtn4.getText();

jtxtDisplay.setText(Enternumber); // TODO add your handling code here:

private void jBtn5ActionPerformed(java.awt.event.ActionEvent evt) {

String Enternumber = jtxtDisplay.getText() + jBtn5.getText();

jtxtDisplay.setText(Enternumber); // TODO add your handling code here:

private void jBtn66ActionPerformed(java.awt.event.ActionEvent evt) {

String Enternumber = jtxtDisplay.getText() + jBtn66.getText();

jtxtDisplay.setText(Enternumber); // TODO add your handling code here:

private void jBtn7ActionPerformed(java.awt.event.ActionEvent evt) {

String Enternumber = jtxtDisplay.getText() + jBtn7.getText();

jtxtDisplay.setText(Enternumber); // TODO add your handling code here:

17
private void jBtn9ActionPerformed(java.awt.event.ActionEvent evt) {

String Enternumber = jtxtDisplay.getText() + jBtn9.getText();

jtxtDisplay.setText(Enternumber); // TODO add your handling code here:

private void jBtn0ActionPerformed(java.awt.event.ActionEvent evt) {

String Enternumber = jtxtDisplay.getText() + jBtn0.getText();

jtxtDisplay.setText(Enternumber); // TODO add your handling code here:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

jtxtDisplay.setText(""); // TODO add your handling code here:

private void jBtn10ActionPerformed(java.awt.event.ActionEvent evt) {

double x;

firstnum=Double.parseDouble(jtxtDisplay.getText());

x=firstnum;

jtxtDisplay.setText("+");

operations="+";

// TODO add your handling code here:

18
}

private void jBtn11ActionPerformed(java.awt.event.ActionEvent evt) {

firstnum=Double.parseDouble(jtxtDisplay.getText());

jtxtDisplay.setText("-");

operations="-"; // TODO add your handling code here:

private void jBtn12ActionPerformed(java.awt.event.ActionEvent evt) {

firstnum=Double.parseDouble(jtxtDisplay.getText());

jtxtDisplay.setText("");

operations="*"; // TODO add your handling code here:

private void jBtn13ActionPerformed(java.awt.event.ActionEvent evt) {

firstnum=Double.parseDouble(jtxtDisplay.getText());

jtxtDisplay.setText("");

operations="/"; // TODO add your handling code here:

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

19
/* Set the Nimbus look and feel */

//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">

/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.

* For details see


http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html

*/

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(calculators.class.getName()).log(java.util.logging.Level.SEVER
E, null, ex);

} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(calculators.class.getName()).log(java.util.logging.Level.SEVER
E, null, ex);

} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(calculators.class.getName()).log(java.util.logging.Level.SEVER
E, null, ex);

} catch (javax.swing.UnsupportedLookAndFeelException ex) {

20
java.util.logging.Logger.getLogger(calculators.class.getName()).log(java.util.logging.Level.SEVER
E, null, ex);

//</editor-fold>

/* Create and display the form */

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

public void run() {

new calculators().setVisible(true);

});

// Variables declaration - do not modify

private javax.swing.JButton jBtn0;

private javax.swing.JButton jBtn1;

private javax.swing.JButton jBtn10;

private javax.swing.JButton jBtn11;

private javax.swing.JButton jBtn12;

private javax.swing.JButton jBtn13;

private javax.swing.JButton jBtn16;

private javax.swing.JButton jBtn2;

private javax.swing.JButton jBtn3;

private javax.swing.JButton jBtn4;

private javax.swing.JButton jBtn5;

21
private javax.swing.JButton jBtn6;

private javax.swing.JButton jBtn66;

private javax.swing.JButton jBtn7;

private javax.swing.JButton jBtn8;

private javax.swing.JButton jBtn9;

private javax.swing.JButton jButton1;

private javax.swing.JTextField jtxtDisplay;

// End of variables declaration

OUTPUT:-

22
23

You might also like