Welcome to Dream.In.Code
Become a Java Expert!

Join 149,494 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,363 people online right now. Registration is fast and FREE... Join Now!




Maths - Square Root

 
Reply to this topicStart new topic

Maths - Square Root

lost69
20 Mar, 2007 - 07:41 AM
Post #1

New D.I.C Head
*

Joined: 20 Mar, 2007
Posts: 5


My Contributions
Hi, I'm trying to calculate square root but i cannot seem to get it working. here is the code:

CODE
import javax.swing.*;
import java.awt.event.*;
import java.math.*;


public class Calc implements ActionListener{

  private JButton btnAdd, btnMinus, btnDivide, btnMulti, btnPercent, btnSqRoot, btnClear, btnHelp;
  private JLabel lblNumber1, lblNumber2,lblResult, lblPercentage, lblSqRoot, lblClear, lblHelp;
  private JTextField txtNumber1, txtNumber2, txtResult;
  private JPanel panel;
  private JFrame frame;
  double Result1;

  public Calc(){
   frame = new JFrame();
   frame.setTitle("Calculator");
   frame.setSize(500,400);
   frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);

   panel = new JPanel();
   panel.setLayout(null);

   lblNumber1 = new JLabel("First Number");
   lblNumber1.setBounds(10, 50, 100, 20);
   panel.add(lblNumber1);

   txtNumber1 = new JTextField("");
   txtNumber1.setBounds(110, 50, 150, 20);
   panel.add(txtNumber1);

   lblNumber2 = new JLabel("Second Number");
   lblNumber2.setBounds(10, 70, 100, 20);
   panel.add(lblNumber2);

   txtNumber2= new JTextField("");
   txtNumber2.setBounds(110, 70, 150, 20);
   panel.add(txtNumber2);

   lblResult = new JLabel("Result");
   lblResult.setBounds(10, 90, 100, 20);
   panel.add(lblResult);

   txtResult= new JTextField("");
   txtResult.setBounds(110, 90, 150, 20);
   panel.add(txtResult);




   btnAdd = new JButton("Add");
   btnAdd.setBounds(50, 200, 100, 20);
   btnAdd.addActionListener(this);
   panel.add(btnAdd);


   btnMinus = new JButton("Subtract");
   btnMinus.setBounds(150, 200, 100, 20);
   btnMinus.addActionListener(this);
   panel.add(btnMinus);


   btnDivide = new JButton("Divide");
   btnDivide.setBounds(250, 200, 100, 20);
   btnDivide.addActionListener(this);
   panel.add(btnDivide);

   btnMulti = new JButton("Multiply");
   btnMulti.setBounds(350, 200, 100, 20);
   btnMulti.addActionListener(this);
   panel.add(btnMulti);

   btnPercent = new JButton("Percentage");
   btnPercent.setBounds(50, 222, 100, 20);
   btnPercent.addActionListener(this);
   panel.add(btnPercent);

   btnSqRoot = new JButton("Sq Root");
   btnSqRoot.setBounds(150, 222, 100, 20);
   btnSqRoot.addActionListener(this);
   panel.add(btnSqRoot);

   btnClear = new JButton("Clear");
   btnClear.setBounds(250, 222, 100, 20);
   btnClear.addActionListener(this);
   panel.add(btnClear);

   btnHelp = new JButton("Help");
   btnHelp.setBounds(350, 222, 100, 20);
   btnHelp.addActionListener(this);
   panel.add(btnHelp);

   frame.getContentPane().add(panel);
   frame.setVisible(true);
  }

  public void actionPerformed(ActionEvent event){
   Object source = event.getSource();
   int s1, s2, s3;
   s1 = Integer.parseInt(txtNumber1.getText());
   s2 = Integer.parseInt(txtNumber2.getText());
   //s3 = Integer.parseInt(txtResult.getText());


   if (source == btnAdd)
   {
           txtResult.setText(String.valueOf(s1+s2));
   }

    if (source == btnMinus)
    {
        txtResult.setText(String.valueOf(s1-s2));
    }

    if (source == btnDivide)
    {
        txtResult.setText(String.valueOf(s1/s2));
    }

    if (source == btnMulti)
    {
        txtResult.setText(String.valueOf(s1*s2));
    }

    if (source == btnPercent)
    {
        txtResult.setText(String.valueOf(s2/s1*100));
    }

    if (source == btnSqRoot)
    {
        txtResult = Math.sqrt(String.valueOf(s1));
    }


  }

  public static void main(String[] args){
   Calc myCalc= new Calc();
  }
}



And here is the error message:


QUOTE

C:\Users\Alex\Documents\AO BBIT\Programming 1\Assignment 5\Calc.java:132: sqrt(double) in java.lang.Math cannot be applied to (java.lang.String)
txtResult = Math.sqrt(String.valueOf(s1));
^
1 error

Tool completed with exit code 1


thanks for any help
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Maths - Square Root
20 Mar, 2007 - 08:58 AM
Post #2

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
Math.sqrt(double) needs a double value, but you are passing it a String (s1). You have to parse it first, for example with:
Double.parseDouble(String)
so you get a double out of it.
User is offlineProfile CardPM
+Quote Post

lost69
RE: Maths - Square Root
20 Mar, 2007 - 09:15 AM
Post #3

New D.I.C Head
*

Joined: 20 Mar, 2007
Posts: 5


My Contributions
QUOTE(1lacca @ 20 Mar, 2007 - 09:58 AM) *

Math.sqrt(double) needs a double value, but you are passing it a String (s1). You have to parse it first, for example with:
Double.parseDouble(String)
so you get a double out of it.



OK, so where do i put this parse part, i tried the line above where im doing the calculation, but that does not seem to work.
User is offlineProfile CardPM
+Quote Post

horace
RE: Maths - Square Root
20 Mar, 2007 - 09:25 AM
Post #4

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 5 times
Dream Kudos: 50
My Contributions
in
CODE

        txtResult = Math.sqrt(String.valueOf(s1));

s1 is an int and txtResult is a TextField, so try
CODE

       txtResult.setText(new String(" " + Math.sqrt(s1)));

User is offlineProfile CardPM
+Quote Post

1lacca
RE: Maths - Square Root
21 Mar, 2007 - 12:53 AM
Post #5

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
Ooops, sorry, horace is right, I've just looked at the error, and was writing gibberish ohmy.gif

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 05:56PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month