-
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
-
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
-
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.
-
Re: Problem with a tutorial program(Adding the answer of two squared numbers together
Quote:
Originally Posted by
Melawe
Ummmm OK I'm fine for books thanks :P. Did my last post help you in any way or confuse you more?
-
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
-
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).
-
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);
-
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.
-
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)
-
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. :/
-
Re: Problem with a tutorial program(Adding the answer of two squared numbers together
anyone got a clue whats wrong with it?
-
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
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:
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
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.
-
Re: Problem with a tutorial program(Adding the answer of two squared numbers together
Quote:
Originally Posted by
Faz
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
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);
-
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.
-
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. :/
-
Re: Problem with a tutorial program(Adding the answer of two squared numbers together
-
Re: Problem with a tutorial program(Adding the answer of two squared numbers together
-
Re: Problem with a tutorial program(Adding the answer of two squared numbers together
It worked!!! :D Thanks everyone!!!! :D
-
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
-
Re: Problem with a tutorial program(Adding the answer of two squared numbers together
-
Re: Problem with a tutorial program(Adding the answer of two squared numbers together
Thread Tools -> Mark this thread as solved