Question

I have some problems with my code. In Mimer I get this result for the code...

I have some problems with my code. In Mimer I get this result for the code bellow:

tests.cpp:29: Failure
The difference between p1.distanceTo(p2) and 5.0 is 0.75735931288071523, which exceeds EPS, where
p1.distanceTo(p2) evaluates to 4.2426406871192848,
5.0 evaluates to 5, and
EPS evaluates to 1.0000000000000001e-05.

Can I get some help what I did wrong here, The assignment is below:

Write a class called Point that contains two doubles that represent its x- and y-coordinates. It should have get and set methods for both fields. It should have a constructor that takes two double parameters and initializes its coordinates with those values. It should have a default constructor that initializes both coordinates to zero. It should also contain a method called distanceTo that takes as a parameter another Point and returns the distance from the Point that was passed as a parameter to the Point that we called the method of. You will need to use sqrt(). For example at the end of the following, dist should be equal to 5.0:

Point p1(-1.5, 0.0);
Point p2(1.5, 4.0);
double dist = p1.distanceTo(p2);

Next, write a class called LineSegment that contains two Point-pointers, where each holds the addressof a Point object that represents an endpoint of the LineSegment. It should have get and set methods for both fields and a constructor that takes the addresses of two Point objects and passes them to the set methods to initialize the data members. Your LineSegment constructor and set and get methods should only be working with Point-pointers - they shouldn't do anything with x- and y-coordinates.

LineSegment should also contain a method called length that returns the length of the LineSegment – by using the distanceTo method on its endpoints – and a method called slope that returns the slope of the LineSegment.  You don't need to do anything special for vertical lines - division by zero will result in the special value inf, which stands for "infinity". Your program will not be tested with line segments where both endpoints have the same coordinates. The LineSegement class might be used as follows:

Point p1(4.3, 7.52);
Point p2(-17.0, 1.5);
LineSegment ls1(&p1, &p2);
double length = ls1.length();
double slope = ls1.slope();

The functions for the Point class should have the following names:

  • setXCoord, getXCoord
  • setYCoord, getYCoord
  • distanceTo

The functions for the LineSegment class should have the following names:

  • setEnd1, getEnd1
  • setEnd2, getEnd2
  • length
  • slope

The files must be named: Point.hpp, Point.cpp, LineSegment.hpp and LineSegment.cpp

Point.cpp and LineSegment.hpp should both #include Point.hpp. LineSegment.cpp should #include LineSegment.hpp. The main method you write for testing will also need to include LineSegment.hpp. If you named the file with your main method "geomMain.cpp", then you can compile your program with "g++ Point.cpp LineSegment.cpp geomMain.cpp -o geom".




#include "LineSegment.hpp"
#include <iostream>

/***********************************************
** Name: LineSeqment
** Description: Pointer setEnd1
************************************************/
LineSegment::LineSegment(Point *point1, Point *point2)
{
    setEnd1(point1);
    setEnd2(point2);
}
/***********************************************
** Name: setEnd1
** Description: setter for end1;
************************************************/
void LineSegment::setEnd1(Point *point)
{
    end1 = point;
}
/***********************************************
** Name: setEnd2
** Description: setter for end2;
************************************************/
void LineSegment::setEnd2(Point *point)
{
    end2 = point;
}
/***********************************************
** Name: getEnd1()
** Description: getter for end1 and return value.
************************************************/
Point* LineSegment::getEnd1()
{
    return end1;
}
/***********************************************
** Name: getEnd2()
** Description: getter for end1 and return value.
************************************************/
Point* LineSegment::getEnd2()
{
    return end2;
}
/***********************************************
** Name: length()
** Description: return the value and distanceTo.
************************************************/
double LineSegment::length()
{
    return end1->distanceTo(*end2);
}
/***********************************************
** Name: slope()
** Description: return the rx/ry value for all of
**              the coordenates. Returns the slope
 *              of the lineSegment.
************************************************/
double LineSegment::slope()
{
    double ry = end2->getYCoord() - end1->getYCoord();
    double rx = end2->getXCoord() - end1->getXCoord();
    return ry/rx;
}
#ifndef LineSegment_hpp
#define LineSegment_hpp
#include "Point.hpp"

