Bookmark Topic Watch Topic
  • New Topic

Distance formula class  RSS feed

 
Jennings Cavil
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Report post to moderator
Help! I've found a few distance formula testers online but no classes.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Report post to moderator
I don't really know how to start it or what instance variables to include
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Report post to moderator
**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
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!