java - Attempting to write code for a simple calculator. Trouble getting the program to solve the equation that the user inputs -
currently when user presses buttons on calculator displays value on text box above, cannot solve equation. every button pressed displays in textfield when press '=' (equals) solves equation. me problem please?
//michael moradi //period c1 //june 2, 2015 //final project semester 2 import javax.swing.*; //imports needed code import java.awt.*; import java.awt.event.*; public class calculator extends jframe implements actionlistener { public jbutton button1, button2, button3, button4, button5, button6, button7, button8, button9, button10, button11, button12, button13, button14, button15, button16; public jtextarea text; //makes these accessible throughout code public calculator() { setsize(350,300); //sets size 300 300 setresizable(false); //does not let user change size of window setdefaultcloseoperation(exit_on_close);//makes app close when press x on top left container contentpane = getcontentpane();//gets contentpane contentpane.setbackground(color.cyan);//sets background color white contentpane.setlayout(new flowlayout());//makes contentpane read left right text = new jtextarea(1, 25); contentpane.add(text); text.seteditable(false); button1 = new jbutton ("7"); contentpane.add(button1); button1.addactionlistener(this); button2 = new jbutton ("8"); contentpane.add(button2); button2.addactionlistener(this); button3 = new jbutton ("9"); contentpane.add(button3); button3.addactionlistener(this); button4 = new jbutton ("÷"); contentpane.add(button4); button4.addactionlistener(this); button5 = new jbutton ("4"); contentpane.add(button5); button5.addactionlistener(this); button6 = new jbutton ("5"); contentpane.add(button6); button6.addactionlistener(this); button7 = new jbutton ("6"); contentpane.add(button7); button7.addactionlistener(this); button8 = new jbutton ("x"); contentpane.add(button8); button8.addactionlistener(this); button9 = new jbutton ("1"); contentpane.add(button9); button9.addactionlistener(this); button10 = new jbutton ("2"); contentpane.add(button10); button10.addactionlistener(this); button11 = new jbutton ("3"); contentpane.add(button11); button11.addactionlistener(this); button12 = new jbutton ("-"); contentpane.add(button12); button12.addactionlistener(this); button13 = new jbutton ("0"); contentpane.add(button13); button13.addactionlistener(this); button14 = new jbutton ("c"); contentpane.add(button14); button14.addactionlistener(this); button15 = new jbutton ("="); contentpane.add(button15); button15.addactionlistener(this); button16 = new jbutton ("+"); contentpane.add(button16); button16.addactionlistener(this); } public static void main(string[] args) { calculator guiwindow = new calculator(); //uses gui guiwindow.setvisible(true); //makes visible } public void actionperformed(actionevent e) { container contentpane = getcontentpane(); string enterednumbers = text.gettext(); if (e.getactioncommand().equals("7")) text.settext(enterednumbers + ("7")); if (e.getactioncommand().equals("8")) text.settext(enterednumbers + ("8")); if (e.getactioncommand().equals("9")) text.settext(enterednumbers + ("9")); if (e.getactioncommand().equals("÷")) text.settext(enterednumbers + ("÷")); if (e.getactioncommand().equals("4")) text.settext(enterednumbers + ("4")); if (e.getactioncommand().equals("5")) text.settext(enterednumbers + ("5")); if (e.getactioncommand().equals("6")) text.settext(enterednumbers + ("6")); if (e.getactioncommand().equals("x")) text.settext(enterednumbers + ("x")); if (e.getactioncommand().equals("1")) text.settext(enterednumbers + ("1")); if (e.getactioncommand().equals("2")) text.settext(enterednumbers + ("2")); if (e.getactioncommand().equals("3")) text.settext(enterednumbers + ("3")); if (e.getactioncommand().equals("-")) text.settext(enterednumbers + ("-")); if (e.getactioncommand().equals("0")) text.settext(enterednumbers + ("0")); if (e.getactioncommand().equals("c")) text.settext(""); if (e.getactioncommand().equals("=")) text.settext(enterednumbers + ("=")); if (e.getactioncommand().equals("+")) text.settext(enterednumbers + ("+")); } }
for this, want change actionlistener. every other button—which encourage give more meaningful names, way—simply adds token of text calculator field. "=" meant, description, solve it. so,
if (e.getactioncommand().equals("=")) text.settext(enterednumbers + ("=")); should more this:
if (e.getactioncommand().equals("=")) { string math = text.gettext(); answerfield.settext(solve(math)); } private solve(string math) { //this tricky part, see description next. } for solving method, suggest 1 of 2 things. first break down series of regular expressions, can create , try out (before implementing) here. there, know whether provided field number, or operator. operators best paired enumerations , function fields. unfortunately, still leaves question of how handle operator precedence open.
for that, you'll want break down reverse polish notation; shunting-yard algorithm. after that, can step through , apply each operation function in order appears.
if you're in hurry, use java scripting engine , implement math solvable javascript; tutorial available here. currently, javascript/ecmascript available, i'm sure there plenty of others available through external libraries. (when you're dealing changing dynamic code, it's godsend.)
hopefully 1 of these resources you. once implemented script in java focused on solving mathematical equations, using first method; , plug in new applications second method. best of luck!
Comments
Post a Comment