Jennings Cavil

Greenhorn

Posts: 8

posted 1 year ago

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.

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

Jennings Cavil

Greenhorn

Posts: 8

posted 1 year ago

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.

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

Jennings Cavil

Greenhorn

Posts: 8

posted 1 year ago

**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));

}

}

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

posted 1 year ago

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

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

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

posted 1 year ago

Duplicate topic ... https://coderanch.com/t/670946/java/java/distance-formula-class-reward

Please use the other topic.

Henry

Please use the other topic.

Henry