1

I know there was probably a better way to make this program do what it does but i couldn't come up with a better algorithm that does exactly how this program does, any suggestions are helpful

package calc;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

/**
*
* @author Ben
*/
public class GUI extends JFrame {

int response, count=0;    
double num1, num2, num3, num5, num6, num7, num8, total=0;
String operation, answer, num, testnum;
private JButton one, two, three, four, five, six, seven, eight, 
        nine, zero, multiply, divide, subtract, add, equals, clear;
private JTextField display, fakedisplay;

public GUI(){
    super("Calculator");
    setLayout (new FlowLayout());

    fakedisplay = new JTextField(10);
    display = new JTextField(10);
    display.setEditable(false);        
    add(display);                  
    one = new JButton("1");     
    add(one);
    two = new JButton("2");
    add(two);
    three = new JButton("3");
    add(three);
    four = new JButton("4");
    add(four);
    five = new JButton("5");
    add(five);
    six = new JButton("6");
    add(six);
    seven = new JButton("7");
    add(seven);
    eight = new JButton("8");
    add(eight);
    nine = new JButton("9");
    add(nine);
    zero = new JButton("0");
    add(zero);               
    multiply = new JButton("*");
    add(multiply);        
    divide = new JButton("/");
    add(divide);
    subtract = new JButton("-");
    add(subtract);
    add = new JButton("+");
    add(add);
    equals = new JButton("=");
    add(equals);  
    clear = new JButton("Clear");
    add(clear);


    handler handle = new handler();

    one.addActionListener(handle);
    two.addActionListener(handle);
    three.addActionListener(handle);
    four.addActionListener(handle);
    five.addActionListener(handle);
    six.addActionListener(handle);
    seven.addActionListener(handle);
    eight.addActionListener(handle);
    nine.addActionListener(handle);
    zero.addActionListener(handle);
    multiply.addActionListener(handle);
    divide.addActionListener(handle);
    subtract.addActionListener(handle);
    add.addActionListener(handle);
    equals.addActionListener(handle);
    clear.addActionListener(handle);

}
private class handler implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e){

