/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* TwoNumbers.java
*
* Created on Jul 19, 2010, 11:59:42 AM
*/
/**
*
* @author Achanta
*/
public class TwoNumbers extends javax.swing.JFrame {
private boolean counter = true,op=false;
String strnum;
int total = 0,i=0;
boolean click = true;
private int no = 0,lastCommand=1,command=0;
private boolean addClick=true,subClick=true,mulClick=true,divClick=true;
String temp;
int opType=0;
int opStack[]=new int[20];
/** Creates new form TwoNumbers */
public TwoNumbers() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
//
//GEN-BEGIN:initComponents
private void initComponents() {
jButton11 = new javax.swing.JButton();
text = new javax.swing.JTextField();
add = new javax.swing.JButton();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
sub = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
jButton9 = new javax.swing.JButton();
jButton10 = new javax.swing.JButton();
mulbutton = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
divbutton = new javax.swing.JButton();
equalbutton = new javax.swing.JButton();
c = new javax.swing.JButton();
backspace = new javax.swing.JButton();
jButton11.setText("jButton11");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
add.setText("+");
add.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addActionPerformed(evt);
}
});
jButton1.setText("1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("2");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setText("3");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
sub.setText("-");
sub.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
subActionPerformed(evt);
}
});
jButton4.setText("4");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jButton5.setText("5");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
jButton6.setText("6");
jButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton6ActionPerformed(evt);
}
});
jButton7.setText("7");
jButton7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton7ActionPerformed(evt);
}
});
jButton8.setText("8");
jButton8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton8ActionPerformed(evt);
}
});
jButton9.setText("9");
jButton9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton9ActionPerformed(evt);
}
});
jButton10.setText("0");
jButton10.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton10ActionPerformed(evt);
}
});
mulbutton.setText("*");
mulbutton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mulbuttonActionPerformed(evt);
}
});
jLabel1.setFont(new java.awt.Font("Times New Roman", 3, 14)); // NOI18N
jLabel1.setText("Calculator");
divbutton.setText("/");
divbutton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
divbuttonActionPerformed(evt);
}
});
equalbutton.setText("=");
equalbutton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
equalbuttonActionPerformed(evt);
}
});
c.setText("C");
c.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cActionPerformed(evt);
}
});
backspace.setText("BackSpace");
backspace.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
backspaceActionPerformed(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()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(jButton6)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton7)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton8)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton9))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton4)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton5)
.addComponent(jButton10)))
.addComponent(text, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(backspace, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(c, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(add)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(sub)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(mulbutton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(divbutton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(equalbutton))
.addGroup(layout.createSequentialGroup()
.addGap(86, 86, 86)
.addComponent(jLabel1)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(text, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton4)
.addComponent(jButton5)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton6)
.addComponent(jButton7)
.addComponent(jButton8)
.addComponent(jButton9)
.addComponent(jButton10))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(add)
.addComponent(sub, javax.swing.GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE)
.addComponent(mulbutton)
.addComponent(divbutton)
.addComponent(equalbutton))
.addGap(1, 1, 1)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(backspace)
.addComponent(c))
.addContainerGap())
);
pack();
}// //GEN-END:initComponents
private void equal()
{
no=Integer.parseInt(text.getText());
switch(opType)
{
case 1:
total += no;
break;
case 2:
total -= no;
break;
case 3:
total *= no;
break;
case 4:
total /= no;
break;
}
text.setText(Integer.toString(total));
counter=true;
}
private void getDigits()
{
if(counter==true)
{
text.setText("");
text.setText(temp);
no=Integer.parseInt(temp);
counter=false;
}
else {
strnum=text.getText() + temp;
no=Integer.parseInt(strnum);
text.setText(strnum);
}
}
private void assign() {
if (op == false) {
getDigits();
} else {
opeartion();
}
}
private void opeartion() {
if (
(addClick == true || subClick == false||mulClick==false||divClick==false)&&
(subClick==true||addClick==false||mulClick==false||divClick==false)&&
(mulClick==true||addClick==false||subClick==false||divClick==false)&&
(divClick==true||addClick==false||subClick==false||mulClick==false))
{
total = Integer.parseInt(text.getText());
System.out.println("the total and no values are "+total+" "+no);
subClick = false;
mulClick = false;
divClick = false;
addClick = false;
}
else
{
switch (opType) {
case 1:System.out.println("the total and no values are "+total+" "+no);
total+= no;
break;
case 2:System.out.println("the total and no values are "+total+" "+no);
total-= no;
break;
case 3:System.out.println("the total and no values are "+total+" "+no);
total*=no;
break;
case 4:System.out.println("the total and no values are "+total+" "+no);
total=total/no;
break;
}
}
text.setText(Integer.toString(total));
counter=true;
}
private void addActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addActionPerformed
op=true;
opType=1;subClick=true;mulClick=true;divClick=true;
assign();
}//GEN-LAST:event_addActionPerformed
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
temp = "1";op=false;
assign();
}//GEN-LAST:event_jButton1ActionPerformed
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
temp = "2";op=false;
assign();
}//GEN-LAST:event_jButton2ActionPerformed
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
temp = "3";op=false;
assign();
}//GEN-LAST:event_jButton3ActionPerformed
private void subActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_subActionPerformed
op=true;
opType=2;addClick=true;mulClick=true;divClick=true;
assign();
}//GEN-LAST:event_subActionPerformed
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
temp = "4";op=false;
assign();
}//GEN-LAST:event_jButton4ActionPerformed
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
temp = "5";op=false;
assign();
}//GEN-LAST:event_jButton5ActionPerformed
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
temp = "6";op=false;
assign();
}//GEN-LAST:event_jButton6ActionPerformed
private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
temp = "7";op=false;
assign();
}//GEN-LAST:event_jButton7ActionPerformed
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
temp = "8";op=false;
assign();
}//GEN-LAST:event_jButton8ActionPerformed
private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
temp = "9";op=false;
assign();
}//GEN-LAST:event_jButton9ActionPerformed
private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed
temp = "0";op=false;
assign();
}//GEN-LAST:event_jButton10ActionPerformed
private void mulbuttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mulbuttonActionPerformed
op=true;
opType=3;addClick=true;subClick=true;divClick=true;
assign();
}//GEN-LAST:event_mulbuttonActionPerformed
private void divbuttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_divbuttonActionPerformed
op=true;
opType=4;addClick=true;subClick=true;mulClick=true;
assign();
}//GEN-LAST:event_divbuttonActionPerformed
private void equalbuttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_equalbuttonActionPerformed
addClick=true;subClick=true;mulClick=true;divClick=true;
equal();
}//GEN-LAST:event_equalbuttonActionPerformed
private void cActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cActionPerformed
no=0;
text.setText("");
}//GEN-LAST:event_cActionPerformed
private void backspaceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backspaceActionPerformed
String notemp=Integer.toString(no);
System.out.println("the string length is "+notemp.length());
if (notemp.length() == 1) {
no = 0;
notemp=Integer.toString(no);
} else {
notemp = notemp.substring(0, notemp.length() - 1);
int no = Integer.parseInt(notemp);
System.out.println("the number is " + no);
}
text.setText(notemp);
counter=true;
}//GEN-LAST:event_backspaceActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TwoNumbers().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton add;
private javax.swing.JButton backspace;
private javax.swing.JButton c;
private javax.swing.JButton divbutton;
private javax.swing.JButton equalbutton;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton10;
private javax.swing.JButton jButton11;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
private javax.swing.JLabel jLabel1;
private javax.swing.JButton mulbutton;
private javax.swing.JButton sub;
private javax.swing.JTextField text;
// End of variables declaration//GEN-END:variables
}