Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-13-2008, 06:40 PM   PM User | #1
webguy08
Regular Coder

 
Join Date: Mar 2008
Posts: 136
Thanks: 39
Thanked 1 Time in 1 Post
webguy08 is an unknown quantity at this point
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
webguy08 is offline   Reply With Quote
Old 10-13-2008, 08:21 PM   PM User | #2
tomws
Senior Coder

 
tomws's Avatar
 
Join Date: Nov 2007
Location: Arkansas
Posts: 2,644
Thanks: 29
Thanked 330 Times in 326 Posts
tomws will become famous soon enoughtomws will become famous soon enough
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.
tomws is offline   Reply With Quote
Old 10-14-2008, 03:38 PM   PM User | #3
webguy08
Regular Coder

 
Join Date: Mar 2008
Posts: 136
Thanks: 39
Thanked 1 Time in 1 Post
webguy08 is an unknown quantity at this point
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?
webguy08 is offline   Reply With Quote
Old 10-14-2008, 04:23 PM   PM User | #4
tomws
Senior Coder

 
tomws's Avatar
 
Join Date: Nov 2007
Location: Arkansas
Posts: 2,644
Thanks: 29
Thanked 330 Times in 326 Posts
tomws will become famous soon enoughtomws will become famous soon enough
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.
tomws is offline   Reply With Quote
Old 10-15-2008, 01:54 AM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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'); 
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
webguy08 (10-15-2008)
Old 10-15-2008, 11:50 AM   PM User | #6
shyam
Senior Coder

 
shyam's Avatar
 
Join Date: Jul 2005
Posts: 1,563
Thanks: 2
Thanked 163 Times in 160 Posts
shyam will become famous soon enough
Quote:
Originally Posted by webguy08 View Post
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
shyam is offline   Reply With Quote
Old 10-15-2008, 04:46 PM   PM User | #7
webguy08
Regular Coder

 
Join Date: Mar 2008
Posts: 136
Thanks: 39
Thanked 1 Time in 1 Post
webguy08 is an unknown quantity at this point
Quote:
Originally Posted by shyam View Post
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 04:48 PM..
webguy08 is offline   Reply With Quote
Old 10-15-2008, 06:01 PM   PM User | #8
shyam
Senior Coder

 
shyam's Avatar
 
Join Date: Jul 2005
Posts: 1,563
Thanks: 2
Thanked 163 Times in 160 Posts
shyam will become famous soon enough
Quote:
Originally Posted by webguy08 View Post
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 View Post
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
shyam is offline   Reply With Quote
Old 10-18-2008, 08:37 PM   PM User | #9
webguy08
Regular Coder

 
Join Date: Mar 2008
Posts: 136
Thanks: 39
Thanked 1 Time in 1 Post
webguy08 is an unknown quantity at this point
Quote:
Originally Posted by shyam View Post
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.
webguy08 is offline   Reply With Quote
Old 10-19-2008, 04:37 PM   PM User | #10
shyam
Senior Coder

 
shyam's Avatar
 
Join Date: Jul 2005
Posts: 1,563
Thanks: 2
Thanked 163 Times in 160 Posts
shyam will become famous soon enough
Quote:
Originally Posted by webguy08 View Post
I knew what int was
good

Quote:
Originally Posted by webguy08 View Post
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
shyam is offline   Reply With Quote
Users who have thanked shyam for this post:
webguy08 (10-19-2008)
Old 10-19-2008, 05:18 PM   PM User | #11
webguy08
Regular Coder

 
Join Date: Mar 2008
Posts: 136
Thanks: 39
Thanked 1 Time in 1 Post
webguy08 is an unknown quantity at this point
Oh yeh I forgot that I can create multiple constructors

Thanks
webguy08 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 Jump


All times are GMT +1. The time now is 07:42 AM.


Advertisement
Log in to turn off these ads.