        if(e.getSource()==one){
            response = 1;               
            display.setText(display.getText() + response);
            fakedisplay.setText(fakedisplay.getText() + response);
        }
        else if (e.getSource()==two){
            response = 2;
            display.setText(display.getText() + response);      
            fakedisplay.setText(fakedisplay.getText() + response);
        }
        else if (e.getSource()==three){
                response = 3;
                display.setText(display.getText() + response);
                fakedisplay.setText(fakedisplay.getText() + response);
        }
        else if (e.getSource()==four){
                response = 4;
                display.setText(display.getText() + response);
                fakedisplay.setText(fakedisplay.getText() + response);
        }
        else if (e.getSource()==five){
                response = 5;
                display.setText(display.getText() + response);
                fakedisplay.setText(fakedisplay.getText() + response);
        }
        else if (e.getSource()==six){
                response = 6;
                display.setText(display.getText() + response);
                fakedisplay.setText(fakedisplay.getText() + response);
        }
        else if (e.getSource()==seven){
                response = 7;
                display.setText(display.getText() + response);
                fakedisplay.setText(fakedisplay.getText() + response);
        }
        else if (e.getSource()==eight){
                response = 8;
                display.setText(display.getText() + response);
                fakedisplay.setText(fakedisplay.getText() + response);
        }
        else if (e.getSource()==nine){
                response = 9;
                display.setText(display.getText() + response);
                fakedisplay.setText(fakedisplay.getText() + response);
        }
        else if(e.getSource()==zero){
            response = 0;
            display.setText(display.getText() + response);
            fakedisplay.setText(fakedisplay.getText() + response);
        }
        else if (e.getSource()==multiply){
            if(count == 0){
                num1 = Double.parseDouble(display.getText());
                count++;
            }                
            else if(count == 1){
                num2 = Double.parseDouble(fakedisplay.getText());
                total = num1*num2;
                count++;
            }
            else if(count == 2){
                num3 = Double.parseDouble(fakedisplay.getText());
                total = total*num3;
                count++;
            }
            operation = "*";
             display.setText(display.getText() + operation);
             fakedisplay.setText("");

        }
        else if (e.getSource()==divide){
             if(count == 0){
                num1 = Double.parseDouble(display.getText());
                count++;
            }
             else if(count == 1){
                num2 = Double.parseDouble(fakedisplay.getText());
                total = num1/num2+num1%num2;
                count++;
            }
             else if(count == 2){
                num3 = Double.parseDouble(fakedisplay.getText());
                total = total/num3+total%num3;
                count++;
            }
                operation = "/";                    
                display.setText(display.getText() + operation);
                fakedisplay.setText("");
        }
        else if (e.getSource()==add){
            if(count == 0){
                num1 = Double.parseDouble(display.getText());
                count++;
            }
            else if(count == 1){
                num2 = Double.parseDouble(fakedisplay.getText());
                total = num1+num2;
                count++;
            }
            else if(count == 2){
                num3 = Double.parseDouble(fakedisplay.getText());
                total = total+num3;
                count++;
            }
                operation = "+";
                display.setText(display.getText() + operation);
                fakedisplay.setText("");
        }
        else if (e.getSource()==subtract){
             if(count == 0){
                num1 = Double.parseDouble(display.getText());
                count++;
            }
             else if (count == 1) {
                num2 = Double.parseDouble(fakedisplay.getText());
                total = num1-num2;
                count++;
            }
             else if(count == 2){
                num3 = Double.parseDouble(fakedisplay.getText());
                total = total-num3;
                count++;
            }
                operation = "-";
                display.setText(display.getText() + operation);
                fakedisplay.setText("");
        }
        else if (e.getSource()==equals){                
            operation = "=";
            display.setText(display.getText() + operation + total);                        
        }
        else if (e.getSource()==clear){
            display.setText("");
        }


}



}
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
user2555459
  • 29
  • 2
  • 6
  • 1
    Please refer to this [answer](http://stackoverflow.com/a/17259571/1057230), might be this can help :-) – nIcE cOw Jul 09 '13 at 05:13
  • 1
    I did not look through the 200+ lines of source code (at least, not carefully), though note that [this answer](http://stackoverflow.com/a/7441804/418556) achieves a working calculator in around 140 LOC. – Andrew Thompson Jul 09 '13 at 07:32

1 Answers1

3

There are a couple of issues I see, but the first thing you'll need to help you along:

Where you are performing the equals operation? Doing a simple arithmetic function:

A op B = ?  

(where A and B are numbers and op is one of +, -, *, /)

will slot a number into num1, but will not perform the op operation because count will never equal 1.

To help yourself out, add the following after the last else if block:

System.out.println("num1: " + num1 + " num2: " + num2 + " num3: " + num3
   + "\ndisplay: " + display.getText() + " fakedisplay: " + fakedisplay.getText() 
   + "\nresponse: " + response + "\ncount: " + count + "\ntotal: " + total);

This will give you a hint as to what has just happened after you hit each key/button.

If you think about the logic steps, what you need to do is:

Create fields for leftValue, operator, rightValue.
Handle the input just entered
   if input is a number and leftValue is null 
       leftValue = input
   if input is a number and leftValue (and operator) are not null
       leftValue = leftValue OPERATION input
   if input is an operator and leftValue is not null
       operator = input
mcalex
  • 6,628
  • 5
  • 50
  • 80
  • thanks for the help, that code after i added it actually does really show whats going on in the background thanks! im still a bit confused on how i would get count to go up accurately though, because count == 0 should work because its default is 0 and then at the end of the if statement is count++ so shouldnt it send it up to 1? – user2555459 Jul 09 '13 at 05:25
  • Yes, but once it hits 1 it doesn't go back into the (do some operation) part of the loop. If you do '2' '+' '4' '=' you will hit count = 1 after '+', but you don't re-enter that loop (while count = 1) to do the operation. I think you need to think about what happens when you hit '=', and treat it as an operation. – mcalex Jul 09 '13 at 05:30
  • sorry if i sound like i dont know anything because i just started getting into GUI and actionlisteners, but when you have an actionListener does that mean you have to somehow make the program operate to keep cycling through like you stated above? as in to make it keep operating past when count just equasls 1. – user2555459 Jul 09 '13 at 05:42
  • `ActionListener` listens for an action (eg a button press), so every time there's an action, the listener queries the action `(if e.getSource() == whatever)` to see if it can handle it. Watch what happens when you do 3 x 3 x 3 =. Also, add: ` + "\ntotal: " + total` to the `System.out.println` between 'count' and ')'. I've edited the answer to add this. – mcalex Jul 09 '13 at 06:06
  • yeah it assigns ever variable the same number, so is there some sort of other listener i would need for this rather then an ActionListener? – user2555459 Jul 09 '13 at 06:14
  • That's not the problem, it's the logic. Think on what a (simple) arithmetic operation is: a number, an operator, a number. You can't perform the operation until you know both numbers. You don't know both numbers until you have either hit equals, or hit another operator. Not until that has happened, can you perform the operation and store the result (in a subtotal, or in num1, maybe). So the flow is: enter number [set number] enter operator [set operator] enter number [perform operation, set result] enter operator [set operator] enter number [perform op, set result] enter equals [display] – mcalex Jul 09 '13 at 06:24
  • i understand, so how should i edit my code to work like this correctly? – user2555459 Jul 09 '13 at 06:29
  • thanks a ton! lets hope i can put this together and make it work with all the help! – user2555459 Jul 10 '13 at 00:44