Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ A point to be drawn on a graphical window can be represented by a pair of in

ID: 3689942 • Letter: C

Question

C++

A point to be drawn on a graphical window can be represented by a pair of integer coordinates (x, y). For this lab exercise, you shall implement a C++ class named Point which will encapsulate three data members mX, mY, and mColor which are all ints (I use the convention where I name all of my data members with a leading 'm' character so their names will not conflict with the names of formal parameters or local variables; you do not have to do this). mX and mY are the coordinates of the point on the graphical window and mColor is the color of the point. Here is the UML class diagram for the Point class to be implemented.

We will also create another class named PointTest which is designed to test the Point class functions to ensure they are correctly written. The main() function in Lab09.cpp will simply instantiate (create) a PointTest object named pointTest and will then call point Test.run() to perform the testing. The run() function of the PointTest class will contain code which creates Point objects, and then calls the various Point class functions on the allocated Point objects to verify the functions are correctly implemented. Here is the UML class diagram for the PointTest class.

We discussed and implemented most of the Point class function members in the lecture so referring to your lecture notes may be helpful. The code for the first version of the Point class (lacking the mColor data member and the distance(), init(), toString(), and

additional ctor) can be found on the course website, in source code files Point.hpp and Point.cpp. The functions you will need to modify and/or add are discussed below.

+ Point(int pInitX, int pInitY, int pInitColor) :

A third constructor which can be called to initialize all three data members to the parameters. Typical usage,

Point q(300, 200, 2); // q is at (300, 200) and is whatever color 2 represents

+ distance (pAnotherPoint : Point&) : double

Calculates and returns the distance between the Point object on which the function is invoked (in the function, referred to as this Point) and the input Point parameter pAnotherPoint (which is passed by-reference). Typical usage,

Point p1(1, 2); // p1 is at (1, 2) Point p2(3, 4); // p2 is at (3, 4)

double dist1 = p1.distance(p2); // dist1 assigned 2.82843. this Point is p1 and pAnotherPoint is p2. double dist2 = p2.distance(p1); // dist2 assigned 2.82843. this Point is p2 and pAnotherPoint is p1.

+ getColor() : int   and   + setColor(pNewColor : int) : void Accessor and mutator functions for the mColor data member.

+ toString () : string

Returns a string representation of the Point object on which this function is called. For example, if the mX and mY data members are

10 and -33 and mColor is 2, this function will return the string "(10, -33), 2". For example,

Point q(300, 200, 2);            // q is at (300, 200) and is whatever color 2 represents cout << "q = " << q.toString(); // outputs: q = (300, 200); color is 2

? init (pX : int, pY : int, pColor : int) : void

A private function which is only callable from the other functions of the Point class. This function shall be called from the ctors, to initialize the object being instantiated. When called from the default ctor, the input parameters should be 0, 0, and 0. When called from the second ctor, the input parameters should be the pInitX and pInitY parameters to the second constructor and 0 for the color. When called from the third ctor, the input parameters should be the pInitX, pInitY, and pInitColor parameters to the third constructor.

this lab is broken into a Pointtest.cpp for the functions, Point.hpp for uml diagram class, pointtest.cpp for the uml, and our main function, using basic class, fucntions, and stringstream sout

Explanation / Answer

Point.h

#include <string>
#include <cmath>
using namespace std;
class Point
{
   public:
       Point();
       Point(int pX,int pY);
       Point(int   pX,int pY,int pColor);
       double distance(Point& p);
       int getColor();
       int getX();
       int getY();
       void move(int pNewX,int pNewY);
       void setColor(int pColor);
       void setX(int pX);
       void setY(int pY);
       string toString();
   private:
       int mX,mY,mColor;
       void init(int pX,int pY,int pZ);
};

Point::Point()
{
   init(0,0,0);
}

Point::Point(int pX,int pY)
{
   init(pX,pY,0);
}

Point::Point(int pX,int pY,int pColor)
{
   init(pX,pY,pColor);
}

double Point::distance(Point &p)
{
   int xDiff=this->mX-p.mX;
   int yDiff=this->mY-p.mY;
   double d=sqrt(xDiff*xDiff+yDiff*yDiff);
   return d;
}

int Point::getColor()
{
   return mColor;
}

int Point::getX()
{
   return mX;
}

int Point::getY()
{
   return mY;
}

void Point::move(int pNewX,int pNewY)
{
   mX=pNewX;
   mY=pNewY;
}

void Point::setColor(int pColor)
{
   mColor=pColor;
}

void Point::setX(int pX)
{
   mX=pX;
}

void Point::setY(int pY)
{
   mY=pY;
}

void Point::init(int pX,int pY,int pColor)
{
   mX=pX;
   mY=pY;
   mColor=pColor;
}

string Point::toString()
{
   string s="("+to_string(mX)+","+to_string(mY)+"), "+to_string(mColor);
   return s;
}

PointTest.h

#include <iostream>
#include "Point.h"
using namespace std;

class PointTest
{
public:
   static void run();
};

void PointTest::run()
{
   Point p(2,5,1);


   if(2!=p.getX())
   {
       cout<<"Point class test failed"<<endl;
   }
   cout<<"X coordinate is "<<p.getX()<<endl;

   if(5!=p.getY())
   {
       cout<<"Point class test failed"<<endl;
   }
   cout<<"Y coordinate is "<<p.getY()<<endl;

   if(1!=p.getColor())
   {
       cout<<"Point class test failed"<<endl;
   }
   cout<<"Color is "<<p.getColor()<<endl;

   Point p1(0,5,3);
   if(p.distance(p1)!=2)
   {
       cout<<"Test failed in distance function"<<endl;
   }
   cout<<"distance from point (0,5) is "<<p.distance(p1)<<endl;

   cout<<p.toString()<<endl;
}

test.cpp

#include "PointTest.h"

int main()
{
   PointTest::run();
}

Sample output:

X coordinate is 2

Y coordinate is 5

Color is 1

distance from point (0,5) is 2

(2,5), 1