Java Calculator Square Root Function
Hi all new to java made a simple calculator for my uni project i have now been asked to add a square root function i have tried but the function doesn’t work and now neither does the calculator please help
Copy of my code:
Code Java:
package calculator;
//Calculator User Interface
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.lang.Math;
public class calculatorinterface implements ActionListener {
JFrame frame = new JFrame("Tom's Calculator");
JPanel panel = new JPanel(new GridLayout(4,3));
JPanel panel2 = new JPanel(new GridLayout(4,2));
JPanel background = new JPanel(new BorderLayout());
JTextArea text = new JTextArea(1,20);
JButton but1 = new JButton("1");
JButton but2 = new JButton("2");
JButton but3 = new JButton("3");
JButton but4 = new JButton("4");
JButton but5 = new JButton("5");
JButton but6 = new JButton("6");
JButton but7 = new JButton("7");
JButton but8 = new JButton("8");
JButton but9 = new JButton("9");
JButton but0 = new JButton("0");
JButton butadd = new JButton("+");
JButton butsub = new JButton("-");
JButton butmulti = new JButton("*");
JButton butdiv = new JButton("/");
JButton buteq = new JButton("=");
JButton butclear = new JButton("C");
// scientific calculator
JButton butsqrt = new JButton("sqrt");
Double number1,number2,result;
int addc=0,subc=0,multic=0,divc=0,sqrt=0;
public void ui()
{
frame.setVisible(true);
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.add(background);
background.add("Center",panel);
background.add("East",panel2);
background.add("North",text);
panel.add(but1);
panel.add(but2);
panel.add(but3);
panel.add(but4);
panel.add(but5);
panel.add(but6);
panel.add(but7);
panel.add(but8);
panel.add(but9);
panel.add(but0);
panel.add(buteq);
panel.add(butclear);
panel2.add(butadd);
panel2.add(butsub);
panel2.add(butmulti);
panel2.add(butdiv);
panel2.add(butsqrt);
but1.addActionListener(this);
but2.addActionListener(this);
but3.addActionListener(this);
but4.addActionListener(this);
but5.addActionListener(this);
but6.addActionListener(this);
but7.addActionListener(this);
but8.addActionListener(this);
but9.addActionListener(this);
but0.addActionListener(this);
butadd.addActionListener(this);
butsub.addActionListener(this);
butmulti.addActionListener(this);
butdiv.addActionListener(this);
buteq.addActionListener(this);
butclear.addActionListener(this);
butsqrt.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if(source==butclear)
{
number1=0.0;
number2=0.0;
text.setText("");
}
if(source==but1)
{
text.append("1");
}
if(source==but2)
{
text.append("2");
}
if(source==but3)
{
text.append("3");
}
if(source==but4)
{
text.append("4");
}
if(source==but5)
{
text.append("5");
}
if(source==but6)
{
text.append("6");
}
if(source==but7)
{
text.append("7");
}
if(source==but8)
{
text.append("8");
}
if(source==but9)
{
text.append("9");
}
if(source==but0)
{
text.append("0");
}
if(source==butadd)
{
number1=number_reader();
text.setText("");
addc=1;
subc=0;
multic=0;
divc=0;
sqrt=0;
}
if(source==butsub)
{
number1=number_reader();
text.setText("");
addc=0;
subc=1;
multic=0;
divc=0;
sqrt=0;
}
if(source==butmulti)
{
number1=number_reader();
text.setText("");
addc=0;
subc=0;
multic=1;
divc=0;
sqrt=0;
}
if(source==butdiv)
{
number1=number_reader();
text.setText("");
addc=0;
subc=0;
multic=0;
divc=1;
sqrt=0;
if (source==butsqrt)
{
number1=number_reader();
addc=0;
subc=0;
multic=0;
divc=0;
sqrt=1;
}
if(source==buteq)
{
number2=number_reader();
if(addc>0)
{
result=number1+number2;
text.setText(Double.toString(result));
}
if(subc>0)
{
result=number1-number2;
text.setText(Double.toString(result));
}
if(multic>0)
{
result=number1*number2;
text.setText(Double.toString(result));
}
if(divc>0)
{
result=number1/number2;
text.setText(Double.toString(result));
if (sqrt>0)
result=Math.sqrt(number1);
text.setText(Double.toString(result));
}
}
}
}
public double number_reader()
{
Double num1;
String s;
s=text.getText();
num1=Double.valueOf(s);
return num1;
}
}
Re: Java Calculator Square Root Function
Sorry, but saying something "doesn't work" is as useful a question as "then fix it" is a useful answer. What doesn't work about it? Do you see an Exception? Strange behavior? What did you expect to happen? What happens instead?
Also, when posting code, please use the code or highlight tags. Nobody wants to read through unformatted code.
Re: Java Calculator Square Root Function
It turns out I was wasting my time, as the OP is already receiving help elsewhere:
This thread has been cross posted here:
http://www.java-forums.org/advanced-java/38311-java-calculator-square-root-pleas-help.html
Although cross posting is allowed,
for everyone's benefit, please read:
Java Programming Forums Cross Posting Rules
The Problems With Cross Posting
Re: Java Calculator Square Root Function
im sorry i thought these were differnt forums and whats an OP? and i signed up 2 both as the teahcer recomeneded them for if we got stuck all i wnted was to get my calculator working as before trying to add the square root function it worked fine and now it dosent atall sorry for any troubles caused
Re: Java Calculator Square Root Function
Quote:
Originally Posted by
laser1992
im sorry i thought these were differnt forums and whats an OP? and i signed up 2 both as the teahcer recomeneded them for if we got stuck all i wnted was to get my calculator working as before trying to add the square root function it worked fine and now it dosent atall sorry for any troubles caused
Don't worry you are obviousally new to this..
Cross posting can be an issue for a number of reasons. Mainly because if you don't tell us you have posted somewhere else, we will spend our valuable time attempting to help you, only to find out your question has been solved elsewhere.
Please read: http://www.javaprogrammingforums.com...s-posting.html
When posting on the forums, it is always best to give as much information about the problem as possible.
Just like Kevin said, What doesn't work about it? Do you see an Exception? Strange behavior? What did you expect to happen? What happens instead?
Ultimately this will improve your chances of a quality response.
We are always here to help :)
Oh and OP means original poster.
Re: Java Calculator Square Root Function
ah i see your point and well first year of university were learning basic java we were asked to make a calculator so rather than just download the source code to try and gain a better understanding i watched a YouTube tutorial and made a working calculator. then we were asked to add square root button i did some research to try and find out what needed 2 be added the above code is what i came up with however when ran no errors show when compiling but now when run none of the functions such as + or – only typing in numbers works. All i wnated 2 do was add a square root button to my already working calculator i added a new button and tired 2 follow how the other buttons worked to make the square root one work however it didnt