Question

In: Computer Science

Given the definition for a Point class that holds the coordinates of the point as double...

Given the definition for a Point class that holds the coordinates of the point as double values x and y, write a function called pt_dist that takes two points and returns the straight-line distance between them (as a double). Use two ways, pt_dist function version and the pt_dist method version. In main, include two if else tests for each, If passed "TEST PASSED, DIST IS " else "Test Failed, dist is ".

Hint: Rhymes with Bythagorean Beorem.

#include <iostream>
#include <cmath>
using namespace std;

// given point class
class Point
{
public:
   double x, y;
};

// put your function here, and name it pt_dist

//method version


int main() {
   return 0;
}

Solutions

Expert Solution

HI, Please find my implementation

Please let me know in case of any issue.

#include <iostream>
#include <cmath>
using namespace std;
// given point class
class Point
{
public:
double x, y;
// method declaration
double pt_dist(Point &other);
};
// put your function here, and name it pt_dist
double pt_dist(Point &first, Point &second){
    return sqrt((first.x - second.x)*(first.x - second.x) + (first.y - second.y)*(first.y - second.y));
}

//method version
double Point::pt_dist(Point &other){
    return sqrt((other.x - x)*(other.x - x) + (other.y - y)*(other.y - y));
}

int main() {

   // creating two objects
   Point p1;
   p1.x = 3;
   p1.y = 4;

   Point p2;
   p2.x = 8;
   p2.y = 9;

   double dist1 = p1.pt_dist(p2);
   double dist2 = pt_dist(p1, p2);

   cout<<"Distance: "<<dist1<<endl;
   if(dist1 == dist2){
       cout<<"TEST PASSED, DIST IS"<<endl;
   }
   else{
       cout<<"TEST Failed, DIST IS"<<endl;
   }
return 0;
}


Related Solutions

in C++ Write a definition for a class ‘point’ to describe a point with coordinates (X,Y)...
in C++ Write a definition for a class ‘point’ to describe a point with coordinates (X,Y) in 2-D space. The private data members are the coordinates X and Y of type ‘double’. The public member functions are: A default constructor (no parameters) to initialize X and Y to zero. An explicit value constructor with two parameters to initialize the values of X and Y. Two functions, one to set the X value and the other to set the Y value...
A incomplete definition of a class Temperature is given below: public class Temperature { private double...
A incomplete definition of a class Temperature is given below: public class Temperature { private double value[] = {36.5, 40, 37, 38.3}; } [6] (i) Copy and put it in a new class. Write a method toString() of the class, which does not have any parameters and returns a string containing all the values separated by newlines. When the string is printed, each value should appear on a line in the ascending order of their indexes. Copy the content of...
Plot the point whose polar coordinates are given. Then find the Cartesian coordinates of the point...
Plot the point whose polar coordinates are given. Then find the Cartesian coordinates of the point b. (2, π/4) c.(−3, −π/6)
The Cartesian coordinates of a point are given. (a) (2, −5) (i) Find polar coordinates (r,...
The Cartesian coordinates of a point are given. (a) (2, −5) (i) Find polar coordinates (r, θ) of the point, where r > 0 and 0 ≤ θ < 2π. (r, θ) = (ii) Find polar coordinates (r, θ) of the point, where r < 0 and 0 ≤ θ < 2π. (r, θ) = (b) (-2, −2) (i) Find polar coordinates (r, θ) of the point, where r > 0 and 0 ≤ θ < 2π. (r, θ) =...
Design a class named GeoPoint to represent a point with x- and y-coordinates. The class contains:  ...
Design a class named GeoPoint to represent a point with x- and y-coordinates. The class contains:   The data fields x and y that represent the coordinates with gette and setter methods. A no-arg constructor that creates a point (0, 0).   A constructor that constructs a point with specified coordinates. The method equals(GeoPoint p) that returns true if two GeoPoint objects have the same x- and y-coordinates. Write a test program that creates an array of GeoPoint objects. The size of...
Design a class named MyPoint to represent a point with x- and y-coordinates. The class should...
Design a class named MyPoint to represent a point with x- and y-coordinates. The class should contain: Two data fields x and y that represent the coordinates. A no-arg constructor that creates a point at (0, 0). A constructor that creates a point with specified coordinates. Get methods for data fields x and y respectively. A method named distance that returns the distance from this point to another point with specified x- and y-coordinates. Use the formula: root (x2-x1)2 +...
The Cartesian coordinates of a point are given. (a)    (−3, 3) (i) Find polar coordinates (r, θ)...
The Cartesian coordinates of a point are given. (a)    (−3, 3) (i) Find polar coordinates (r, θ) of the point, where r > 0 and 0 ≤ θ < 2π. (r, θ) = (ii) Find polar coordinates (r, θ) of the point, where r < 0 and 0 ≤ θ < 2π. (r, θ) = b. (5,5sqrt(3)) (i) Find polar coordinates (r, θ) of the point, where r > 0 and 0 ≤ θ < 2π. (r, θ) = (ii) Find...
Study the following class definition: class Car { public: Car(double speed); void start(); void accelerate(double speed);...
Study the following class definition: class Car { public: Car(double speed); void start(); void accelerate(double speed); void stop(); double get_speed() const; private: double speed; }; Which of the following options would make logical sense in the definition of the void accelerate(double speed)function? Group of answer choices this->speed = this->speed; this->speed = speed; this.speed = speed; speed1 = this->speed; Flag this Question Question 131 pts The Point class has a public function called display_point(). What is the correct way of calling...
Find the polar coordinates of a point with the Cartesian coordinates
 Find the polar coordinates of a point with the Cartesian coordinates (x,y)=(−4√2,4√2).
Write a convertToMetric class that has the following field: standard - holds a double, standard length...
Write a convertToMetric class that has the following field: standard - holds a double, standard length value in feet. The class should have the following methods: Constructor - that accepts a length in feet (as a double) and stores it in the standard field. setStandard - accepts a standard length in feet and stores it in standard. getStandard - returns the value of the standard field, as a length in feet, no conversion required. getMeters - returns the value of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT