Results 1 to 8 of 8
  1. #1
    nortski is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default Simple square root problem!

    Hi everyone,

    This is my first time posting. I'm brand new to programming but have taken it upon myself to study a Java programming course. However; I'm stuck on a simple little problem, I'm quite ashamed about this :o

    I have 2 classes: MyMath and Squares

    I have to return the square root of a value passed into the MyMath class from the Squares class and print it to the screen.

    The object of this excercise is to get used to static methods, to be able to use methods within a class without the need to instantiate an object.

    But as you can guess by now, it ain't working.

    Here is the code:

    public class MyMath
    {

    public static long square(int a)
    {
    long b = a*a;
    return b;
    }
    }

    and:

    public class Squares
    {
    public static void main(String [] args)
    {
    MyMath.square(10);
    System.out.println(MyMath.square());
    }
    }

    MyMath class compiles but Squares does not. Can someone point me in the right direction please? No doubt it will be a really simple answer.

    Kind regards,
    nortski.

  2. #2
    trax is offline Member
    Join Date
    Mar 2009
    Posts
    42
    Rep Power
    0

    Default

    Check the file name it should be squares because you are including main in that , n tell me these 2 classes are in different file or in same file , Its better You tell me the error you encounter . one suggestion use eclipse ide.

  3. #3
    trax is offline Member
    Join Date
    Mar 2009
    Posts
    42
    Rep Power
    0

    Default

    one more thing dont feel ashamed of asking small things coz no1 is born genious.

  4. #4
    nortski is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default

    Hi trax;

    The classes are in different files and the file names appear to be correct.

    The error I get is:

    Squares.java:6: square(int) in MyMath cannot be applied to ()
    System.out.println(MyMath.square());
    ______________________^

    Any clearer?

    Thx again.

  5. #5
    kbindumadhavi is offline Member
    Join Date
    Apr 2009
    Posts
    20
    Rep Power
    0

    Default

    MyMath.square(10);
    Here MyMath.square() returns long value. Also it takes int value as argument.
    So you cannot use that in System.out.
    That is the reason below is not applicable
    System.out.println(MyMath.square());

    Try this
    public class Squares
    {
    public static void main(String [] args)
    {
    -->System.out.println(MyMath.square(10));
    }
    }
    The statement with arrow can be replaced with the following:
    long l = MyMath.square(10);
    System.out.println(l);

  6. #6
    trax is offline Member
    Join Date
    Mar 2009
    Posts
    42
    Rep Power
    0

    Default

    use it :

    System.out.println(MyMath.square(10));

  7. #7
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    Java Code:
    MyMath.square(10);
    The square method in class myMath returns an int, but you are not receiving that result into any variable in the call in to the square method. You need a variable to receive what the method returns. Also,
    Java Code:
    System.out.println(MyMath.square());
    the above is not correct because you are calling the square method without any parameters (the call to the method is empty).

    I would suggest some like (example):

    Java Code:
    int myVar:
    myVar = MyClass.MyMethod (12);
    System.out.println(myVar);
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  8. #8
    nortski is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default

    Cheers guys, worked a treat. It actually does make complete sense it's just that I follow my excerise book to literally, this isn't the first syntax error I've come across and still only on the first module :mad:

    Who knows what else I'll be up against!

    Thanks again guys!

Similar Threads

  1. Creating a New Method for Square Root Loop
    By SapphireSpark in forum New To Java
    Replies: 14
    Last Post: 02-25-2009, 01:21 PM
  2. 8-Square puzzle loop
    By SapphireSpark in forum New To Java
    Replies: 7
    Last Post: 12-04-2008, 07:21 PM
  3. Simple IO problem
    By aamp in forum New To Java
    Replies: 2
    Last Post: 12-01-2008, 02:27 PM
  4. SWT OpenGL snippet: draw a square
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 06-28-2008, 09:29 PM
  5. Problem using buttons to creat a magic square game
    By goldman in forum New To Java
    Replies: 5
    Last Post: 05-05-2008, 04:04 AM

Posting Permissions

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