Say, I input "2-5*6/4" ...It will not give me the correct result.
Another problem is that, for example you type "3 -" then you decided to use "+" instead of "-", the first operation will not cancel.. Help please. Thanks.
package javaapplication1;
public class ScientificCalculator extends javax.swing.JDialog {
private int perform = 1;
private double total1 = 0;
private double total2 = 0;
private double extra = 1;
private double extra2 = 1;
private int clear = 0;
private int test = 0;
private char operation = ' ';
public ScientificCalculator(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
}
public void getOperation(String btn){
operation = btn.charAt(0);
if(operation=='+'){
total1=total1+Double.parseDouble(txt1.getText());
txt1.setText(Double.toString(total1));
}
if(operation=='-'){
total1=total1+Double.parseDouble(txt1.getText());
txt1.setText(Double.toString(total1));
}
if(operation=='*'){
extra=extra*Double.parseDouble(txt1.getText());
txt1.setText(Double.toString(extra));
}
if(operation=='/'){
extra=Double.parseDouble(txt1.getText())/extra;
txt1.setText(Double.toString(extra));
}
}
/** 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.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jButton2 = new javax.swing.JButton();
txt1 = new javax.swing.JTextField();
btnSeven = new javax.swing.JButton();
btnEight = new javax.swing.JButton();
btnNine = new javax.swing.JButton();
btnFour = new javax.swing.JButton();
btnFive = new javax.swing.JButton();
btnSix = new javax.swing.JButton();
btnOne = new javax.swing.JButton();
btnTwo = new javax.swing.JButton();
btnThree = new javax.swing.JButton();
btnZero = new javax.swing.JButton();
jButton12 = new javax.swing.JButton();
btnDecimal = new javax.swing.JButton();
btnMultiply = new javax.swing.JButton();
btnDivide = new javax.swing.JButton();
btnAdd = new javax.swing.JButton();
btnMinus = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
btnClear = new javax.swing.JButton();
btnEquals = new javax.swing.JButton();
jButton2.setText("jButton1");
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
txt1.setFont(new java.awt.Font("Tahoma", 0, 24));
txt1.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
txt1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txt1ActionPerformed(evt);
}
});
btnSeven.setText("7");
btnSeven.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSevenActionPerformed(evt);
}
});
btnEight.setText("8");
btnEight.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnEightActionPerformed(evt);
}
});
btnNine.setText("9");
btnNine.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnNineActionPerformed(evt);
}
});
btnFour.setText("4");
btnFour.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnFourActionPerformed(evt);
}
});
btnFive.setText("5");
btnFive.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnFiveActionPerformed(evt);
}
});
btnSix.setText("6");
btnSix.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSixActionPerformed(evt);
}
});
btnOne.setText("1");
btnOne.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnOneActionPerformed(evt);
}
});
btnTwo.setText("2");
btnTwo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnTwoActionPerformed(evt);
}
});
btnThree.setText("3");
btnThree.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnThreeActionPerformed(evt);
}
});
btnZero.setText("0");
btnZero.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnZeroActionPerformed(evt);
}
});
jButton12.setText("jButton12");
btnDecimal.setText(".");
btnDecimal.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnDecimalActionPerformed(evt);
}
});
btnMultiply.setText("*");
btnMultiply.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnMultiplyActionPerformed(evt);
}
});
btnDivide.setText("/");
btnDivide.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnDivideActionPerformed(evt);
}
});
btnAdd.setText("+");
btnAdd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAddActionPerformed(evt);
}
});
btnMinus.setText("-");
btnMinus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnMinusActionPerformed(evt);
}
});
jButton6.setText("=");
btnClear.setText("C");
btnClear.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnClearActionPerformed(evt);
}
});
btnEquals.setText("=");
btnEquals.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnEqualsActionPerformed(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(119, 119, 119)
.addComponent(jButton12, javax.swing.GroupLayout.PREFERRED_SIZE, 0, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(31, 31, 31)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(btnSeven)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnEight)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnNine))
.addGroup(layout.createSequentialGroup()
.addComponent(btnFour)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnFive)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnSix))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(btnZero, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(btnOne)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnTwo)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnDecimal)
.addComponent(btnThree))))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(btnAdd, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
.addComponent(btnMultiply, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(btnEquals, 0, 0, Short.MAX_VALUE)
.addComponent(btnMinus, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
.addComponent(btnDivide, javax.swing.GroupLayout.DEFAULT_SIZE, 42, Short.MAX_VALUE))
.addGap(33, 33, 33)
.addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 0, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(44, 44, 44)
.addComponent(btnClear))))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(txt1, javax.swing.GroupLayout.PREFERRED_SIZE, 290, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(22, 22, 22))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(23, 23, 23)
.addComponent(txt1, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(35, 35, 35)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnNine, javax.swing.GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnSeven, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnEight, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnClear, javax.swing.GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnSix, javax.swing.GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnFour, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnFive, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnTwo, javax.swing.GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE)
.addComponent(btnThree, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(btnOne, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnDivide, javax.swing.GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE)
.addComponent(btnMultiply, javax.swing.GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnMinus, javax.swing.GroupLayout.DEFAULT_SIZE, 37, Short.MAX_VALUE)
.addComponent(btnAdd, javax.swing.GroupLayout.DEFAULT_SIZE, 37, Short.MAX_VALUE))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton6, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)
.addComponent(btnEquals, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnZero, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)
.addComponent(btnDecimal, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)))
.addGap(18, 18, 18)
.addComponent(jButton12, javax.swing.GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void btnThreeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThreeActionPerformed
// TODO add your handling code here:
if (test>=1){
txt1.setText("");
}
if (clear==1){
txt1.setText("");
}txt1.getText();
String Three = txt1.getText() + btnThree.getText();
txt1.setText(Three);
clear=0;
test = 0;
}//GEN-LAST:event_btnThreeActionPerformed
private void btnFourActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFourActionPerformed
// TODO add your handling code here:
if (test>=1){
txt1.setText("");
}
if (clear==1){
txt1.setText("");
}txt1.getText();
String Four = txt1.getText() + btnFour.getText();
txt1.setText(Four);
clear=0;
test = 0;
}//GEN-LAST:event_btnFourActionPerformed
private void btnZeroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnZeroActionPerformed
// TODO add your handling code here:
if (test>=1){
txt1.setText("");
}
if (clear==1){
txt1.setText("");
}txt1.getText();
String Zero = txt1.getText() + btnZero.getText();
txt1.setText(Zero);
clear=0;
test = 0;
}//GEN-LAST:event_btnZeroActionPerformed
private void btnOneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOneActionPerformed
// TODO add your handling code here:
if (test>=1){
txt1.setText("");
}
if (clear==1){
txt1.setText("");
}txt1.getText();
String One = txt1.getText() + btnOne.getText();
txt1.setText(One);
clear=0;
test = 0;
}//GEN-LAST:event_btnOneActionPerformed
private void btnTwoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTwoActionPerformed
// TODO add your handling code here:
if (test>=1){
txt1.setText("");
}
if (clear==1){
txt1.setText("");
}txt1.getText();
String Two = txt1.getText() + btnTwo.getText();
txt1.setText(Two);
clear=0;
test = 0;
}//GEN-LAST:event_btnTwoActionPerformed
private void btnFiveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFiveActionPerformed
// TODO add your handling code here:
if (test>=1){
txt1.setText("");
}
if (clear==1){
txt1.setText("");
}txt1.getText();
String Five = txt1.getText() + btnFive.getText();
txt1.setText(Five);
clear=0;
}//GEN-LAST:event_btnFiveActionPerformed
private void btnSixActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSixActionPerformed
// TODO add your handling code here:
if (test>=1){
txt1.setText("");
}
else if (clear==1){
txt1.setText("");
}
txt1.getText();
String Six = txt1.getText() + btnSix.getText();
txt1.setText(Six);
clear=0;
test = 0;
}//GEN-LAST:event_btnSixActionPerformed
private void btnSevenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSevenActionPerformed
// TODO add your handling code here:
if (test>=1){
txt1.setText("");
}
if (clear==1){
txt1.setText("");
}
txt1.getText();
String Seven = txt1.getText() + btnSeven.getText();
txt1.setText(Seven);
clear=0;
test = 0;
}//GEN-LAST:event_btnSevenActionPerformed
private void btnEightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEightActionPerformed
// TODO add your handling code here:
if (test>=1){
txt1.setText("");
}
if (clear==1){
txt1.setText("");
}
else txt1.getText();
String Eight = txt1.getText() + btnEight.getText();
txt1.setText(Eight);
clear=0;
test = 0;
}//GEN-LAST:event_btnEightActionPerformed
private void btnNineActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNineActionPerformed
// TODO add your handling code here:
if (test>=1){
txt1.setText("");
}
if (clear==1){
txt1.setText("");
}txt1.getText();
String Nine = txt1.getText() + btnNine.getText();
txt1.setText(Nine);
clear=0;
test = 0;
}//GEN-LAST:event_btnNineActionPerformed
private void btnDecimalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDecimalActionPerformed
// TODO add your handling code here:
if (test>=1){
txt1.setText("");
}
if (clear==1){
txt1.setText("");
}
txt1.getText();
String Point = txt1.getText() + btnDecimal.getText();
txt1.setText(Point);
clear = 0;
test = 0;
}//GEN-LAST:event_btnDecimalActionPerformed
private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClearActionPerformed
// TODO add your handling code here:
txt1.setText("0");
perform = 1;
total1 = 0;
total2 = 0;
extra = 1;
extra2 = 1;
clear = 0;
test = 0;
operation = ' ';
clear=1;
}//GEN-LAST:event_btnClearActionPerformed
private void txt1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txt1ActionPerformed
// TODO add your handling code here:
txt1.setText("");
clear = 1;
}//GEN-LAST:event_txt1ActionPerformed
private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
// TODO add your handling code here:
String btn = btnAdd.getText();
getOperation(btn);
test = test + 1;
}//GEN-LAST:event_btnAddActionPerformed
private void btnEqualsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEqualsActionPerformed
// TODO add your handling code here:
if(operation=='+'){
total2=total1+Double.parseDouble(txt1.getText());
txt1.setText(Double.toString(total2));
total1=0;
}
if(operation=='-'){
total2=total1-Double.parseDouble(txt1.getText());
txt1.setText(Double.toString(total2));
total1=0;
}
if(operation=='*'){
extra2=extra*Double.parseDouble(txt1.getText());
txt1.setText(Double.toString(extra2));
extra=1;
}
if(operation=='/'){
extra2=extra/Double.parseDouble(txt1.getText());
txt1.setText(Double.toString(extra2));
extra=1;
}
}//GEN-LAST:event_btnEqualsActionPerformed
private void btnDivideActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDivideActionPerformed
// TODO add your handling code here:
String btn = btnDivide.getText();
getOperation(btn);
test = test + 1;
}//GEN-LAST:event_btnDivideActionPerformed
private void btnMultiplyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMultiplyActionPerformed
// TODO add your handling code here:
String btn = btnMultiply.getText();
getOperation(btn);
test = test + 1;
}//GEN-LAST:event_btnMultiplyActionPerformed
private void btnMinusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMinusActionPerformed
// TODO add your handling code here:
String btn = btnMinus.getText();
getOperation(btn);
test = test + 1;
}//GEN-LAST:event_btnMinusActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
ScientificCalculator dialog = new ScientificCalculator(new javax.swing.JFrame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btnAdd;
private javax.swing.JButton btnClear;
private javax.swing.JButton btnDecimal;
private javax.swing.JButton btnDivide;
private javax.swing.JButton btnEight;
private javax.swing.JButton btnEquals;
private javax.swing.JButton btnFive;
private javax.swing.JButton btnFour;
private javax.swing.JButton btnMinus;
private javax.swing.JButton btnMultiply;
private javax.swing.JButton btnNine;
private javax.swing.JButton btnOne;
private javax.swing.JButton btnSeven;
private javax.swing.JButton btnSix;
private javax.swing.JButton btnThree;
private javax.swing.JButton btnTwo;
private javax.swing.JButton btnZero;
private javax.swing.JButton jButton12;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton6;
private javax.swing.JTextField txt1;
// End of variables declaration//GEN-END:variables
}

New Topic/Question
This topic is locked


MultiQuote



|