# distance

• (2 Pages)
• 1
• 2

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

### #1 anne-tic

• New D.I.C Head

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?
Was This Post Helpful? 0

### #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..
Was This Post Helpful? 0

### #4 pbl

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

Reputation: 8378
• 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()
Was This Post Helpful? 0

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

Was This Post Helpful? 0

### #6 pbl

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

Reputation: 8378
• 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;
instead of
determinant = Math.power(deltax, 2.0); // or whatever the method name/parameters are
Was This Post Helpful? 0

### #7 anne-tic

• New D.I.C Head

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
answer = Math.sqrt(temp);

do i have to declare this one and initialize or put this on formula portion??
Was This Post Helpful? 0

### #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.
Was This Post Helpful? 0

### #9 anne-tic

• New D.I.C Head

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....
Was This Post Helpful? 0

### #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();

```

Was This Post Helpful? 0

### #11 Locke

• Sarcasm Extraordinaire!

Reputation: 550
• Posts: 5,624
• 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

Was This Post Helpful? 0

### #12 anne-tic

• New D.I.C Head

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
Was This Post Helpful? 0

### #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;

```

Was This Post Helpful? 0

### #14 Locke

• Sarcasm Extraordinaire!

Reputation: 550
• Posts: 5,624
• 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.

In your power situation...

```// 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

Was This Post Helpful? 0

### #15 anne-tic

• New D.I.C Head

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.

In your power situation...

```// 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
Was This Post Helpful? 0

• (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; }