import java.awt.*; import java.applet.*; public class Calculator extends Applet { public boolean degrees = true;; public boolean zeroLcd = true; public boolean dotMissing = true; public boolean prevMult = false; public boolean expFlag = false; public boolean eSet = false; public boolean hypFlag = false; public double lcdVal = 0; public double lastTerm = 0.0; public double theSum = 0.0; public double theTerm = 0.0; public double expBase = 0.0; public String nextmul = "mul"; public String nextadd = "add"; public String thePressedKey; public int theRounding = 10; static boolean inApplet = true; public static void main(String args[]) { inApplet = false; Frame appletFrame = new Frame("Java Calculator"); Applet thisCal = new Calculator(); thisCal.init(); thisCal.start(); //appletFrame.setLayout(new FlowLayout()); appletFrame.add("Center",thisCal); appletFrame.resize(416,238); appletFrame.show(); } public void init() { super.init(); //{{INIT_CONTROLS setLayout(null); resize(301,163); setBackground(new Color(65280)); panel1 = new java.awt.Panel(); panel1.setLayout(new GridLayout(0,8,1,1)); panel1.reshape(3,24,297,90); panel1.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.setBackground(new Color(6343310)); add(panel1); Group1 = new CheckboxGroup(); bDeg = new java.awt.Checkbox("Deg", Group1, true); bDeg.reshape(0,0,36,14); bDeg.setFont(new Font("Helvetica", Font.PLAIN, 9)); panel1.add(bDeg); bRad = new java.awt.Checkbox("Rad", Group1, false); bRad.reshape(37,0,36,14); bRad.setFont(new Font("Helvetica", Font.PLAIN, 9)); panel1.add(bRad); b10 = new java.awt.Button("E"); b10.reshape(74,0,36,14); b10.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(b10); label3 = new java.awt.Label(""); label3.reshape(111,0,36,14); label3.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(label3); label1 = new java.awt.Label(""); label1.reshape(148,0,36,14); label1.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(label1); label2 = new java.awt.Label(""); label2.reshape(185,0,36,14); label2.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(label2); bCE = new java.awt.Button("CE"); bCE.reshape(222,0,36,14); bCE.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(bCE); bC = new java.awt.Button("C"); bC.reshape(259,0,36,14); bC.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(bC); b1 = new java.awt.Button("1"); b1.reshape(0,15,36,14); b1.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(b1); b2 = new java.awt.Button("2"); b2.reshape(37,15,36,14); b2.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(b2); b3 = new java.awt.Button("3"); b3.reshape(74,15,36,14); b3.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(b3); bPlus = new java.awt.Button("+"); bPlus.reshape(111,15,36,14); bPlus.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(bPlus); bSin = new java.awt.Button("sin"); bSin.reshape(148,15,36,14); bSin.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(bSin); bCos = new java.awt.Button("cos"); bCos.reshape(185,15,36,14); bCos.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(bCos); bTan = new java.awt.Button("tan"); bTan.reshape(222,15,36,14); bTan.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(bTan); bCot = new java.awt.Button("cot"); bCot.reshape(259,15,36,14); bCot.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(bCot); b4 = new java.awt.Button("4"); b4.reshape(0,30,36,14); b4.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(b4); b5 = new java.awt.Button("5"); b5.reshape(37,30,36,14); b5.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(b5); b6 = new java.awt.Button("6"); b6.reshape(74,30,36,14); b6.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(b6); bMinus = new java.awt.Button("-"); bMinus.reshape(111,30,36,14); bMinus.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(bMinus); bAsin = new java.awt.Button("asin"); bAsin.reshape(148,30,36,14); bAsin.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(bAsin); bAcos = new java.awt.Button("acos"); bAcos.reshape(185,30,36,14); bAcos.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(bAcos); bAtan = new java.awt.Button("atan"); bAtan.reshape(222,30,36,14); bAtan.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(bAtan); bAcot = new java.awt.Button("acot"); bAcot.reshape(259,30,36,14); bAcot.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(bAcot); b7 = new java.awt.Button("7"); b7.reshape(0,45,36,14); b7.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(b7); b8 = new java.awt.Button("8"); b8.reshape(37,45,36,14); b8.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(b8); b9 = new java.awt.Button("9"); b9.reshape(74,45,36,14); b9.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(b9); bMul = new java.awt.Button("*"); bMul.reshape(111,45,36,14); bMul.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(bMul); bLn = new java.awt.Button("ln x"); bLn.reshape(148,45,36,14); bLn.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(bLn); bLog = new java.awt.Button("log x"); bLog.reshape(185,45,36,14); bLog.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(bLog); bX2 = new java.awt.Button("x^2"); bX2.reshape(222,45,36,14); bX2.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(bX2); bXY = new java.awt.Button("y^x"); bXY.reshape(259,45,36,14); bXY.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(bXY); bDot = new java.awt.Button("."); bDot.reshape(0,60,36,14); bDot.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(bDot); b0 = new java.awt.Button("0"); b0.reshape(37,60,36,14); b0.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(b0); bSign = new java.awt.Button("+/-"); bSign.reshape(74,60,36,14); bSign.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(bSign); bDiv = new java.awt.Button("/"); bDiv.reshape(111,60,36,14); bDiv.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(bDiv); bExp = new java.awt.Button("e^x"); bExp.reshape(148,60,36,14); bExp.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(bExp); bExp10 = new java.awt.Button("10^x"); bExp10.reshape(185,60,36,14); bExp10.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(bExp10); bRoot = new java.awt.Button("sqrt(x)"); bRoot.reshape(222,60,36,14); bRoot.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(bRoot); bOver = new java.awt.Button("1/x"); bOver.reshape(259,60,36,14); bOver.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(bOver); bEqual = new java.awt.Button("="); bEqual.reshape(0,75,36,14); bEqual.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(bEqual); label4 = new java.awt.Label(""); label4.reshape(37,75,36,14); label4.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(label4); label5 = new java.awt.Label(""); label5.reshape(74,75,36,14); label5.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(label5); label6 = new java.awt.Label(""); label6.reshape(111,75,36,14); label6.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(label6); bPi = new java.awt.Button("pi"); bPi.reshape(148,75,36,14); bPi.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(bPi); bRan = new java.awt.Button("ran"); bRan.reshape(185,75,36,14); bRan.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(bRan); bFact = new java.awt.Button("x!"); bFact.reshape(222,75,36,14); bFact.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(bFact); bHyp = new java.awt.Button("hyp"); bHyp.reshape(259,75,36,14); bHyp.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel1.add(bHyp); panel2 = new java.awt.Panel(); panel2.setLayout(new GridLayout(0,1,1,1)); panel2.reshape(3,114,50,48); panel2.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel2.setBackground(new Color(6343310)); add(panel2); inM1 = new java.awt.Button("Disp>M1"); inM1.reshape(0,0,50,15); inM1.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel2.add(inM1); inM2 = new java.awt.Button("Disp>M2"); inM2.reshape(0,16,50,15); inM2.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel2.add(inM2); inM3 = new java.awt.Button("Disp>M3"); inM3.reshape(0,32,50,15); inM3.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel2.add(inM3); panel3 = new java.awt.Panel(); panel3.setLayout(new GridLayout(0,1,1,1)); panel3.reshape(248,114,50,48); panel3.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel3.setBackground(new Color(6343310)); add(panel3); outM1 = new java.awt.Button("M1>Disp"); outM1.reshape(0,0,50,15); outM1.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel3.add(outM1); outM2 = new java.awt.Button("M2>Disp"); outM2.reshape(0,16,50,15); outM2.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel3.add(outM2); outM3 = new java.awt.Button("M3>Disp"); outM3.reshape(0,32,50,15); outM3.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel3.add(outM3); panel4 = new java.awt.Panel(); panel4.setLayout(new GridLayout(0,1,1,0)); panel4.reshape(55,114,191,50); panel4.setFont(new Font("Helvetica", Font.PLAIN, 10)); panel4.setBackground(new Color(1602868)); add(panel4); M1 = new java.awt.TextField(); M1.setText("0"); M1.reshape(0,0,191,16); M1.setFont(new Font("Helvetica", Font.PLAIN, 9)); M1.setForeground(new Color(16711680)); M1.setBackground(new Color(0)); panel4.add(M1); M2 = new java.awt.TextField(); M2.setText("0"); M2.reshape(0,16,191,16); M2.setFont(new Font("Helvetica", Font.PLAIN, 9)); M2.setForeground(new Color(16711680)); M2.setBackground(new Color(0)); panel4.add(M2); M3 = new java.awt.TextField(); M3.setText("0"); M3.reshape(0,32,191,16); M3.setFont(new Font("Helvetica", Font.PLAIN, 9)); M3.setForeground(new Color(16711680)); M3.setBackground(new Color(0)); panel4.add(M3); lcd = new java.awt.TextField(); lcd.setText("0"); lcd.reshape(3,3,295,20); lcd.setFont(new Font("Helvetica", Font.BOLD, 14)); lcd.setForeground(new Color(16711680)); lcd.setBackground(new Color(0)); add(lcd); //}} } public boolean handleEvent(Event event) { // Switch Degrees/Radians if((event.target instanceof Checkbox) && (event.id == Event.ACTION_EVENT)) { Checkbox currentCheckbox = (Checkbox)event.target; boolean checkboxState = currentCheckbox.getState(); if(currentCheckbox == bDeg){ if(checkboxState){ degrees = true; } } else if(currentCheckbox == bRad){ if(checkboxState){ degrees = false; } } return true; } // Pressing a key on the keyboard if (event.id == Event.KEY_PRESS) { thePressedKey = ""+(char)event.key; if (zeroLcd) { lcd.setText(""); zeroLcd = false; } if (thePressedKey.equalsIgnoreCase("w")) { lcd.setText("© W. Bauer, 1999"); return true; } //System.out.println(thePressedKey+"|"+event.toString()); // If this is the delete key, then remove last character and re-display if (event.key == 8){ String lcdNew = lcd.getText(); int lcdL = lcdNew.length(); if (lcdL > 1) { lcdNew = lcdNew.substring(0,lcdL-1); // removes last char lcdVal = convertToNumber(lcdNew); } } } // hyp button activates hyperbolic functions for sin, cos, tan, and cot if (event.target == bHyp && event.id == Event.ACTION_EVENT) { hypFlag = true; return true; } // Numbers if ((event.target == b1 && event.id == Event.ACTION_EVENT) || (event.id == Event.KEY_PRESS && thePressedKey.equals("1"))) { bAction("1"); return true; } if ((event.target == b2 && event.id == Event.ACTION_EVENT) || (event.id == Event.KEY_PRESS && thePressedKey.equals("2"))) { bAction("2"); return true; } if ((event.target == b3 && event.id == Event.ACTION_EVENT) || (event.id == Event.KEY_PRESS && thePressedKey.equals("3"))) { bAction("3"); return true; } if ((event.target == b4 && event.id == Event.ACTION_EVENT) || (event.id == Event.KEY_PRESS && thePressedKey.equals("4"))) { bAction("4"); return true; } if ((event.target == b5 && event.id == Event.ACTION_EVENT) || (event.id == Event.KEY_PRESS && thePressedKey.equals("5"))) { bAction("5"); return true; } if ((event.target == b6 && event.id == Event.ACTION_EVENT) || (event.id == Event.KEY_PRESS && thePressedKey.equals("6"))) { bAction("6"); return true; } if ((event.target == b7 && event.id == Event.ACTION_EVENT) || (event.id == Event.KEY_PRESS && thePressedKey.equals("7"))) { bAction("7"); return true; } if ((event.target == b8 && event.id == Event.ACTION_EVENT) || (event.id == Event.KEY_PRESS && thePressedKey.equals("8"))) { bAction("8"); return true; } if ((event.target == b9 && event.id == Event.ACTION_EVENT) || (event.id == Event.KEY_PRESS && thePressedKey.equals("9"))) { bAction("9"); return true; } if ((event.target == b0 && event.id == Event.ACTION_EVENT) || (event.id == Event.KEY_PRESS && thePressedKey.equals("0"))) { bAction("0"); return true; } if ((event.target == b10 && event.id == Event.ACTION_EVENT) || (event.id == Event.KEY_PRESS && thePressedKey.equalsIgnoreCase("e"))) { bAction("e"); dotMissing = false; eSet = true; return true; } if ((event.target == bDot && event.id == Event.ACTION_EVENT) || (event.id == Event.KEY_PRESS && thePressedKey.equals("."))) { if (zeroLcd) { lcd.setText("0."); zeroLcd = false; } else { if (dotMissing){ lcd.setText(lcd.getText()+"."); dotMissing = false; } } makeLcdNumber(); hypFlag = false; return true; } if ((event.target == bC && event.id == Event.ACTION_EVENT) || (event.id == Event.KEY_PRESS && thePressedKey.equalsIgnoreCase("c"))) { // Clear All lcd.setText("0"); lcdVal = 0.0; theSum = 0.0; theTerm = 0.0; dotMissing = true; zeroLcd = true; prevMult = false; expFlag = false; eSet = false; hypFlag = false; return true; } if (event.target == bCE && event.id == Event.ACTION_EVENT) { // Clear last lcd.setText("0"); lcdVal = 0.0; dotMissing = true; zeroLcd = true; eSet = false; hypFlag = false; return true; } if (event.target == bPi && event.id == Event.ACTION_EVENT) { // Pi lcdVal = Math.PI; displayNumber(lcdVal,theRounding); dotMissing = false; zeroLcd = true; return true; } if (event.target == inM1 && event.id == Event.ACTION_EVENT) { // Store in mem. M1.setText(lcd.getText()); hypFlag = false; return true; } if (event.target == inM2 && event.id == Event.ACTION_EVENT) { M2.setText(lcd.getText()); hypFlag = false; return true; } if (event.target == inM3 && event.id == Event.ACTION_EVENT) { M3.setText(lcd.getText()); hypFlag = false; return true; } if (event.target == outM1 && event.id == Event.ACTION_EVENT) { // Recall from mem. lcd.setText(M1.getText()); makeLcdNumber(); zeroLcd = true; hypFlag = false; return true; } if (event.target == outM2 && event.id == Event.ACTION_EVENT) { lcd.setText(M2.getText()); makeLcdNumber(); zeroLcd = true; hypFlag = false; return true; } if (event.target == outM3 && event.id == Event.ACTION_EVENT) { lcd.setText(M3.getText()); makeLcdNumber(); zeroLcd = true; hypFlag = false; return true; } if (event.target == bLn && event.id == Event.ACTION_EVENT) { // Natural log lcdVal = Math.log(lcdVal); displayNumber(lcdVal,theRounding); return true; } if (event.target == bLog && event.id == Event.ACTION_EVENT) { // log base 10 lcdVal = Math.log(lcdVal)/Math.log(10.0d); displayNumber(lcdVal,theRounding); return true; } if (event.target == bExp && event.id == Event.ACTION_EVENT) { // exp(x) lcdVal = Math.exp(lcdVal); displayNumber(lcdVal,theRounding); return true; } if (event.target == bExp10 && event.id == Event.ACTION_EVENT) { // 10^(x) lcdVal = Math.pow(10.0d,lcdVal); displayNumber(lcdVal,theRounding); return true; } if (event.target == bX2 && event.id == Event.ACTION_EVENT) { // x^2 lcdVal = lcdVal*lcdVal; displayNumber(lcdVal,theRounding); return true; } if (event.target == bFact && event.id == Event.ACTION_EVENT) { // x! int nx = (int)lcdVal; if (lcdVal-nx == 0) { if (nx > 0 && nx < 171) { int iii = 0; double theFac = 1; while (iii < nx){ iii++; theFac = theFac * (double)iii; } lcdVal = theFac; displayNumber(lcdVal,theRounding); } else { lcd.setText("Error: out of range"); } } else { lcd.setText("Err: x must be integer"); } return true; } if (event.target == bRan && event.id == Event.ACTION_EVENT) { // random number lcdVal = Math.random(); displayNumber(lcdVal,theRounding); return true; } if (event.target == bRoot && event.id == Event.ACTION_EVENT) { // sqrt(x) lcdVal = Math.sqrt(lcdVal); displayNumber(lcdVal,theRounding); return true; } if (event.target == bSin && event.id == Event.ACTION_EVENT) { // sin(x) if (hypFlag) { // hyperbolic function lcdVal = 0.5d*(Math.exp(lcdVal) - Math.exp(-lcdVal)); } else { if (degrees){ lcdVal = Math.sin(lcdVal*Math.PI/180.0); } else { lcdVal = Math.sin(lcdVal); } } displayNumber(lcdVal,theRounding); return true; } if (event.target == bCos && event.id == Event.ACTION_EVENT) { // cos(x) if (hypFlag) { // hyperbolic function lcdVal = 0.5d*(Math.exp(lcdVal) + Math.exp(-lcdVal)); } else { if (degrees){ lcdVal = Math.cos(lcdVal*Math.PI/180.0); } else { lcdVal = Math.cos(lcdVal); } } displayNumber(lcdVal,theRounding); return true; } if (event.target == bTan && event.id == Event.ACTION_EVENT) { // tan(x) if (hypFlag) { // hyperbolic function lcdVal = (Math.exp(2.0d*lcdVal) - 1.0d)/ (Math.exp(2.0d*lcdVal) + 1.0d); } else { if (degrees){ lcdVal = Math.tan(lcdVal*Math.PI/180.0); } else { lcdVal = Math.tan(lcdVal); } } displayNumber(lcdVal,theRounding); return true; } if (event.target == bCot && event.id == Event.ACTION_EVENT) { // cot(x) if (hypFlag) { // hyperbolic function lcdVal = (Math.exp(lcdVal) + Math.exp(-lcdVal))/ (Math.exp(lcdVal) - Math.exp(-lcdVal)); } else { if (degrees){ lcdVal = 1.0/Math.tan(lcdVal*Math.PI/180.0); } else { lcdVal = 1.0/Math.tan(lcdVal); } } displayNumber(lcdVal,theRounding); return true; } if (event.target == bAsin && event.id == Event.ACTION_EVENT) { // asin(x) if (hypFlag) { // hyperbolic function lcdVal = Math.log(lcdVal + Math.pow((lcdVal*lcdVal+1.0d),0.5d)); } else { if (degrees){ lcdVal = Math.asin(lcdVal)*180.0/Math.PI; } else { lcdVal = Math.asin(lcdVal); } } displayNumber(lcdVal,theRounding); return true; } if (event.target == bAcos && event.id == Event.ACTION_EVENT) { // acos(x) if (hypFlag) { // hyperbolic function if (Math.abs(lcdVal) < 1.0d) { lcd.setText("Error: |x|<1"); return true; } else { double theSign = lcdVal/Math.abs(lcdVal); lcdVal = Math.log(lcdVal + theSign*Math.pow((lcdVal*lcdVal-1.0d),0.5d)); } } else { if (degrees){ lcdVal = Math.acos(lcdVal)*180.0/Math.PI; } else { lcdVal = Math.acos(lcdVal); } } displayNumber(lcdVal,theRounding); return true; } if (event.target == bAtan && event.id == Event.ACTION_EVENT) { // atan(x) if (hypFlag) { // hyperbolic function if (Math.abs(lcdVal) < 1.0d) { lcdVal = 0.5d*Math.log((1.0d+lcdVal)/(1.0d-lcdVal)); } else { lcd.setText("Error: |x|>1"); return true; } } else { if (degrees){ lcdVal = Math.atan(lcdVal)*180.0/Math.PI; } else { lcdVal = Math.atan(lcdVal); } } displayNumber(lcdVal,theRounding); return true; } if (event.target == bAcot && event.id == Event.ACTION_EVENT) { // acot(x) if (hypFlag) { // hyperbolic function if (Math.abs(lcdVal) < 1.0d) { lcd.setText("Error: |x|<1"); return true; } else { lcdVal = 0.5d*Math.log((lcdVal+1.0d)/(lcdVal-1.0d)); } } else { if (degrees){ lcdVal = Math.atan(1.0/lcdVal)*180.0/Math.PI; } else { lcdVal = Math.atan(1.0/lcdVal); } } displayNumber(lcdVal,theRounding); return true; } if ((event.target == bXY && event.id == Event.ACTION_EVENT) || (event.id == Event.KEY_PRESS && thePressedKey.equals("^"))) { // y^x expFlag = true; expBase = lcdVal; zeroLcd = true; return true; } if ((event.target == bMul && event.id == Event.ACTION_EVENT) || (event.id == Event.KEY_PRESS && thePressedKey.equals("*"))) { // * calculateIt("mult"); nextmul = "mult"; return true; } if ((event.target == bDiv && event.id == Event.ACTION_EVENT) || (event.id == Event.KEY_PRESS && thePressedKey.equals("/"))) { // / calculateIt("mult"); nextmul = "div"; return true; } if ((event.target == bPlus && event.id == Event.ACTION_EVENT) || (event.id == Event.KEY_PRESS && thePressedKey.equals("+"))) { // + calculateIt("add"); nextadd = "add"; return true; } if ((event.target == bMinus && event.id == Event.ACTION_EVENT) || (event.id == Event.KEY_PRESS && thePressedKey.equals("-"))) { // - calculateIt("add"); nextadd = "sub"; return true; } if ((((event.target == bEqual) || (event.target == lcd)) && event.id == Event.ACTION_EVENT) || (event.id == Event.KEY_PRESS && thePressedKey.equals("="))) { // = or Return Key calculateIt("add"); theSum = 0; nextadd = "add"; nextmul = "mul"; expFlag = false; return true; } if (event.target == bSign && event.id == Event.ACTION_EVENT) { // Sign Change if (eSet || zeroLcd) { bAction("-"); } else { makeLcdNumber(); lcdVal = - lcdVal; displayNumber(lcdVal,theRounding); } return true; } if (event.target == bOver && event.id == Event.ACTION_EVENT) { // 1/x lcdVal = 1.0/lcdVal; displayNumber(lcdVal,theRounding); return true; } if (event.target instanceof Button && event.id == Event.ACTION_EVENT) System.out.println("!undefined!"); return false; } void calculateIt(String kind){ if (expFlag){ lcdVal = Math.pow(expBase,lcdVal); expFlag = false; } if (kind == "add"){ if (prevMult) { if (nextmul == "mult"){ theTerm = theTerm * lcdVal; } else { theTerm = theTerm / lcdVal; } } else { theTerm = lcdVal; } if (nextadd == "add"){ theSum = theSum + theTerm; } else { theSum = theSum - theTerm; } displayNumber(theSum,theRounding); prevMult = false; } else if (kind == "mult"){ if(prevMult){ if (nextmul == "mult"){ theTerm = theTerm * lcdVal; } else { theTerm = theTerm / lcdVal; } } else { theTerm = lcdVal; prevMult = true; } displayNumber(theTerm,theRounding); } else { //System.out.println("Problem "+kind); } zeroLcd = true; dotMissing = true; } void bAction(String s) { if (zeroLcd) { lcd.setText(s); zeroLcd = false; } else { lcd.setText(lcd.getText()+s); } makeLcdNumber(); } void makeLcdNumber(){ String lcdString = lcd.getText().trim(); lcdVal = convertToNumber(lcdString); } // method takes an input string and returns it as a double without truncating precision public double convertToNumber(String inString){ double outNum = 0.0; double ounMant = 0.0; double outExp = 0.0; double outSign = 1.0; String inExp = ""; String inMant = ""; inString = inString.trim(); int myLength = inString.length(); int myDotPos = inString.indexOf("."); int myEpos = inString.indexOf("e"); if (myEpos < 0) myEpos = inString.indexOf("E"); // separate into mantissa and exponent and evaluate exponent if (!(myEpos<0)) { inExp = inString.substring(myEpos+1); inMant = inString.substring(0,myEpos); try { Double myDouble1 = new Double(inExp); outExp = myDouble1.doubleValue(); } catch (Exception e) { } } else { inMant = inString; } // parse mantissa and convert to number if (myEpos == 0) { // e is the first char => no mantissa outNum = 1.0d; } else { if (inMant.substring(0,1).equals("-")) { // negative number inMant = inMant.substring(1); outSign = -1.0; } myDotPos = inMant.indexOf("."); if (myDotPos<0) myDotPos = inMant.length(); outNum = 0.0; int i=0; while (i < inMant.length()){ char c = inMant.charAt(i); if (Character.isDigit(c)){ if (i > myDotPos) { outNum += Character.digit(c,10)*Math.pow(10,myDotPos-i); } else { outNum += Character.digit(c,10)*Math.pow(10,myDotPos-i-1); } } i++; } } outNum = outNum*outSign*Math.pow(10,outExp); /* System.out.println("e:"+myEpos+";.:"+myDotPos+";length"+myLength); System.out.println(inString+"|"+inMant+"|"+inExp); System.out.println("outExp:"+outExp+"; outNum:"+outNum); System.out.println("-------------------------------"); */ return outNum; } public void displayNumber(double theNum,int thePrecision) { StringBuffer sss = new StringBuffer(); double myMantissa = 0.0; double myExponent = 0.0; int num1 = 0; double dnum1; double rem1 = 0.0; boolean neg = false; zeroLcd = true; hypFlag = false; if (theNum < 0) { neg = true; } //System.out.println("theNum:"+theNum+"; neg:"+neg); String myString = ""+theNum; int myLength = myString.length(); int myDotPos = myString.indexOf("."); //System.out.println("myString:"+myString); if (myDotPos < 0) { // Integer => return unchanged lcd.setText(myString); } else { if (neg) { theNum = -theNum; // avoid the minus sign, need to add it later to output string myString = ""+theNum; } // == Step 1 == split the input number into its exponent // and Mantissa int myEpos = Math.max(myString.indexOf("e"),myString.indexOf("E")); String stringExponent = myString.substring(myEpos+1); if (myEpos < 0){ myMantissa = theNum; } else { try { // Here I convert the String to Integer Double myD = new Double(stringExponent); myExponent = myD.doubleValue(); } catch (Exception e) { } myMantissa = theNum/Math.pow(10,myExponent); } // == Step 2 == process the mantissa num1 = (int)Math.floor(myMantissa); if (neg) { sss.append("-").append(num1).append("."); } else { sss.append(num1).append("."); } rem1 = 10.0d*(myMantissa-(double)num1); //System.out.println("0;num1:"+num1+";rem1:"+rem1+";sss:"+sss); int i = 2; if (num1 > 9) i++; if (num1 > 99) i++; if (num1 > 999) i++; while (i < thePrecision){ dnum1 = Math.floor(rem1); num1 = (int)Math.floor(rem1); sss.append(num1); rem1 = 10.0d*(rem1-dnum1); i++; //System.out.println(i+"num1:"+num1+";rem1:"+rem1+";sss:"+sss); } num1 = (int)Math.round(rem1); sss.append(num1); if (!(myEpos < 0)) { sss.append("e").append(stringExponent); } //System.out.println(sss); String outString = sss.toString(); lcd.setText(outString); } } public String getAppletInfo() { return "Java-Calculator ** " + "Author: Wolfgang Bauer ** " + "Copyright, 1997"; } //{{DECLARE_CONTROLS java.awt.Panel panel1; java.awt.Checkbox bDeg; CheckboxGroup Group1; java.awt.Checkbox bRad; java.awt.Button b10; java.awt.Label label3; java.awt.Label label1; java.awt.Label label2; java.awt.Button bCE; java.awt.Button bC; java.awt.Button b1; java.awt.Button b2; java.awt.Button b3; java.awt.Button bPlus; java.awt.Button bSin; java.awt.Button bCos; java.awt.Button bTan; java.awt.Button bCot; java.awt.Button b4; java.awt.Button b5; java.awt.Button b6; java.awt.Button bMinus; java.awt.Button bAsin; java.awt.Button bAcos; java.awt.Button bAtan; java.awt.Button bAcot; java.awt.Button b7; java.awt.Button b8; java.awt.Button b9; java.awt.Button bMul; java.awt.Button bLn; java.awt.Button bLog; java.awt.Button bX2; java.awt.Button bXY; java.awt.Button bDot; java.awt.Button b0; java.awt.Button bSign; java.awt.Button bDiv; java.awt.Button bExp; java.awt.Button bExp10; java.awt.Button bRoot; java.awt.Button bOver; java.awt.Button bEqual; java.awt.Label label4; java.awt.Label label5; java.awt.Label label6; java.awt.Button bPi; java.awt.Button bRan; java.awt.Button bFact; java.awt.Button bHyp; java.awt.Panel panel2; java.awt.Button inM1; java.awt.Button inM2; java.awt.Button inM3; java.awt.Panel panel3; java.awt.Button outM1; java.awt.Button outM2; java.awt.Button outM3; java.awt.Panel panel4; java.awt.TextField M1; java.awt.TextField M2; java.awt.TextField M3; java.awt.TextField lcd; //}} }