/* *Java script: prog6_calc.java *Author: Ashwin Prakash *Bioinformatics Lab, Department of Medicine *HSC, University of Toledo, Toledo, Ohio, USA *written January, 2011 *Contact ashwin.prakash@rockets.utoledo.edu * *LICENSE: GPL version 3 or later */ /* * The program provides a web based GUI for a simple implementation of a calculator applet * The user can enter numerical values within the text area in the applet and execute mathematical * operators to obtain results of these calculations within the above mentioned text area. * The calculator also provides certain scientific functions such as log, ln, etc. * The applet also houses a random number generator which can be executed using the "rand" button * The applet also includes a Memory function which can be used to store one number, which can be * retreived later and used for further calculations. */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.lang.*; public class prog6_calc extends JApplet implements ActionListener{ Container c; TextField ta1; //JLabel L1; JButton beq, bcs, bcl, b1, b2, b3, b4, b5, b6, b7, b8, b9, b0, bdot; JButton bM, badd, bmul, bdiv, bsub; JButton blog, bln, bsqrt, bcbrt, binv; JButton bround, bexp, bsqr, bcub, brand; String num; String operate=""; String mem = ""; //String useFloat="NO"; public void init(){ c=getContentPane(); c.setLayout(null); ta1 = new TextField(); ta1.setLocation(10,40); ta1.setSize(435, 50); beq = new JButton("="); beq.setLocation(452, 40); beq.setSize(50,50); beq.addActionListener(this); bcl = new JButton("Bk"); bcl.setLocation(510, 40); bcl.setSize(60,50); bcl.addActionListener(this); //------------------------------------------------------------------------------------- b1 = new JButton("1"); b1.setLocation(10, 100); b1.setSize(75,75); b1.addActionListener(this); b2 = new JButton("2"); b2.setLocation(100, 100); b2.setSize(75,75); b2.addActionListener(this); b3 = new JButton("3"); b3.setLocation(190, 100); b3.setSize(75,75); b3.addActionListener(this); b4 = new JButton("4"); b4.setLocation(10, 180); b4.setSize(75,75); b4.addActionListener(this); b5 = new JButton("5"); b5.setLocation(100, 180); b5.setSize(75,75); b5.addActionListener(this); b6 = new JButton("6"); b6.setLocation(190, 180); b6.setSize(75,75); b6.addActionListener(this); b7 = new JButton("7"); b7.setLocation(10, 260); b7.setSize(75,75); b7.addActionListener(this); b8 = new JButton("8"); b8.setLocation(100, 260); b8.setSize(75,75); b8.addActionListener(this); b9 = new JButton("9"); b9.setLocation(190, 260); b9.setSize(75,75); b9.addActionListener(this); bM = new JButton("M"); bM.setLocation(10, 340); bM.setSize(75,75); bM.addActionListener(this); b0 = new JButton("0"); b0.setLocation(100, 340); b0.setSize(75,75); b0.addActionListener(this); bdot = new JButton("."); bdot.setLocation(190, 340); bdot.setSize(75,75); bdot.addActionListener(this); //------------------------------------------------------------------------------------- badd = new JButton("+"); badd.setLocation(280, 100); badd.setSize(90,55); badd.addActionListener(this); bsub = new JButton("-"); bsub.setLocation(280, 160); bsub.setSize(90,55); bsub.addActionListener(this); bmul = new JButton("x"); bmul.setLocation(280, 220); bmul.setSize(90,55); bmul.addActionListener(this); bdiv = new JButton("/"); bdiv.setLocation(280, 280); bdiv.setSize(90,55); bdiv.addActionListener(this); brand = new JButton("rand"); brand.setLocation(280, 340); brand.setSize(90,75); brand.addActionListener(this); //------------------------------------------------------------------------------------- blog = new JButton("log"); blog.setLocation(380, 100); blog.setSize(90,55); blog.addActionListener(this); bln = new JButton("ln"); bln.setLocation(380, 160); bln.setSize(90,55); bln.addActionListener(this); bsqrt = new JButton("sqrt"); bsqrt.setLocation(380, 220); bsqrt.setSize(90,55); bsqrt.addActionListener(this); bcbrt = new JButton("cbrt"); bcbrt.setLocation(380, 280); bcbrt.setSize(90,55); bcbrt.addActionListener(this); binv = new JButton("1/x"); binv.setLocation(380, 340); binv.setSize(90,75); binv.addActionListener(this); //------------------------------------------------------------------------------------- bround = new JButton("round"); bround.setLocation(480, 100); bround.setSize(90,55); bround.addActionListener(this); bexp = new JButton("exp"); bexp.setLocation(480, 160); bexp.setSize(90,55); bexp.addActionListener(this); bsqr = new JButton("x^2"); bsqr.setLocation(480, 220); bsqr.setSize(90,55); bsqr.addActionListener(this); bcub = new JButton("x^3"); bcub.setLocation(480, 280); bcub.setSize(90,55); bcub.addActionListener(this); bcs = new JButton("CE"); bcs.setLocation(480, 340); bcs.setSize(90,75); bcs.addActionListener(this); //------------------------------------------------------------------------------------- //c.add(L1); c.add(ta1); c.add(beq); c.add(bcl); c.add(b1); c.add(b2); c.add(b3); c.add(b4); c.add(b5); c.add(b6); c.add(b7); c.add(b8); c.add(b9); c.add(bM); c.add(b0); c.add(bdot); c.add(badd); c.add(bsub); c.add(bmul); c.add(bdiv); c.add(bcs); c.add(blog); c.add(bln); c.add(bsqrt); c.add(bcbrt); c.add(binv); c.add(bround); c.add(bexp); c.add(bsqr); c.add(bcub); c.add(brand); } //------------------------------------------------------------------------------------- public void actionPerformed(ActionEvent e){ if(e.getSource()==beq){ String x = ta1.getText(); if(x==""){ ta1.setText(""+num); } //if(useFloat=="YA"){ double num2 = Double.parseDouble(x); double num1 = Double.parseDouble(num); double ans; if(operate=="+"){ ans = num1 + num2; ta1.setText(""+ans); } if(operate == "-"){ ans = num1-num2; ta1.setText(""+ans); } if(operate == "x"){ ans = num1*num2; ta1.setText(""+ans); } if(operate == "/"){ ans = num1/num2; ta1.setText(""+ans); } /*}else if(useFloat=="NO"){ int num2 = Integer.parseInt(x); int num1 = Integer.parseInt(num); int ans; if(operate=="+"){ ans = num1 + num2; ta1.setText(""+ans); } if(operate == "-"){ ans = num1-num2; ta1.setText(""+ans); } if(operate == "x"){ ans = num1*num2; ta1.setText(""+ans); } if(operate == "/"){ ans = num1/num2; ta1.setText(""+ans); } }*/ } if(e.getSource()==bcl){ String x = ta1.getText(); int len = x.length(); if(len>0){x = x.substring(0, len-1);} else{}; ta1.setText(x); } if(e.getSource()==bdot){ String x = ta1.getText(); if(x.contains(".")==false){ //useFloat="YA"; ta1.setText(x+"."); }else{ ta1.setText(x); } } if(e.getSource()==bcs){ ta1.setText(""); } if(e.getSource()==b1){ String x = ta1.getText(); ta1.setText(x+"1"); } if(e.getSource()==b2){ String x = ta1.getText(); ta1.setText(x+"2"); } if(e.getSource()==b3){ String x = ta1.getText(); ta1.setText(x+"3"); } if(e.getSource()==b4){ String x = ta1.getText(); ta1.setText(x+"4"); } if(e.getSource()==b5){ String x = ta1.getText(); ta1.setText(x+"5"); } if(e.getSource()==b6){ String x = ta1.getText(); ta1.setText(x+"6"); } if(e.getSource()==b0){ String x = ta1.getText(); ta1.setText(x+"0"); } if(e.getSource()==b7){ String x = ta1.getText(); ta1.setText(x+"7"); } if(e.getSource()==b8){ String x = ta1.getText(); ta1.setText(x+"8"); } if(e.getSource()==b9){ String x = ta1.getText(); ta1.setText(x+"9"); } if(e.getSource()==bM){ String x = ta1.getText(); if(x.length()>0){ mem = x; }else{ ta1.setText(x+mem); } } if(e.getSource()==badd){ String x = ta1.getText(); num = x; operate = "+"; ta1.setText(""); } if(e.getSource()==bsub){ String x = ta1.getText(); if(x.length()>0){ num = x; operate = "-"; ta1.setText(""); }else{ta1.setText(x+"-");} } if(e.getSource()==bmul){ String x = ta1.getText(); num = x; operate = "x"; ta1.setText(""); } if(e.getSource()==bdiv){ String x = ta1.getText(); num = x; operate = "/"; ta1.setText(""); } if(e.getSource()==bln){ String x = ta1.getText(); double num = Double.parseDouble(x); double ans = Math.log(num); ta1.setText(""+ans); } if(e.getSource()==blog){ String x = ta1.getText(); double num = Double.parseDouble(x); double ans = Math.log10(num); ta1.setText(""+ans); } if(e.getSource()==bsqrt){ String x = ta1.getText(); double num = Double.parseDouble(x); double ans = Math.sqrt(num); ta1.setText(""+ans); } if(e.getSource()==bcbrt){ String x = ta1.getText(); double num = Double.parseDouble(x); double ans = Math.cbrt(num); ta1.setText(""+ans); } if(e.getSource()==binv){ String x = ta1.getText(); double num = Double.parseDouble(x); double ans = 1/num; ta1.setText(""+ans); } if(e.getSource()==bround){ String x = ta1.getText(); float num = Float.parseFloat(x); float ans = Math.round(num); ta1.setText(""+ans); } if(e.getSource()==bexp){ String x = ta1.getText(); double num = Double.parseDouble(x); double ans = Math.exp(num); ta1.setText(""+ans); } if(e.getSource()==bsqr){ String x = ta1.getText(); float num = Float.parseFloat(x); float ans = num*num; ta1.setText(""+ans); } if(e.getSource()==bcub){ String x = ta1.getText(); float num = Float.parseFloat(x); float ans = num*num*num; ta1.setText(""+ans); } if(e.getSource()==brand){ double ans = Math.random(); ta1.setText(""+ans); } } }