Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 21 of 21

Thread: Problem with a tutorial program(Adding the answer of two squared numbers together)

  1. #1
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Question Problem with a tutorial program(Adding the answer of two squared numbers together)

    Hi every one!

    I just got a book about a week ago on Java, and its going great! I just can't find how to add an x integer squared and a y integer squared. Here is the code i currently have :

    class XYZ {
        public static void main(String[] args){
            int x = 300;
            System.out.println("The squate root of "
                    + x
                    + " is "
                    +Math.sqrt(x)
                    );
            int y = 146;
            System.out.println("The square root of "
                    + y
                    + " is "
                    +Math.sqrt(y)
                    );
        }
    }

    What I can't seem to get right is adding the result of x and y squared, I am using a book ( Sam's Java in 24 hours, I am a Self-learning teen) to learn Java, this will be the first program I will wright on my own so please don't give the answer just an example I can use to know what is wrong or how I cloud do it (the harder the better I really want to try this with no answers on the next page).
    I tried this
    int z = 0;
            System.out.println("The square root of 300 + 146 is ");
            double x = y ;
            System.out.print(z);
    I keep getting errors and the Net Beans IDE flags with this message
    " x is already defined in main(java.lang.String[])"
    I really don't have a clue what that means, please tell me anyone who knows what those message mean.


    Thanks all!


    -Melawe


  2. #2
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: Problem with a tutorial program(Adding the answer of two squared numbers together

    OK lets just take a look at your current code first

    int z = 0; //Here you are delaring z as 0
            System.out.println("The square root of 300 + 146 is ");//Here you are just printing out this now I doubt you want to have the numbers unchangable so you might want to do a something similar to what you did before with x and y.
            double x = y ;//This is the line that is causing you trouble and the reason is you already have a variable called x so you can't declare it again. I'm not too sure what you are trying to do here to be honest do you really want to set x = y?
            System.out.print(z);//Here you are printing out z and as you haven't changed it this will be 0

    So now we need to decide what you want to do, you want to get the square root of z right? And z is x + y. So it isn't any different to what you've already done except for getting the value which is not just assigning it a number manually. I'm not sure how to help too much without just giving the answer. So maybe just in pseudocode.

    Declare z
    Assign z a value (x+y)
    Print out preciding text
    Print out square root of z

  3. The Following User Says Thank You to Faz For This Useful Post:

    JavaPF (March 29th, 2010)

  4. #3
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Problem with a tutorial program(Adding the answer of two squared numbers together

    Here is an online text book version InformIT: Sams Teach Yourself Java 2 in 24 Hours > Activities
    It is the 4th edition thou i am using the 6th.
    Last edited by Melawe; March 30th, 2010 at 12:14 AM.

  5. #4
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: Problem with a tutorial program(Adding the answer of two squared numbers together

    Quote Originally Posted by Melawe View Post
    Here is an online text book InformIT: Sams Teach Yourself Java 2 in 24 Hours > Activities
    It is the 4th edition thou i am using the 6th.
    Ummmm OK I'm fine for books thanks . Did my last post help you in any way or confuse you more?

  6. #5
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Problem with a tutorial program(Adding the answer of two squared numbers together

    I think so I got what you mean I will try it asap.

    Thanks

    -Melawe

  7. #6
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Problem with a tutorial program(Adding the answer of two squared numbers together

    I tried this
    System.out.println("The square root of 300 + 146 is ");
                    double z = x + y;
            System.out.print(z);
    and this
    System.out.println("The square root of 300 + 146 is "
                   + double z = x + y;
            );
            System.out.print(z);
    still didn't work (the e-book link that I posted is what I am trying too do).

  8. #7
    Junior Member
    Join Date
    Mar 2010
    Posts
    19
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Problem with a tutorial program(Adding the answer of two squared numbers together

    In both of the codes u didnt use the

    Math.sqrt(int) function

    this will do the job

    System.out.println("The square root of 300 + 146 is ");
                    double z =Math.sqrt( x + y);
            System.out.print(z);

  9. #8
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Problem with a tutorial program(Adding the answer of two squared numbers together

    Sorry there was a mistake in the code
    System.out.println("The square root of 300 plus the square root of 146 is ");
                    double z = x + y;
            System.out.print(z);
    I am trying to add the square root of 300 and of 146, but it wont show when I compile it.
    Last edited by Melawe; March 30th, 2010 at 02:20 AM.

  10. #9
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Problem with a tutorial program(Adding the answer of two squared numbers together

    Here is the what I get when Netbeans compiles it:

    run:
    The squate root of 300 is 17.320508075688775
    The square root of 146 is 12.083045973594572
    The square root of 300 + 146 is
    0BUILD SUCCESSFUL (total time: 1 second)

  11. #10
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Problem with a tutorial program(Adding the answer of two squared numbers together

    It did it!

    System.out.println("The square root of 300 plus the square root of 146 is ");
                    double z =Math.sqrt(x + y);
            System.out.print(z);
    run:
    The squate root of 300 is 17.320508075688775
    The square root of 146 is 12.083045973594572
    The square root of 300 plus the square root of 146 is
    21.118712081942874BUILD SUCCESSFUL (total time: 0 seconds)

    The wrong thing in the result is that 300 and 146 squared should only be added not added then squared. :/

  12. #11
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Problem with a tutorial program(Adding the answer of two squared numbers together

    anyone got a clue whats wrong with it?

  13. #12
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: Problem with a tutorial program(Adding the answer of two squared numbers together

    OK I think you have a bit of a misunderstanding as to what exactly is happening when you write

    System.out.print(Math.sqrt(x))

    That doesn't change x from 300 to 17.32.... it simply prints 17.32.... right?

    So when you come to this stage
    z = Math.sqrt(x+y)

    It is worked out like this
    z = Math.sqrt(300+146)
    z = Math.sqrt(446)
    z = 21.11....


    Now I'm going to say that logically z should be x+y not the square root of x+y. This would be useful if you ever wanted to reuse this class to perform different manipulation other then square root. So to get that result you would want to either assign z as x + y or just right
    System.out.print(Math.sqrt(x+y))

    EDIT:
    Ah just re-read your last post I think I see what your trying to do now.
    You want to add the squareroot of x and the square root of y not get the sqareroot of x + y.

    So once again I think your assuming
    Math.sqrt(x)
    Is changing x but it is not.

    Just think about what you are trying to do this right:

    x^1/2 + y^1/2 (that is the same as a squareroot if you didn't know)

    So why not just try putting that into java and see what it comes out with?

    But just be aware that the square root of 300 + 146(which is what you have written in your code) is 21.11...
    What you are looking for is the squre root of 300 + the squareroot of 146 you should change your code to reflect this.
    Last edited by Faz; March 30th, 2010 at 06:30 AM.

  14. The Following User Says Thank You to Faz For This Useful Post:

    Melawe (March 30th, 2010)

  15. #13
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Smile Re: Problem with a tutorial program(Adding the answer of two squared numbers together

    Quote Originally Posted by Faz View Post
    EDIT:
    Ah just re-read your last post I think I see what your trying to do now.
    You want to add the squareroot of x and the square root of y not get the sqareroot of x + y.

    So once again I think your assuming
    Math.sqrt(x)
    Is changing x but it is not.
    Yea thats true, so the code should look like this at the end?
    System.out.println("The square root of 300 plus the square root of 146 is ");
                    double z =(x^1/2 + y^1/2 );
            System.out.print(z);

  16. #14
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: Problem with a tutorial program(Adding the answer of two squared numbers together

    Yes, basically. Just make sure to use Math.sqrt not ^1/2 .

  17. #15
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Problem with a tutorial program(Adding the answer of two squared numbers together

    Okay thanks, um what IDE would you recommend? I was using Netbeans but the last few updates came with a few Trojans and other Spyware. :/

  18. #16
    Junior Member
    Join Date
    Mar 2010
    Posts
    19
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Problem with a tutorial program(Adding the answer of two squared numbers together

    I recommend eclipse...

  19. #17
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Problem with a tutorial program(Adding the answer of two squared numbers together

    Okay thanks!

  20. #18
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Problem with a tutorial program(Adding the answer of two squared numbers together

    It worked!!! Thanks everyone!!!!

  21. #19
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Problem with a tutorial program(Adding the answer of two squared numbers together

    I'm glad you are all working. would you please mark this thread as solved.


    Regards,
    Chris

  22. #20
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Problem with a tutorial program(Adding the answer of two squared numbers together

    How do I do that?

  23. #21
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Problem with a tutorial program(Adding the answer of two squared numbers together

    Thread Tools -> Mark this thread as solved

Similar Threads

  1. Adding to Array from JavaSpace problem
    By rtumatt in forum Collections and Generics
    Replies: 2
    Last Post: September 15th, 2011, 07:11 AM
  2. Arrays.equals not returning correct answer.
    By Anyone in forum Collections and Generics
    Replies: 2
    Last Post: February 11th, 2010, 10:12 AM
  3. Adding to Array from JavaSpace problem
    By rtumatt in forum Collections and Generics
    Replies: 3
    Last Post: January 5th, 2010, 07:19 PM
  4. Tutorial requests
    By Json in forum The Cafe
    Replies: 9
    Last Post: August 1st, 2009, 03:14 PM
  5. Store data in database using JSP
    By abhi4jdk in forum Java Servlet
    Replies: 1
    Last Post: May 18th, 2009, 04:42 AM