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

• March 29th, 2010, 01:56 AM
Melawe
Problem with a tutorial program(Adding the answer of two squared numbers together)
Hi every one! :D

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 :

Code :

```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
Code :

```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. :P

Thanks all!

-Melawe
• March 29th, 2010, 02:45 AM
Faz
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

Code :

```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
• March 29th, 2010, 04:24 AM
Melawe
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.
• March 29th, 2010, 10:19 AM
Faz
Re: Problem with a tutorial program(Adding the answer of two squared numbers together
Quote:

Originally Posted by Melawe
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 :P. Did my last post help you in any way or confuse you more?
• March 29th, 2010, 04:16 PM
Melawe
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
• March 30th, 2010, 12:20 AM
Melawe
Re: Problem with a tutorial program(Adding the answer of two squared numbers together
I tried this
Code :

```System.out.println("The square root of 300 + 146 is "); double z = x + y; System.out.print(z);```
and this
Code :

```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).
• March 30th, 2010, 01:58 AM
Sai
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

Code :

```System.out.println("The square root of 300 + 146 is "); double z =Math.sqrt( x + y); System.out.print(z);```
• March 30th, 2010, 02:17 AM
Melawe
Re: Problem with a tutorial program(Adding the answer of two squared numbers together
Sorry there was a mistake in the code
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.
• March 30th, 2010, 02:38 AM
Melawe
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)
• March 30th, 2010, 02:55 AM
Melawe
Re: Problem with a tutorial program(Adding the answer of two squared numbers together
It did it! :D

Code :

```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. :/
• March 30th, 2010, 03:05 AM
Melawe
Re: Problem with a tutorial program(Adding the answer of two squared numbers together
anyone got a clue whats wrong with it?
• March 30th, 2010, 06:16 AM
Faz
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

Code :

`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
Code :

`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
Code :

`System.out.print(Math.sqrt(x+y))`

EDIT:
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
Code :

`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.
• March 30th, 2010, 06:35 AM
Melawe
Re: Problem with a tutorial program(Adding the answer of two squared numbers together
Quote:

Originally Posted by Faz
EDIT:
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
Code :

`Math.sqrt(x)`
Is changing x but it is not.

Yea thats true, so the code should look like this at the end?
Code :

```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);```
• March 30th, 2010, 06:44 AM
Faz
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 :P.
• April 1st, 2010, 12:09 AM
Melawe
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. :/
• April 1st, 2010, 12:44 AM
Sai
Re: Problem with a tutorial program(Adding the answer of two squared numbers together
I recommend eclipse...
• April 1st, 2010, 03:04 AM
Melawe
Re: Problem with a tutorial program(Adding the answer of two squared numbers together
Okay thanks!
• April 6th, 2010, 05:17 PM
Melawe
Re: Problem with a tutorial program(Adding the answer of two squared numbers together
It worked!!! :D Thanks everyone!!!! :D
• April 7th, 2010, 02:46 AM
Freaky Chris
Re: Problem with a tutorial program(Adding the answer of two squared numbers together