class LineSegment
{
    private:
        Point
            *end1,
            *end2;
    public:
        LineSegment(Point *point1, Point *point2);
        void setEnd1(Point *point);
        void setEnd2(Point *point);
        Point* getEnd1();
        Point* getEnd2();
        double length();
        double slope();
};
#endif
#include <iostream>
#include <cmath>
#include "Point.hpp"
/***********************************************
** Name: Point()
** Description: Defualt Constructor Point()
 *              xCoord and yCoord set to 0;
************************************************/
Point::Point()
{
    xCoord = 0;
    yCoord = 0;
}
/***********************************************
** Name: Point(double x, double y)
** Description: Constructor that takes two
**              double parameters.
************************************************/
Point::Point(double x, double y)
{
    xCoord = x;
    yCoord = y;
}
/***********************************************
** Name: setXCoord(double x)
** Description: setter for xCoord;
************************************************/
void Point::setXCoord(double x)
{
    xCoord = x;
}
/***********************************************
** Name: setYCoord(double y)
** Description: setter for yCoord;
************************************************/
void Point::setYCoord(double y)
{
    yCoord = y;
}
/***********************************************
** Name: getXCoord(double x)
** Description: getter for xCoord, returns value;
************************************************/
double Point::getXCoord()
{
    return xCoord;
}
/***********************************************
** Name: getYCoord(double y)
** Description: getter for yCoord, returns value;
************************************************/
double Point::getYCoord()
{
    return yCoord;
}
/****************************************************
** Name: distanceTo(const Point p2)
** Description: getter for distanceTo, returns value;
**              takes as a parameter another Point
**              and returns the distance from the
**              Point that was passed as a parameter
**              to the Point that we called.
*****************************************************/
double Point::distanceTo(const Point p2)
{
    double rx = xCoord - p2.yCoord;
    double ry = yCoord - p2.yCoord;
    return sqrt(rx*rx + ry*ry);
}
#ifndef Point_hpp
#define Point_hpp
class Point
{
    private:
        double xCoord, yCoord;
    public:
        //Constructors
        Point();
        Point(double x, double y);
        //Function Declarations
        void setXCoord(double x);
        void setYCoord(double y);
        double getXCoord();
        double getYCoord();
        double distanceTo(const Point p2);
};
#endif
0 0
Add a comment Improve this question Transcribed image text
Answer #1

See the Error in your Code

double Point::distanceTo(const Point p2)
{
    double rx = xCoord - p2.yCoord; //This should be xCoord - p2.xCoord
    double ry = yCoord - p2.yCoord;
    return sqrt(rx*rx + ry*ry);
}

Corrected code

double Point::distanceTo(const Point p2)
{
    double rx = xCoord - p2.xCoord;
    double ry = yCoord - p2.yCoord;
    return sqrt(rx*rx + ry*ry);
}

