Java GUI Scientific Calculator -
i trying code scientific calculator in java. however, having difficulties trying program calculate. can see in code below, have managed gui working numbers printing on gui. have added functionality "+" button in order test first see if program works.
public class gui extends jframe { private static jbutton [] button = new jbutton[36]; private static textfield tf; private jpanel panel; private jpanel panel1; public gui(){ super("scientific calculator"); panel = new jpanel(); panel1 = new jpanel(new gridlayout(0,4)); tf = new textfield(20); tf.seteditable(false); panel.add(tf); button[0]=new jbutton("rad/deg"); button[1]=new jbutton("x!"); button[2]=new jbutton("sqrt"); button[3]=new jbutton("sin"); button[4]=new jbutton("cos"); button[5]=new jbutton("tan"); button[6]=new jbutton("ln"); button[7]=new jbutton("log"); button[8]=new jbutton("1/x"); button[9]=new jbutton("e^x"); button[10]=new jbutton("x^2"); button[11]=new jbutton("y^x"); button[12]=new jbutton("|x|"); button[13]=new jbutton("pi"); button[14]=new jbutton("e"); button[15]=new jbutton("c"); button[16]=new jbutton("("); button[17]=new jbutton(")"); button[18]=new jbutton("%"); button[19]=new jbutton("/"); button[20]=new jbutton("7"); button[21]=new jbutton("8"); button[22]=new jbutton("9"); button[23]=new jbutton("*"); button[24]=new jbutton("4"); button[25]=new jbutton("5"); button[26]=new jbutton("6"); button[27]=new jbutton("-"); button[28]=new jbutton("1"); button[29]=new jbutton("2"); button[30]=new jbutton("3"); button[31]=new jbutton("+"); button[32]=new jbutton("."); button[33]=new jbutton("0"); button[34] =new jbutton("+/-"); button[35] =new jbutton("="); for(int = 0; i<button.length;i++){ panel1.add(button[i]); } panel.add(panel1); add(panel); handlerclass handler = new handlerclass(); for(int = 0; i<button.length;i++){ button[i].addactionlistener(handler); } } private class handlerclass implements actionlistener{ @override public void actionperformed(actionevent e) { double num1 = 0,num2; string alreadydisplayed = tf.gettext(); string todisplay = ""; if(e.getsource()==button[33]){ alreadydisplayed = tf.gettext(); todisplay = alreadydisplayed + (button[33].gettext()); tf.settext(todisplay); } if(e.getsource()==button[28]){ alreadydisplayed = tf.gettext(); todisplay = alreadydisplayed + (button[28].gettext()); tf.settext(todisplay); } if(e.getsource()==button[29]){ alreadydisplayed = tf.gettext(); todisplay = alreadydisplayed + (button[29].gettext()); tf.settext(todisplay); } if(e.getsource()==button[30]){ alreadydisplayed = tf.gettext(); todisplay = alreadydisplayed + (button[30].gettext()); tf.settext(todisplay); } if(e.getsource()==button[24]){ alreadydisplayed = tf.gettext(); todisplay = alreadydisplayed + (button[24].gettext()); tf.settext(todisplay); } if(e.getsource()==button[25]){ alreadydisplayed = tf.gettext(); todisplay = (button[25].gettext()); tf.settext(todisplay); } if(e.getsource()==button[26]){ alreadydisplayed = tf.gettext(); todisplay = alreadydisplayed + (button[26].gettext()); tf.settext(todisplay); } if(e.getsource()==button[20]){ alreadydisplayed = tf.gettext(); todisplay = alreadydisplayed + (button[20].gettext()); tf.settext(todisplay); } if(e.getsource()==button[21]){ alreadydisplayed = tf.gettext(); todisplay = alreadydisplayed + (button[21].gettext()); tf.settext(todisplay); } if(e.getsource()==button[22]){ alreadydisplayed = tf.gettext(); todisplay = alreadydisplayed + (button[22].gettext()); tf.settext(todisplay); } if(e.getsource()==button[31]){ alreadydisplayed = tf.gettext(); todisplay = alreadydisplayed + (button[31].gettext()); tf.settext(todisplay); }
however problem afterwards. trying have try , catch block see if first input number , convert double, store in array can use operator function afterwards , keep adding numbers until user wants stop , perform calculation. attempt @ code below, wrong. can please suggest doing wrong? thanks
try{ num1 = double.parsedouble(todisplay); } catch(numberformatexception e1){ system.out.println("you have not entered number"); } double[] arr = new double[10]; for(int i=0; i<arr.length;i++){ arr[i]=num1; string test = string.valueof(arr[i]); tf.settext(test); }
edit have wrote new code simple calculator recommended user , works 2 inputs. still getting errors when try implement multiple inputs. below working code 2 input calculator.
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class calcgui extends jframe { private jbutton buttonzero; private jbutton buttonone; private jbutton buttontwo; private jbutton buttonthree; private jbutton buttonfour; private jbutton buttonfive; private jbutton buttonsix; private jbutton buttonseven; private jbutton buttoneight; private jbutton buttonnine; private jbutton opbuttonplus; private jbutton opbuttonminus; private jbutton opbuttondivide; private jbutton opbuttonmultiply; private jbutton opbuttonequals; private jbutton opbuttonclear; private textfield tf; private jpanel numbuttonpanel; private jpanel opbuttonpanel; private jpanel basepanel; public calcgui(){ super("scientific calculator"); basepanel = new jpanel(); numbuttonpanel = new jpanel(new gridlayout(0,4)); opbuttonpanel = new jpanel(new gridlayout(0,1)); tf = new textfield(20); tf.seteditable(false); basepanel.add(tf); buttonzero = new jbutton("0"); numbuttonpanel.add(buttonzero); buttonone = new jbutton("1"); numbuttonpanel.add(buttonone); buttontwo = new jbutton("2"); numbuttonpanel.add(buttontwo); buttonthree = new jbutton("3"); numbuttonpanel.add(buttonthree); buttonfour = new jbutton("4"); numbuttonpanel.add(buttonfour); buttonfive = new jbutton("5"); numbuttonpanel.add(buttonfive); buttonsix = new jbutton("6"); numbuttonpanel.add(buttonsix); buttonseven = new jbutton("7"); numbuttonpanel.add(buttonseven); buttoneight = new jbutton("8"); numbuttonpanel.add(buttoneight); buttonnine = new jbutton("9"); numbuttonpanel.add(buttonnine); basepanel.add(numbuttonpanel); opbuttonplus = new jbutton("+"); opbuttonminus = new jbutton("-"); opbuttondivide = new jbutton("/"); opbuttonmultiply = new jbutton("*"); opbuttonequals = new jbutton("="); opbuttonclear = new jbutton("c"); opbuttonpanel.add(opbuttonplus); opbuttonpanel.add(opbuttonminus); opbuttonpanel.add(opbuttondivide); opbuttonpanel.add(opbuttonmultiply); opbuttonpanel.add(opbuttonequals); opbuttonpanel.add(opbuttonclear); basepanel.add(opbuttonpanel); add(basepanel); handlerclass handler = new handlerclass(); buttonzero.addactionlistener(handler); buttonone.addactionlistener(handler); buttontwo.addactionlistener(handler); buttonthree.addactionlistener(handler); buttonfour.addactionlistener(handler); buttonfive.addactionlistener(handler); buttonsix.addactionlistener(handler); buttonseven.addactionlistener(handler); buttoneight.addactionlistener(handler); buttonnine.addactionlistener(handler); opbuttonplus.addactionlistener(handler); opbuttonminus.addactionlistener(handler); opbuttondivide.addactionlistener(handler); opbuttonmultiply.addactionlistener(handler); opbuttonequals.addactionlistener(handler); opbuttonclear.addactionlistener(handler); } private class handlerclass implements actionlistener{ public void actionperformed(actionevent e){ string alreadydisplayed = tf.gettext(); if(e.getsource()==buttonzero){ string buttonzerotext = alreadydisplayed + buttonzero.gettext(); tf.settext(buttonzerotext); }else if(e.getsource()==buttonone){ string buttononetext = alreadydisplayed + buttonone.gettext(); tf.settext(buttononetext); }else if(e.getsource()==buttontwo){ string buttontwotext = alreadydisplayed + buttontwo.gettext(); tf.settext(buttontwotext); }else if(e.getsource()==buttonthree){ string buttonthreetext = alreadydisplayed + buttonthree.gettext(); tf.settext(buttonthreetext); }else if(e.getsource()==buttonfour){ string buttonfourtext = alreadydisplayed + buttonfour.gettext(); tf.settext(buttonfourtext); }else if(e.getsource()==buttonfive){ string buttonfivetext = alreadydisplayed + buttonfive.gettext(); tf.settext(buttonfivetext); }else if(e.getsource()==buttonsix){ string buttonsixtext = alreadydisplayed + buttonsix.gettext(); tf.settext(buttonsixtext); }else if(e.getsource()==buttonseven){ string buttonseventext = alreadydisplayed + buttonseven.gettext(); tf.settext(buttonseventext); }else if(e.getsource()==buttoneight){ string buttoneighttext = alreadydisplayed + buttoneight.gettext(); tf.settext(buttoneighttext); }else if(e.getsource()==buttonnine){ string buttonninetext = alreadydisplayed + buttonnine.gettext(); tf.settext(buttonninetext); }else if(e.getsource()==opbuttonplus){ string opbuttonplustext = alreadydisplayed + opbuttonplus.gettext(); tf.settext(opbuttonplustext); }else if(e.getsource()==opbuttonequals){ string opbuttonequalstext = alreadydisplayed + opbuttonequals.gettext(); tf.settext(opbuttonequalstext); }else if(e.getsource()==opbuttonminus){ string opbuttonminustext = alreadydisplayed + opbuttonminus.gettext(); tf.settext(opbuttonminustext); }else if(e.getsource()==opbuttonmultiply){ string opbuttonmultiplytext = alreadydisplayed + opbuttonmultiply.gettext(); tf.settext(opbuttonmultiplytext); }else if(e.getsource()==opbuttondivide){ string opbuttondividetext = alreadydisplayed + opbuttondivide.gettext(); tf.settext(opbuttondividetext); }else if(e.getsource()==opbuttonclear){ string opbuttoncleartext = ""; tf.settext(opbuttoncleartext); } double result; for(int i=0; i<alreadydisplayed.length();i++){ if(e.getsource()==opbuttonequals){ if(alreadydisplayed.charat(i)=='+'){ result = double.parsedouble(alreadydisplayed.substring(0,i)) + double.parsedouble(alreadydisplayed.substring(i+1,alreadydisplayed.length())); tf.settext(alreadydisplayed + "=" + result); }else if(alreadydisplayed.charat(i)=='-'){ result = double.parsedouble(alreadydisplayed.substring(0,i)) - double.parsedouble(alreadydisplayed.substring(i+1,alreadydisplayed.length())); tf.settext(alreadydisplayed + "=" + result); }else if(alreadydisplayed.charat(i)=='/'){ result = double.parsedouble(alreadydisplayed.substring(0,i)) / double.parsedouble(alreadydisplayed.substring(i+1,alreadydisplayed.length())); tf.settext(alreadydisplayed + "=" + result); }else if(alreadydisplayed.charat(i)=='*'){ result = double.parsedouble(alreadydisplayed.substring(0,i)) * double.parsedouble(alreadydisplayed.substring(i+1,alreadydisplayed.length())); tf.settext(alreadydisplayed + "=" + result); } } } } } }
the part having difficulties getting number , storing can let user enter more 2 values. example if take out loop @ end of above code , replaced this;
double []num1=new double[10]; double []num2=new double[10]; //double num1 = 0; double temp =0; string str[] = new string [10]; string []str1 = new string[10]; if(e.getactioncommand().equals("+")) if(e.getsource() == opbuttonequals) for( int = 0; i<alreadydisplayed.length(); i++){ if(alreadydisplayed.charat(i)=='+') str[i] = alreadydisplayed.substring(0,i); num1[i] = double.parsedouble(str[i]); str1[i+1] = alreadydisplayed.substring(i+1,alreadydisplayed.length()); num2[i+1] = double.parsedouble(str1[i]); temp += num1[i] + num2[i+1]; tf.settext(double.tostring(temp)); }system.out.println(temp); } }
so when try test see if temp has value in console, prints 0 out each time. string text showing in text field, there no actual calculation working. thought work storing number , storing number , adding them has not.
why not store values string (much easier manipulate), , parse arithmetic expression later? example..
person presses buttons "(", "sin(8)", "+", "8", ")", "+", "1", "="
that evaluates easy string of (sin(8)+8)+1=
you can parse string , obvious things need parsed (e.g. sin, parse between brackets recursively). use math functions evaluate complex expressions, , parse simple ones.
i might recommend writing own functions solve complex expressions though - similar this...
public double solvesin(string inside) { if(numberutils.isnumber(inside)) { return math.sin(double.parsedouble(inside); } else { //call original function direct based on inputs return math.sin(parseequation(inside)); } }
Comments
Post a Comment