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));
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.
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);
}
}
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.
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
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?
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
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