Thanks, PLEASE COMMENT if there is any concern.
Add a comment
Know the answer?
Add Answer to:
I have some problems with my code. In Mimer I get this result for the code...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a class called Point that contains two doubles that represent its x- and y-coordinates. It...

    Write a class called Point that contains two doubles that represent its x- and y-coordinates. It should have get and set methods for both fields. It should have a constructor that takes two double parameters and initializes its coordinates with those values. It should have a default constructor that initializes both coordinates to zero. It should also contain a method called distanceTo that takes as a parameter another Point and returns the distance from the Point that was passed as...

  • Question 2 - Programming Exercise 1. Make a directory for this lab and change into it....

    Question 2 - Programming Exercise 1. Make a directory for this lab and change into it. 2. Copy files using the following command: cp/net/data/ftp/pub/class/115/ftp/cpp/Inheritance/Exercise.cpp Exercise.cpp Finish the program so that it compiles and runs. The instructions are contained in the C++ file. Your completed program should generate output similar to the following: TwoD default constructor This program asks for the coordinates of two points in 3D space and calculates their distance. Please enter the xyz coordinates for the first point:...

  • Extend the code from Lab6.A template is given to you with CircleHeader.h, Circle.cpp and CircleMain.cpp Use...

    Extend the code from Lab6.A template is given to you with CircleHeader.h, Circle.cpp and CircleMain.cpp Use the same Circle UML as below and make extensions as marked and as needed. Given below is a UML for the Painting Class. You will need to write PaintingHeader.h and Painting.cpp. The Painting Class UML contains a very basic skeleton. You will need to flesh out this UML and add more instance variables and methods as needed. You do NOT need to write a...

  • Create a MyPoint class to model a point in a two-dimensional space. The MyPoint class contains:...

    Create a MyPoint class to model a point in a two-dimensional space. The MyPoint class contains: • Two data fields x and y that represent the coordinates. • A no-arg constructor that creates a point (0,0). • A constructor that constructs a point with specified coordinates. • Two get functions for data fields x and y, respectively. • Two set functions for data fields x and y, respectively. • A function named distance that returns the distance from this point...

  • Java Help 2. Task: Create a client for the Point class. Be very thorough with your...

    Java Help 2. Task: Create a client for the Point class. Be very thorough with your testing (including invalid input) and have output similar to the sample output below: ---After declaration, constructors invoked--- Using toString(): First point is (0, 0) Second point is (7, 13) Third point is (7, 15) Second point (7, 13) lines up vertically with third point (7, 15) Second point (7, 13) doesn't line up horizontally with third point (7, 15) Enter the x-coordinate for first...

  • This is my code that i need to finish. In BoxRegion.java I have no idea how...

    This is my code that i need to finish. In BoxRegion.java I have no idea how to create the constructor. I tried to use super(x,y) but It is hard to apply. And Also In BoxRegionHashTable, I don't know how to create displayAnnotation BoxRegion.java ------------------------------------------------ public final class BoxRegion { final Point2D p1; final Point2D p2; /** * Create a new 3D point with given x, y and z values * * @param x1, y1 are the x,y coordinates for point...

  • C++ Could you check my code, it work but professor said that there are some mistakes(check...

    C++ Could you check my code, it work but professor said that there are some mistakes(check virtual functions, prin() and others, according assignment) . Assignment: My code: #include<iostream> #include<string> using namespace std; class BasicShape { //Abstract base class protected: double area; private:    string name; public: BasicShape(double a, string n) { area=a; name=n; } void virtual calcArea()=0;//Pure Virtual Function virtual void print() { cout<<"Area of "<<getName()<<" is: "<<area<<"\n"; } string getName(){    return name; } }; class Circle : public...

  • INSTRUCTIONS: I NEED TO CREATE A PointApp PROGRAM THAT USES THE FOLLOWING API DOCUMENTATION. Below the...

    INSTRUCTIONS: I NEED TO CREATE A PointApp PROGRAM THAT USES THE FOLLOWING API DOCUMENTATION. Below the API Documentation is the code I submitted. However, the output is different for what they are asking. I am looking for someone to fix the code to print out the correct output and to add comments so I can have an idea in how the code works. PLEASE AND THANK YOU. API DOCUMENTATION: public class Point extends java.lang.Object The Point class is used to...

  • Start by using the starter code provided for the Line class. It implements operator overloading so...

    Start by using the starter code provided for the Line class. It implements operator overloading so you can use cin/cout with line objects. A line will be made up of two points. Create an object to implement a “line” class which allows the programmer to store a line. This class must use the “point” class developed in Part B. The object should have two constructors, appropriate set/get functions, and overloaded I/O (cin/cout) functions. It should include functions the return the...

  • Hello I have a question. I would like to check if the code follows this requirement....

    Hello I have a question. I would like to check if the code follows this requirement. Rectangle.h - A complete Rectangle Class including both declaration and definition appRectangle,cpp separate in two different files the class definition and implementation Code: rectangle.h // Rectangle class declaration. class Rectangle { private: double width; double length; public: void setWidth(double); void setLength(double); double getWidth() const; double getLength() const; double getArea() const; }; //************************************************** // setWidth assigns a value to the width member. * //************************************************** void...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT