programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Distance formula class

Jennings Cavil
Greenhorn
Posts: 8
Help! I've found a few distance formula testers online but no classes.

Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Welcome to the Ranch!

Can you please explain what your question is about in more detail? What kind of distance formula do you mean? Where did you find those testers that you mentioned?

With just this one line you posted, it is hard to understand what your question is about and what exactly you want help with.

Jennings Cavil
Greenhorn
Posts: 8
I am trying to write a class that caluclates the distance formula d=square root of (x2-x1)^2+(y2-y1)^2

Jennings Cavil
Greenhorn
Posts: 8
I don't really know how to start it or what instance variables to include

Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Ok, so what does your Java code currently look like and where exactly are you stuck?

The class java.lang.Math contains a sqrt() method which you can use to calculate a square root.

Jennings Cavil
Greenhorn
Posts: 8
**This program calculates the distance formula for 2 points that are entered.*/
public class DistanceFormulaMC
{
//instance variables
private double xOne, yOne, xTwo, yTwo;
private double distance;
{
public DistanceFormulaMC( double x1, double y1, double x2, double y2)
{
xOne=x1;
yOne=y1;
xTwo=x2;
yTwo=y2;
}
public void calcDistance()
{
distance=Math.sqrt((x2-x1)*(x2-x1)+(yt-y1));
}
}

Campbell Ritchie
Marshal
Posts: 56599
172
Jesper de Jong wrote:. . . The class java.lang.Math contains a sqrt() method which you can use to calculate a square root.
It also has a method which calculates the distance directly.

You won't be able to get that code to compile because your variables are out of scope in the second method. Use the fields.

And welcome again

Henry Wong
author
Sheriff
Posts: 23295
125
Duplicate topic ... https://coderanch.com/t/670946/java/java/distance-formula-class-reward