Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By therealvasile

Thread: New to Java need help. Have partial code written!!

  1. #1
    therealvasile is offline Member
    Join Date
    Jul 2013
    Posts
    9
    Rep Power
    0

    Default New to Java: sq root problem

    Okay so I'm new to java (powershell guy) and im working on simple examples trying to teach myself.

    so heres my problem:

    The goal of this exercise is to learn how to use methods in classes and how to test these methods by calling them from the main method. Write a program called SqRoot.java with the following methods:
    (a) A method called sqRoot which accepts an integer number as a parameter and returns the square root of the number as a double
    (b) A method called printSqRoot which accepts an integer number as a parameter but returns nothing. This method just prints the number and its square root.
    (c) A main method which first declares an integer and initializes it. It then creates an object of class SqRoot and then calls the above two methods to test them.

    And heres what ive done so far

    Java Code:
    package sqroot;
    
    public class SqRoot 
    {
        
        public void SqRoot(int number_1)
        {
          // Square roots supplied integer and stores as variable
          double sRoot_1 = Math.sqrt(number_1);
          // prints variable
          System.out.println(sRoot_1);
        }
        
        public void printSqRoot(int number_2)
        {
            // Square roots integer and stores as variable
            double sRoot_2 = Math.sqrt(number_2);
            // prints original number
            System.out.println(number_2);
            // prints sqRoot
            System.out.println(sRoot_2);
        }
        
        public static void main(String[] args) 
        {
            int sqInt = 9;
            
            
        }
    
    }
    need help completing
    Last edited by therealvasile; 07-11-2013 at 08:36 PM.

  2. #2
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    263
    Rep Power
    2

    Default Re: New to Java need help. Have partial code written!!

    use [code] [/ code] tags so that people will bother reading your code.

    in your method SqRoot must return a value, you are not doing that. So not void, double obviously and use the return keyword.

    you must construct the object and call the methods if you don't know how to do that search the java tutorials: http://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html
    Last edited by superhaNds; 07-11-2013 at 07:11 PM. Reason: tutorial link

  3. #3
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    79
    Rep Power
    0

    Default Re: New to Java need help. Have partial code written!!

    There is actually two ways to fix the above code. You can create a SqRoot object in the main class and then call the two methods with passing the value. The object methods will then print the orginal value and the calculations. The other would be to change your methods to return doubles and print them in the main class instead.

    first choice main:
    Java Code:
    SqRoot sq= new SqRoot();
    sq.SqRoot(6);
    second choice in main:
    Java Code:
    SqRoot sq= new SqRoot();
    System.out.println(sq.SqRoot(6));
    You have to create the object to use the methods of the object.
    Last edited by jbarke12; 07-11-2013 at 07:22 PM.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: New to Java need help. Have partial code written!!

    Quote Originally Posted by therealvasile View Post
    New to Java need help. Have partial code written!!
    Please go through the Forum Rules, particularly the third paragraph. Also go through the Guide For New Members and BB Code List - Java Programming Forum - Learn Java Programming and edit your post accordingly. To edit the subject line, click 'Edit Post' and then 'Go Advanced'

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    therealvasile is offline Member
    Join Date
    Jul 2013
    Posts
    9
    Rep Power
    0

    Default Re: New to Java need help. Have partial code written!!

    Quote Originally Posted by jbarke12 View Post
    There is actually two ways to fix the above code. You can create a SqRoot object in the main class and then call the two methods with passing the value. The object methods will then print the orginal value and the calculations. The other would be to change your methods to return doubles and print them in the main class instead.

    first choice main:
    Java Code:
    SqRoot sq= new SqRoot();
    sq.SqRoot(6);
    second choice in main:
    Java Code:
    SqRoot sq= new SqRoot();
    System.out.println(sq.SqRoot(6));
    You have to create the object to use the methods of the object.
    Awesome thanks!

    And I also fixed the subject line. Sorry guys!
    DarrylBurke likes this.

Similar Threads

  1. Partial GET method in Java Logical Error
    By onursahindur in forum Networking
    Replies: 5
    Last Post: 03-14-2013, 10:46 AM
  2. Why this java code has written this way?
    By talenone in forum New To Java
    Replies: 7
    Last Post: 01-10-2013, 11:09 AM
  3. Replies: 3
    Last Post: 04-15-2011, 04:50 AM
  4. Replies: 1
    Last Post: 03-29-2011, 08:59 AM
  5. I need some java / html code written
    By DeDanan in forum New To Java
    Replies: 2
    Last Post: 03-01-2011, 09:36 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
  •