Results 1 to 5 of 5
- 03-13-2011, 11:01 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 2
- Rep Power
- 0
Adding a sqrt button and percentage button to a calculator
Hi i have created this coding so far but am really struggling with adding a percentage button and square root button. i really have no idea and struggling a lot so any help would be much appreciated.
thanks
josie :)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator2 extends JApplet {
public void init() {
CalculatorPanel calc=new CalculatorPanel();
getContentPane().add(calc);
}
}
class CalculatorPanel extends JPanel implements ActionListener {
JButton
n1,n2,n3,n4,n5,n6,n7,n8,n9,n0,plus,minus,mul,div,d ot,equal,clr,sqrt;
static JTextField result=new JTextField("0",45);
static String lastCommand=null;
JOptionPane p=new JOptionPane();
double preRes=0,secVal=0,res;
private static void assign(String no)
{
if((result.getText()).equals("0"))
result.setText(no);
else if(lastCommand=="=")
{
result.setText(no);
lastCommand=null;
}
else
result.setText(result.getText()+no);
}
public CalculatorPanel() {
setLayout(new BorderLayout());
result.setEditable(false);
result.setSize(300,200);
add(result,BorderLayout.NORTH);
JPanel panel=new JPanel();
panel.setLayout(new GridLayout(5,5));
n7=new JButton("7");
panel.add(n7);
n7.addActionListener(this);
n8=new JButton("8");
panel.add(n8);
n8.addActionListener(this);
n9=new JButton("9");
panel.add(n9);
n9.addActionListener(this);
div=new JButton("/");
panel.add(div);
div.addActionListener(this);
n4=new JButton("4");
panel.add(n4);
n4.addActionListener(this);
n5=new JButton("5");
panel.add(n5);
n5.addActionListener(this);
n6=new JButton("6");
panel.add(n6);
n6.addActionListener(this);
mul=new JButton("*");
panel.add(mul);
mul.addActionListener(this);
n1=new JButton("1");
panel.add(n1);
n1.addActionListener(this);
n2=new JButton("2");
panel.add(n2);
n2.addActionListener(this);
n3=new JButton("3");
panel.add(n3);
n3.addActionListener(this);
minus=new JButton("-");
panel.add(minus);
minus.addActionListener(this);
dot=new JButton(".");
panel.add(dot);
dot.addActionListener(this);
n0=new JButton("0");
panel.add(n0);
n0.addActionListener(this);
equal=new JButton("=");
panel.add(equal);
equal.addActionListener(this);
plus=new JButton("+");
panel.add(plus);
plus.addActionListener(this);
add(panel,BorderLayout.CENTER);
clr=new JButton("c");
panel.add(clr);
clr.addActionListener(this);
sqrt=new JButton("sq");
panel.add(sqrt);
add(panel,BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==n1) assign("1");
else if(ae.getSource()==n2) assign("2");
else if(ae.getSource()==n3) assign("3");
else if(ae.getSource()==n4) assign("4");
else if(ae.getSource()==n5) assign("5");
else if(ae.getSource()==n6) assign("6");
else if(ae.getSource()==n7) assign("7");
else if(ae.getSource()==n8) assign("8");
else if(ae.getSource()==n9) assign("9");
else if(ae.getSource()==n0) assign("0");
else if(ae.getSource()==dot)
{
if(((result.getText()).indexOf("."))==-1)
result.setText(result.getText()+".");
}
else if(ae.getSource()==minus)
{
preRes=Double.parseDouble(result.getText());
lastCommand="-";
result.setText("");
}
else if(ae.getSource()==clr)
{
preRes=Double.parseDouble(result.getText());
lastCommand="c";
result.setText("");
}
else if(ae.getSource()==div)
{
preRes=Double.parseDouble(result.getText());
lastCommand="/";
result.setText("");
}
else if(ae.getSource()==div)
{
preRes=Double.parseDouble(result.getText());
lastCommand="sq";
result.setText("");
}
else if(ae.getSource()==equal)
{
secVal=Double.parseDouble(result.getText());
if (lastCommand.equals("/"))
res=preRes/secVal;
else if(lastCommand.equals("*"))
res=preRes*secVal;
else if(lastCommand.equals("-"))
res=preRes-secVal;
else if(lastCommand.equals("+"))
res=preRes+secVal;
result.setText(" "+res);
lastCommand="=";
}
else if(ae.getSource()==mul)
{
preRes=Double.parseDouble(result.getText());
lastCommand="*";
result.setText("");
}
else if(ae.getSource()==plus)
{
preRes=Double.parseDouble(result.getText());
lastCommand="+";
result.setText("");
}
}
}
- 03-13-2011, 11:06 PM #2
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
Math.sqrt() should solve one of your problems. As for the percentage button, 80 * 20% on a calculator is really just syntactic sugar for 80 * 0.2, so all you need to do is convert it to the appropriate multiplication operation.
- 03-14-2011, 12:09 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 2
- Rep Power
- 0
i have now changed my code to this but i can see its all wrong but feel with a couple of tweaks it will work. this is still in regards to the percentage and sqrt buttons
many thanks again
Java Code:import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Calculator2 extends JApplet { public void init() { CalculatorPanel calc=new CalculatorPanel(); getContentPane().add(calc); } } class CalculatorPanel extends JPanel implements ActionListener { JButton n1,n2,n3,n4,n5,n6,n7,n8,n9,n0,plus,minus,mul,div,dot,equal,clr,pcent,sqrt; static JTextField result=new JTextField("0",45); static String lastCommand=null; JOptionPane p=new JOptionPane(); double preRes=0,secVal=0,res; private static void assign(String no) { if((result.getText()).equals("0")) result.setText(no); else if(lastCommand=="=") { result.setText(no); lastCommand=null; } else result.setText(result.getText()+no); } public CalculatorPanel() { setLayout(new BorderLayout()); result.setEditable(false); result.setSize(300,200); add(result,BorderLayout.NORTH); JPanel panel=new JPanel(); panel.setLayout(new GridLayout(5,5)); n7=new JButton("7"); panel.add(n7); n7.addActionListener(this); n8=new JButton("8"); panel.add(n8); n8.addActionListener(this); n9=new JButton("9"); panel.add(n9); n9.addActionListener(this); div=new JButton("/"); panel.add(div); div.addActionListener(this); n4=new JButton("4"); panel.add(n4); n4.addActionListener(this); n5=new JButton("5"); panel.add(n5); n5.addActionListener(this); n6=new JButton("6"); panel.add(n6); n6.addActionListener(this); mul=new JButton("*"); panel.add(mul); mul.addActionListener(this); n1=new JButton("1"); panel.add(n1); n1.addActionListener(this); n2=new JButton("2"); panel.add(n2); n2.addActionListener(this); n3=new JButton("3"); panel.add(n3); n3.addActionListener(this); minus=new JButton("-"); panel.add(minus); minus.addActionListener(this); dot=new JButton("."); panel.add(dot); dot.addActionListener(this); n0=new JButton("0"); panel.add(n0); n0.addActionListener(this); equal=new JButton("="); panel.add(equal); equal.addActionListener(this); plus=new JButton("+"); panel.add(plus); plus.addActionListener(this); add(panel,BorderLayout.CENTER); clr=new JButton("c"); panel.add(clr); clr.addActionListener(this); pcent=new JButton("%"); panel.add(pcent); sqrt=new JButton("sq"); panel.add(sqrt); add(panel,BorderLayout.CENTER); } public void actionPerformed(ActionEvent ae) { if(ae.getSource()==n1) assign("1"); else if(ae.getSource()==n2) assign("2"); else if(ae.getSource()==n3) assign("3"); else if(ae.getSource()==n4) assign("4"); else if(ae.getSource()==n5) assign("5"); else if(ae.getSource()==n6) assign("6"); else if(ae.getSource()==n7) assign("7"); else if(ae.getSource()==n8) assign("8"); else if(ae.getSource()==n9) assign("9"); else if(ae.getSource()==n0) assign("0"); else if(ae.getSource()==dot) { if(((result.getText()).indexOf("."))==-1) result.setText(result.getText()+"."); } else if(ae.getSource()==minus) { preRes=Double.parseDouble(result.getText()); lastCommand="-"; result.setText(""); } else if(ae.getSource()==clr) { preRes=Double.parseDouble(result.getText()); lastCommand="c"; result.setText(""); } else if(ae.getSource()==div) { preRes=Double.parseDouble(result.getText()); lastCommand="/"; result.setText(""); } else if(ae.getSource()==equal) { secVal=Double.parseDouble(result.getText()); if (lastCommand.equals("/")) res=preRes/secVal; else if(lastCommand.equals("*")) res=preRes*secVal; else if(lastCommand.equals("-")) res=preRes-secVal; else if(lastCommand.equals("+")) res=preRes+secVal; else if(lastCommand.equals("sq")) res=Math.sqrt(preRes); result.setText(" "+res); lastCommand="="; } else if(ae.getSource()==pcent) { secVal=Double.parseDouble(result.getText()); if (lastCommand.equals("/")) res=preRes/(secVal/100); else if(lastCommand.equals("*")) res=preRes*(secVal/100); else if(lastCommand.equals("-")) res=preRes-(secVal/100); else if(lastCommand.equals("+")) res=preRes+(secVal/100); result.setText(" "+res); lastCommand="%"; } else if(ae.getSource()==mul) { preRes=Double.parseDouble(result.getText()); lastCommand="*"; result.setText(""); } else if(ae.getSource()==plus) { preRes=Double.parseDouble(result.getText()); lastCommand="+"; result.setText(""); } } }Last edited by Fubarable; 03-14-2011 at 01:14 AM. Reason: Moderator Edit: code tags added
-
So the problem is not calculating the square-root but rather adding a JButton and labelling it SQRT? Could you please clarify
-
Agrees with ozzy -- what specifically is your current question?
Also, I've edited your last post and have added code tags to your code to help it keep its formatting and be readable, since your goal is to get as many people to read your post and understand your code as possible, right?
To do this yourself next time, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.
Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:
Java Code:[code] // your code goes here // notice how the top and bottom tags are different [/code]
Similar Threads
-
Does any one know how to write this e^sqrt(lognloglog(n)) ,where n is biginteger
By ajay.eeralla in forum New To JavaReplies: 3Last Post: 03-08-2011, 05:15 AM -
adding flash file to a button!
By Y. Progammer in forum New To JavaReplies: 1Last Post: 03-03-2010, 03:18 PM -
Adding "Choose File" button to applet
By mju4t in forum Java AppletsReplies: 1Last Post: 09-17-2009, 08:11 AM -
Calculator Online(Without Button)
By lizard_kanta in forum Java AppletsReplies: 4Last Post: 04-29-2009, 07:12 PM -
Adding EXIT button on MIDlet form
By Java Tip in forum Java TipReplies: 0Last Post: 11-22-2007, 10:13 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks