Results 1 to 16 of 16
  1. #1
    ryamz is offline Member
    Join Date
    Jul 2010
    Posts
    16
    Rep Power
    0

    Post need some heLp T.T

    the build output is
    C:\Documents and Settings\STI\My Documents\SqrtException.java:22: sqrt(double) in java.lang.Math cannot be applied to ()
    Result = Math.sqrt();
    ^
    1 error

    Process completed.
    my code is this....
    import javax.swing.*;
    import java.lang.Math.*;

    public class SqrtException
    {
    public static void main(String[] args)throws ArithmeticException
    {

    int num;
    int Result;
    String inputString;

    inputString = JOptionPane.showInputDialog(null,
    "Enter a number to be square-rooted");
    num = Integer.parseInt(inputString);




    try
    {
    Result = Math.sqrt();

    }
    catch(ArithmeticException exception)
    {

    if (num<0);
    }
    JOptionPane.showMessageDialog(null,"cannot use negatives");
    System.exit(0);
    }
    }
    :(:(:confused::confused: im confused what should i put in import****

    helP me pLs...

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  3. #3
    ryamz is offline Member
    Join Date
    Jul 2010
    Posts
    16
    Rep Power
    0

    Default

    1. Write a Java application that throws and catches an Arithmetic Exception when you attempt to take the square roof of a negative value. Prompt the user for an input value and try the Math.sqrt() method on it. The application either displays the square root or catches the thrown Exception and displays an appropriate message. Name the program as SqrtException.java.


    this is the problem...

  4. #4
    ryamz is offline Member
    Join Date
    Jul 2010
    Posts
    16
    Rep Power
    0

    Default

    1. Write a Java application that throws and catches an Arithmetic Exception when you attempt to take the square roof of a negative value. Prompt the user for an input value and try the Math.sqrt() method on it. The application either displays the square root or catches the thrown Exception and displays an appropriate message. Name the program as SqrtException.java.


    this is the problem...

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Yes, but this is nothing to do with Java.

    attempt to take the square roof of a negative value
    Just forget about Java for a moment. Do you know how to do this in simple maths? We cannot find the square root of a negative number, that's a basis of maths actually.

  6. #6
    ryamz is offline Member
    Join Date
    Jul 2010
    Posts
    16
    Rep Power
    0

    Default

    no i dont even know it ... T.T

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Ok, you cannot find the square root of a negative number. Only the positive numbers. So check the number before find the square root of it.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    I've tried to re-write your code as much as in a simple way. Go through the following and let me know.

    Java Code:
    import javax.swing.*;
    import java.lang.Math.*;
    
    public class SqrtException {
    
        public static void main(String[] args) {
    
            double num = -1, Result;
            String inputString;
    
            try {
                while (num < 0) {
                    inputString = JOptionPane.showInputDialog(null,
                            "Enter a number to be square-rooted");
                    if (inputString != null) {
                        num = Double.parseDouble(inputString);
                        if (num < 0) {
                            JOptionPane.showMessageDialog(null, "cannot use negatives");
                        }
                    } else {
                        System.exit(0);
                    }
                }
                Result = Math.sqrt(num);
                JOptionPane.showMessageDialog(null, "Square root is: " + Result);
            } catch (ArithmeticException ex) {
                System.exit(0);
            }
        }
    }

  9. #9
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Quote Originally Posted by Eranga View Post
    Ok, you cannot find the square root of a negative number.
    LIEZ! The square root of a negative number is a combination of its absolute value and the hypothetical value i. e.g. the square root of 9 is 3i. Now, java doesn't deal with that, and most normal people never have to, but its there.

    @OP: The problem with your code is that you're not giving Math.sqrt(double) a number to deal with. I blame whoever wrote the assignment - Math.sqrt() doesn't exist, its either Math.sqrt(double) or just Math.sqrt. Anyway, you need to pass num to the sqrt method as the only parameter. You probably want to change the type of your variables (besides the input) to double, or you'll have to add a cast to int. Also, see what happens if the input is invalid (non-numeric value, window is closed instead of having the accept button clicked (probably says "Ok"), etc.) Then handle that exception/problem in the code. Never trust the user to enter valid data.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Singing Boyo View Post
    LIEZ! The square root of a negative number is a combination of its absolute value and the hypothetical value i. e.g. the square root of 9 is 3i. Now, java doesn't deal with that, and most normal people never have to, but its there.n/problem in the code. Never trust the user to enter valid data.
    Of course, complex numbers are there. But better to leave it aside at the moment. Since OP dosen't know that he cannot find the root of a negative number in very basis mathematical principles, I don't think it's a good idea to discuss about that here. So I don't want to mention about that at the moment. :)

  11. #11
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    I will admit to being slightly hyperactive while writing my last post. Not sure what I had/did, but it was effective :eek:
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Singing Boyo View Post
    I will admit to being slightly hyperactive while writing my last post. Not sure what I had/did, but it was effective :eek:
    Of course lol, your post is more effective. And our OP is seeker on new stuff, you post really helpful to learn a lot. But I just want to point him in a simple way, because he dosen't know how to find the square toot of -36. ;)

  13. #13
    richard's Avatar
    richard is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default

    Sir..?..what is the possible output of your re-writed program?....

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by richard View Post
    Sir..?..what is the possible output of your re-writed program?....
    Why don't you run the code with all the changes discuss here and see. ;)

  15. #15
    richard's Avatar
    richard is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default

    I've try that code sir but the problem is we will not be able to use the while loop or the loop sturcture..its because the problem did not tell us to use the loop structure. And Our Instructor said that we will not be able to use the loop structure because all you is need to take the square root of the negative value.

    thanks sir for your answer..

    i need the code sir that is not uses the loop..

    Can u gave me a code that is not uses a loop?..

    based on the problem sir.
    Last edited by richard; 01-07-2011 at 10:01 AM.
    Java Programming is Challenging:)

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •