distance

how to do

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 1630 Views - Last Post: 19 January 2009 - 02:53 PM Rate Topic: -----

#1 anne-tic  Icon User is offline

  • New D.I.C Head

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

distance

Post icon  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  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • 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  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • 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  Icon User is offline

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

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: distance

Posted 18 January 2009 - 01:42 PM

View Postmostyfriedman, 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  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: distance

Posted 18 January 2009 - 01:56 PM

distance
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

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

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: distance

Posted 18 January 2009 - 02:23 PM

View PostBigAnt, 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  Icon User is offline

  • New D.I.C Head

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

Re: distance

Posted 19 January 2009 - 10:55 AM

View Postpbl, on 18 Jan, 2009 - 12:42 PM, said:

View Postmostyfriedman, 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  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • 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  Icon User is offline

  • New D.I.C Head

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

Re: distance

Posted 19 January 2009 - 12:29 PM

View Postmarkhazlett9, 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  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • 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  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: distance

Posted 19 January 2009 - 12:55 PM

View Postpbl, on 18 Jan, 2009 - 12:42 PM, said:

View Postmostyfriedman, 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()


:lol:

Wasn't his name Pythagoras? :P

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  Icon User is offline

  • New D.I.C Head

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

Re: distance

Posted 19 January 2009 - 01:19 PM

View PostLocke37, on 19 Jan, 2009 - 11:55 AM, said:

View Postpbl, on 18 Jan, 2009 - 12:42 PM, said:

View Postmostyfriedman, 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()


:lol:

Wasn't his name Pythagoras? :P

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  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • 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  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • 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. :D

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

Was This Post Helpful? 0
  • +
  • -

#15 anne-tic  Icon User is offline

  • New D.I.C Head

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

Re: distance

Posted 19 January 2009 - 01:45 PM

View PostLocke37, 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. :D



hello there....i tried the Math.sqrt(); but still it's in error
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2