Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Square Root

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 10-13-2008, 07:40 PM PM User | #1 webguy08 Regular Coder   Join Date: Mar 2008 Posts: 136 Thanks: 39 Thanked 1 Time in 1 Post Square Root Hi, I'm a beginner to Java coding and I am trying to come to understanding the code itself. I am attempting to create a small piece of software (BlueJ) which undertakes Pythagoras' theorem; working out x, y and z. So I have this line of code: Code: ``` public int getx() { x = Math.sqrt((y*y)-(z*z)); }``` What I am trying to achieve from this line of code is x=Square root(y^2-z^2). However, when I try to compile it directs me to the line: x = Math.sqrt((y*y)-(z*z)); Please help
 10-13-2008, 09:21 PM PM User | #2 tomws Senior Coder     Join Date: Nov 2007 Location: Arkansas Posts: 2,644 Thanks: 29 Thanked 330 Times in 326 Posts Perhaps you need to declare your variable before using it. Compiler errors generally provide more than a line number when there's a problem. They can be confusing, but are still often helpful for determining the problem.
 10-14-2008, 04:38 PM PM User | #3 webguy08 Regular Coder   Join Date: Mar 2008 Posts: 136 Thanks: 39 Thanked 1 Time in 1 Post This is the whole code: Code: ```public class Pythagoras { private int x; private int y; private int z; public Pythagoras(int xlength, int ylength, int zlength) { x = xlength; y = ylength; z = zlength; } public int getx() { x = Math.sqrt((y*y)-(z*z)); System.out.println("x = " + x); } public int gety() { y = Math.sqrt((z*z)-(x*x)); System.out.println("y = " + y); } public int getz() { z = Math.sqrt((x*x)+(y*y)); System.out.println("z = " + z); } }``` Can you see the problem?
 10-14-2008, 05:23 PM PM User | #4 tomws Senior Coder     Join Date: Nov 2007 Location: Arkansas Posts: 2,644 Thanks: 29 Thanked 330 Times in 326 Posts Yes, and the answer is still the same. x, y, and z are still not declared before use. Your functions (or "methods", I think Java calls them) have their own variable scope. So, x is not declared in getx(), y is not declared in gety(), etc. Have a Google for "variable scope" and you should find a load of resources to help understand the concept. By the way, also check out the "this" keyword. I'm a Java noob, but it should be something like: `this.x = xlength;` or `this.z = Math.sqrt((x*x)+(y*y));` Use `this` to reference a class' own members and methods.
 10-15-2008, 02:54 AM PM User | #5 Fou-Lu God Emperor     Join Date: Sep 2002 Location: Saskatoon, Saskatchewan Posts: 16,615 Thanks: 4 Thanked 2,599 Times in 2,568 Posts If I'm not mistaken, java requires the use of 'this' only when the context has been masked by a local scoped variable. So, using x within a method where x is a global variable is perfectly acceptable. The problem is simple - Math.sqrt is a double argument function, not an int function. You'll need to cast you're ints into doubles first. Even better, just change all of you're int references into doubles for simplicity. This function also returns a double, so you'll need to change you're method signature. I don't know the behaviour you want though, it looks like you're methods should redeclare the use of x, y, and z as returned values though, instead of altering the originals. Up to you, if you're calculations are correct, the Pythagoran theorem would say that it should be the same anyway. However, you'll need to get the sqrt of an absolute number - it will die if it ever hits a negative number. __________________ PHP Code: ``` header('HTTP/1.1 420 Enhance Your Calm');  ```
 Users who have thanked Fou-Lu for this post: webguy08 (10-15-2008)
10-15-2008, 12:50 PM   PM User | #6
shyam
Senior Coder

Join Date: Jul 2005
Posts: 1,563
Thanks: 2
Thanked 163 Times in 160 Posts
Quote:
 Originally Posted by webguy08 Code: ```public class Pythagoras { ... public int getx() { x = Math.sqrt((y*y)-(z*z)); System.out.println("x = " + x); } public int gety() ... }``` Can you see the problem?
the return type of `Math.sqrt` is a double and since an `int` is of lower precision than a `double` you have to explicitly add a cast for the assignment to succeed. if you paid more attention to the the error message given by the compiler you could have easifly fixed the problem.
__________________
You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

10-15-2008, 05:46 PM   PM User | #7
webguy08
Regular Coder

Join Date: Mar 2008
Posts: 136
Thanks: 39
Thanked 1 Time in 1 Post
Quote:
 Originally Posted by shyam the return type of `Math.sqrt` is a double and since an `int` is of lower precision than a `double` you have to explicitly add a cast for the assignment to succeed. if you paid more attention to the the error message given by the compiler you could have easifly fixed the problem.
I only just started learning Java code so I didn't know what `double` was. The error message just stated "possible loss of precision", which doesn't mean much to me...

So `double` is like `int`, but enables longer numbers to be used?

How could I go about making the values in the constructor optional, so that users only have to enter 2 of the 3 values (either x, y, or z) rather than all 3?

Last edited by webguy08; 10-15-2008 at 05:48 PM..

10-15-2008, 07:01 PM   PM User | #8
shyam
Senior Coder

Join Date: Jul 2005
Posts: 1,563
Thanks: 2
Thanked 163 Times in 160 Posts
Quote:
 Originally Posted by webguy08 So `double` is like `int`, but enables longer numbers to be used?
you should review the primitive datatypes in java chapter...`int` is short for integer and `double` is used to store numbers with decimals
Quote:
 Originally Posted by webguy08 How could I go about making the values in the constructor optional, so that users only have to enter 2 of the 3 values (either x, y, or z) rather than all 3?
overload the constructor to have 2/3 parameters as you want
__________________
You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

10-18-2008, 09:37 PM   PM User | #9
webguy08
Regular Coder

Join Date: Mar 2008
Posts: 136
Thanks: 39
Thanked 1 Time in 1 Post
Quote:
 Originally Posted by shyam you should review the primitive datatypes in java chapter...`int` is short for integer and `double` is used to store numbers with decimals overload the constructor to have 2/3 parameters as you want
I knew what int was

I don't understand what you mean by "overload" the constructor.

10-19-2008, 05:37 PM   PM User | #10
shyam
Senior Coder

Join Date: Jul 2005
Posts: 1,563
Thanks: 2
Thanked 163 Times in 160 Posts
Quote:
 Originally Posted by webguy08 I knew what int was
good

Quote:
 Originally Posted by webguy08 I don't understand what you mean by "overload" the constructor.
maybe u missed the chapter on polymorphism...ok, here is an example

Code:
```public class Pythagoras
{
private int x;
private int y;
private int z;

public Pythagoras(int xlength, int ylength, int zlength)
{
x = xlength;
y = ylength;
z = zlength;
}
public Pythagoras(int xlen, int ylen) {
this(xlen, ylen, 0);
}
public Pythagoras(int xlen) {
this(xlen, 0, 0);
}
}```
notice that only the number of arguments to the constructor changes but the constructor name remains the same..(you could do that to methods as well, and instead of varying the number of arguments you can also change the type of arguments)
__________________
You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

 Users who have thanked shyam for this post: webguy08 (10-19-2008)
 10-19-2008, 06:18 PM PM User | #11 webguy08 Regular Coder   Join Date: Mar 2008 Posts: 136 Thanks: 39 Thanked 1 Time in 1 Post Oh yeh I forgot that I can create multiple constructors Thanks

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 08:51 AM.