Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## 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));

• 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.

• 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?

• 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.

• 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.

• ## Users who have thanked Fou-Lu for this post:

webguy08 (10-15-2008)

• 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.

• 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?

• 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
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

• 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.

• Originally Posted by webguy08
I knew what int was
good

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)

• ## Users who have thanked shyam for this post:

webguy08 (10-19-2008)

• Oh yeh I forgot that I can create multiple constructors

Thanks

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•