Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

Calculator || + - * / || No gui

Quote:you mean class for every method(+-*/) ? why?


This is what he means:
Code
public int add(int a, int b) { return a + b; }
That's a good idea..

If you have a gui, there isn't a need for Switch/case just create buttonhandlers for each button, inside those handlers do the method. (add(1, 2), etc.)

Code
public class Calculator extends JFrame {
  public Calculator() {
    setTitle("Calculator");
    //... blah blah blah
    addButton.addEventListener(new AddButtonHandler());
  }

  public static void main(String[] args) {
    new Calculator();
  }

  private class AddButtonHandler implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      textField.setText(Integer.toString(add(number1, number2)));
    }
  }
}

Was bored, you could do something like that..
Maybe add the number to a stack (on the textField) which has a list of operations?


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by billybluiceezDec 8, 2016 08:13 PM 3 154 Dec 8, 2016 08:13 PM
Last Post: billybluiceez
[.NET] by ChessoMar 11, 2015 08:50 AM 7 289 Mar 11, 2015 08:50 AM
Last Post: ElementalTree
[Java] by JekwaAug 4, 2014 09:01 PM 9 126 Aug 4, 2014 09:01 PM
Last Post: Psifour