# distance

• (2 Pages)
• 1
• 2

## 20 Replies - 2063 Views - Last Post: 19 January 2009 - 02:53 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=81510&amp;s=871d0a4709994af82b587a52d6e8439f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 anne-tic

Reputation: 0
• Posts: 22
• Joined: 12-January 09

# distance

Posted 18 January 2009 - 08:54 AM

hello there.. i really dont know how to do the distance formula in java can u help me?? thanxz
Is This A Good Question/Topic? 0

## Replies To: distance

### #2 Gloin

• Expert Schmexpert...

Reputation: 235
• Posts: 4,489
• Joined: 04-August 08

## Re: distance

Posted 18 January 2009 - 08:57 AM

Are you talking about distance between two points?
In what dimension? 2-D, 3-D, n-D?

### #3 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: distance

Posted 18 January 2009 - 08:58 AM

that's an easy one, you can use the math.sqrt(), math.pow(), addition and subtraction..

### #4 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: distance

Posted 18 January 2009 - 01:42 PM

mostyfriedman, on 18 Jan, 2009 - 07:58 AM, said:

that's an easy one, you can use the math.sqrt(), math.pow(), addition and subtraction..

Pythagore was the name of the guy
You just need sqrt()

### #5 BigAnt

• May Your Swords Stay Sharp

Reputation: 102
• Posts: 2,392
• Joined: 16-August 08

## Re: distance

Posted 18 January 2009 - 01:56 PM

distance

### #6 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: distance

Posted 18 January 2009 - 02:23 PM

BigAnt, on 18 Jan, 2009 - 12:56 PM, said:

You're right
but I would have done:
deltax *= deltax;
deltay *= deltay;
determinant = Math.power(deltax, 2.0); // or whatever the method name/parameters are

### #7 anne-tic

Reputation: 0
• Posts: 22
• Joined: 12-January 09

## Re: distance

Posted 19 January 2009 - 10:55 AM

pbl, on 18 Jan, 2009 - 12:42 PM, said:

mostyfriedman, on 18 Jan, 2009 - 07:58 AM, said:

that's an easy one, you can use the math.sqrt(), math.pow(), addition and subtraction..

Pythagore was the name of the guy
You just need sqrt()

yah the pythagorean theorem.... i have this

do i have to declare this one and initialize or put this on formula portion??

### #8 markhazlett9

• Coding is a lifestyle

Reputation: 61
• Posts: 1,666
• Joined: 12-July 08

## Re: distance

Posted 19 January 2009 - 11:11 AM

Well you always always always at some point in the program need to initialize your variables... so yes you will need to initialize it.

### #9 anne-tic

Reputation: 0
• Posts: 22
• Joined: 12-January 09

## Re: distance

Posted 19 January 2009 - 12:29 PM

markhazlett9, on 19 Jan, 2009 - 10:11 AM, said:

Well you always always always at some point in the program need to initialize your variables... so yes you will need to initialize it.

import java.util.Scanner;

public class Hypotenuse
{

public static void main(String args[])
{
Scanner input=new Scanner(System.in);

int length=0;
int firstleg=^2;
int secondleg=^2;
int hypo=0;
int hypo2=0;

System.out.println( "Enter first leg of

right triangle:" );
firstleg =(int) input.nextDouble();

System.out.println( "Enter second leg of

right triangle:" );
secondleg =(int) input.nextDouble();

hypo= firstleg^2+secondleg^2;
hypo2=Math.sqrt(hypo);

System.out.printf("Hypotenuse is %d",hypo);

}

}

here's my codes.. it did run but i dont know if it's correct....

### #10 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: distance

Posted 19 January 2009 - 12:52 PM

what do u mean by
```int firstleg=^2;
int secondleg=^2;

```

and also why did you cast firstleg and secondleg, why didnt you just input them as ints
```int firstleg = input.nextInt();
int secondleg = input.nextInt();

```

### #11 Locke

• Sarcasm Extraordinaire!

Reputation: 526
• Posts: 5,604
• Joined: 20-March 08

## Re: distance

Posted 19 January 2009 - 12:55 PM

pbl, on 18 Jan, 2009 - 12:42 PM, said:

mostyfriedman, on 18 Jan, 2009 - 07:58 AM, said:

that's an easy one, you can use the math.sqrt(), math.pow(), addition and subtraction..

Pythagore was the name of the guy
You just need sqrt()

Wasn't his name Pythagoras?

And yeah, you only need Math.sqrt() and the ability to subtract. (or add, depending on the way you wanna do it)

This post has been edited by Locke37: 19 January 2009 - 12:57 PM

### #12 anne-tic

Reputation: 0
• Posts: 22
• Joined: 12-January 09

## Re: distance

Posted 19 January 2009 - 01:19 PM

Locke37, on 19 Jan, 2009 - 11:55 AM, said:

pbl, on 18 Jan, 2009 - 12:42 PM, said:

mostyfriedman, on 18 Jan, 2009 - 07:58 AM, said:

that's an easy one, you can use the math.sqrt(), math.pow(), addition and subtraction..

Pythagore was the name of the guy
You just need sqrt()

Wasn't his name Pythagoras?

And yeah, you only need Math.sqrt() and the ability to subtract. (or add, depending on the way you wanna do it)

hello there im almost done... how can i declare a double??? im so clueless that's why i tried ab=^2 stands for ab raise to the power of 2

### #13 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: distance

Posted 19 January 2009 - 01:22 PM

for doubles
```double x = input.nextDouble();

```

to raise something to the power of 2
```x *= x;

```

### #14 Locke

• Sarcasm Extraordinaire!

Reputation: 526
• Posts: 5,604
• Joined: 20-March 08

## Re: distance

Posted 19 January 2009 - 01:25 PM

Yeah...the ^ operator does not work like a power operator. It's a bit-wise operator...in other words, don't use them here.

```// raises 5 to the 4th power.
Math.pow(5, 4);
```

You can either do it that way, or if you're just squaring it...look at the post above mine.

This post has been edited by Locke37: 19 January 2009 - 01:25 PM

### #15 anne-tic

Reputation: 0
• Posts: 22
• Joined: 12-January 09

## Re: distance

Posted 19 January 2009 - 01:45 PM

Locke37, on 19 Jan, 2009 - 12:25 PM, said:

Yeah...the ^ operator does not work like a power operator. It's a bit-wise operator...in other words, don't use them here.

```// raises 5 to the 4th power.
Math.pow(5, 4);
```

You can either do it that way, or if you're just squaring it...look at the post above mine.

hello there....i tried the Math.sqrt(); but still it's in error

• (2 Pages)
• 1
• 2

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }