Results 1 to 7 of 7
- 02-03-2011, 09:57 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
Java Calculator Square Root Pleas Help
Java Calculator Help Would Be Greatly Appreciated
Hi all new to java made a simple calculator for my uni project have now been asked to add a square root function i have tried but the function doesnt work and now neither does the calculator please help sorry if this is in the wrong section
Copy of my code:
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;
}
}
- 02-03-2011, 11:10 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
In your code you have to press the '=' buttton after you've pressed the 'sqrt' button, and even then: check your code in the '/' button handling: the nesting of your tests is incorrect. Your state machine is totally incorrect, sorry for saying. e.g. press '1', '+', '='. Do you like that behaviour?
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-03-2011, 11:44 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
thanks for the reply so if i moved it before = would it then work? and i followed a youtube video to make the calculator as im completely new to java so thats why the coding is like tht.
- 02-03-2011, 12:56 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
No need to apologize for anything; this is a nice project for learning and testing your Java skills. Think about the functionality: pressing the 'sqrt' button is like pressing the '+' button but it should calculate the square root of number1 and display the result. The '=' key has nothing to do with it.
Why not give it a try and see what happens?
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-03-2011, 01:12 PM #5
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
thanks i understand in principle wht ur saying about the functionality of the button how when clciked it should work like the plus button however im really not sure wht i need 2 change within my coding please could you tell me were ive gone wrong? ive tried moving a few things with no succes :( kind regads tom
- 02-03-2011, 01:14 PM #6
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 02-03-2011, 03:28 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
Think of it: when you press the '=' button your calculator should do something; the same with the 'sqrt' button: you calculator should calculate and display the square root of number1. You should remove the code for the square root from the body of the if equal button and move it to somewhere else.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Help with Square root function and Overload(calc error)
By racingoutbac in forum New To JavaReplies: 3Last Post: 11-04-2010, 12:09 AM -
Find the square root with a particular method
By roud9 in forum New To JavaReplies: 2Last Post: 09-27-2010, 11:39 PM -
square root and prime numbers
By roud9 in forum New To JavaReplies: 16Last Post: 09-22-2010, 03:20 PM -
Simple square root problem!
By nortski in forum New To JavaReplies: 7Last Post: 04-01-2009, 02:11 PM -
Creating a New Method for Square Root Loop
By SapphireSpark in forum New To JavaReplies: 14Last Post: 02-25-2009, 01:21 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks