i build calculator java swing library. every thing else works except multiplication , division operators in actionevent loop. other operators work completely.
this error occurs: have tried try statement on part of code
calculator:

calculator multiplication error:
first enter number
then press operator suppose clear textfield - error occurs @ step
then enter second number
then press = button output answer

picture of error:
if(e.equals("*")) { fnum = txt.gettext(); logic.settotal(fnum); op = "*"; txt.settext(""); // error occurs here, textfield isn't cleared joptionpane.showmessagedialog(null, fnum); //messagebox see if fnum contains string textfield } if(e.equals("/")) { fnum = txt.gettext(); op = "/"; txt.settext(""); } actionevent loop/function:
public void actionperformed(actionevent ea) { else if(op.equals("*")) { logic.settotal(fnum); logic.multiplication(snum); total1 = logic.total; } else if(op.equals("/")) { logic.settotal(fnum); logic.divide(snum); total1 = logic.total; } txt.settext(""+total1); } logic inner class
inner class:
public class inner extends calculators{ public double total; public inner() { total = 0; } public void settotal(string n) { total = converttonumber(n); } public void divide(string n) { total /= converttonumber(n); } public void multiplication(string n) { total *=converttonumber(n); } } if confused please ask more code because can't include of code.
you @ first creating buttons this:
... jbutton plus = new jbutton("+"); jbutton multiplication = new jbutton("*"); jbutton divide = new jbutton("/"); jbutton minus = new jbutton("-"); ... and adding this action listener. lines missing:
... plus.addactionlistener(this); // missing: multiplication.addactionlistener(this); // missing: divide.addactionlistener(this); minus.addactionlistener(this); ... how found bug:
- downloaded code, compiled, etc.
- ran code, tried addition, multiplication, etc. (checking behaviour of application). kind of black box testing.
- looked differences between addition , multiplication analyzing code. related white box testing.
- i saw,
joptionpane.showmessagedialog(null, fnum);should called - has not been called. sat break point (in eclipse) debugging. - when realized,
actionperformedmethod has not been called @ i, searched lines of code, registering actionlisteners.
besides that: recommend refactor code. benefit rethinking structure of code. better readability, code easier maintain , new features can implemented faster.
i recommend to:
- reduce visibility of fields. make fields
private, can find references them. - avoid repetitions (called don't repead yourself technique). example: instead of calling
addactionlistenereach button make collection of buttons (i.e.arraylist<jbutton>, useforloop calladdactionlistenereach 1 of them. - also avoid duplicated code fragments, defining more, shorter methods
- consider deleting class
calculators, put code directly methods ofinner. - find more meaningful name
inner. maybeintermediateresultor similar. - create separate
actionlistenerinstance each button. cost little bit of performance (not noticable humans), avoid longif-chains - post code on code review (in stackexchange network) getting more , new ideas
Comments
Post a Comment