20 Replies - 1335 Views - Last Post: 19 January 2009 - 02:53 PM
#1
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
Replies To: distance
#2
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?
In what dimension? 2-D, 3-D, n-D?
#3
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
Re: distance
Posted 18 January 2009 - 01:42 PM
#6
#7
#8
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
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
Re: distance
Posted 19 January 2009 - 12:52 PM
what do u mean by
and also why did you cast firstleg and secondleg, why didnt you just input them as ints
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
#12
Re: distance
Posted 19 January 2009 - 01:19 PM
Locke37, on 19 Jan, 2009 - 11:55 AM, said:
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
Re: distance
Posted 19 January 2009 - 01:22 PM
for doubles
to raise something to the power of 2
double x = input.nextDouble();
to raise something to the power of 2
x *= x;
#14
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...
You can either do it that way, or if you're just squaring it...look at the post above mine.
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
#15
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...
You can either do it that way, or if you're just squaring it...look at the post above mine.
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
|
|

New Topic/Question
Reply




MultiQuote







|