In: Computer Science
In C++ fill in the comments the easiest to get the program work and get this output.
Sample Output
Please input the x: 2.5
Please input the y: -5
Please input the x: 3
Please input the y: 7.5
You entered: ( 2.5, -5 ) and ( 3, 7.5 )
point3 is ( 2.5, -5 )
Press any key to continue . . .
#include <iostream> // For cout and cin
#include <iomanip> // For formatting
output, e.g. setprecision
#include <string> // For using string
data type
using namespace std; // Avoid need to use std:: qualification on cout and other things
class Point {
private:
// WRITE declarations of x and y as
doubles
// x and y coordinates of the point
public:
// WRITE declaration of getX, getY, setX, setY,
readCoords, and printCoords
// WRITE definitions of these member functions
after definition of main().
// getX - Get the value of the x member
variable
// getY - Get the value of the y member
variable
// setX - Set the value of the x member
variable
// setY - Set the value of the y member
variable
// readCoords - Fill in the x, y values with
user input
// printCoords - Display in format ( x, y
)
// For Future Lab:
// double getSlope( Point
); // Determine slope between this
point and another point
// For Future Lab:
// Point getMidpoint( Point );
// Determine midpoint between this point and another point
};
int main()
{
// Do "Press any key to continue..." on
exit
atexit([] {system("pause"); });
Point point1; // Two points for testing
methods
Point point2; //
// Obtain values for points from user.
point1.readCoords();
point2.readCoords();
// Show the values on the console.
cout << "You entered: ";
point1.printCoords();
cout << " and ";
point2.printCoords();
cout << endl;
// Create a point3 that is a pointer to a
Point object.
// Initialize point3 to a Point object that you
allocate using the new operator.
// Set its x and y from point1, and print its
coords.
// Finally, delete the object.
// WRITE code for point3 here.
// For future lab: Report the slope of the line
connecting the points.
//cout << "The slope between them is "
<< point1.getSlope(point2) << endl;
// For future lab: Report the midpoint of the
line connecting the points.
//cout << "The midpoint between them is
";
//point1.getMidpoint(point2).printCoords();
//cout << endl;
return 0;
}
//
=================================================================
// getX()
// return the value of the x attribute
//
// Purpose: Provide the user with access to the x value
// Input: none
// Output: returns the value of the objects x data member
//
// YOU WRITE THIS
}
//
=================================================================
// getY()
// return the value of the y attribute
//
// Purpose: Provide the user with access to the y value
// Input: none
// Output: returns the value of the objects y data member
//
// YOU WRITE THIS
//
=================================================================
// setX()
// sets the value of the x attribute
//
// Purpose: Allows the user to change the x value
// Input: the new x value
// Output: none
//
// YOU WRITE THIS
//
=================================================================
// setY()
// sets the value of the y attribute
//
// Purpose: Allows the user to change the y value
// Input: the new y value
// Output: none
//
// YOU WRITE THIS
//
=================================================================
// readCoords()
// fill in the x,y with user input.
//
// Purpose: Asks the user for the x,y coordinates and
//
sets the values into the object.
// Input: none
// Output: none
//
void Point::readCoords {
// YOU WRITE THIS
} // end readCoords()
//
=================================================================
// printCoords()
// display in format (x,y))
//
// Purpose: Display the x and y attributes on the console in the
format ( x, y )).
// Assume: x,y have been initialized.
// Input: none
// Output: none
//
void Point:: printCoords() {
// YOU WRITE THIS
} // end printCoords())
/*
//
=================================================================
// getSlope()
// Calculate the slope between two points
//
// Purpose: Given a second point, calculates and returns the
// value of the slope between those two points as defined by
// m = ( y2 - y1 ) / ( x2 - x1
)
// Assume: This point, and the other point, have both been
initialized.
// Input: A second point object
// Output: Returns the slope of the line segment between
them.
//
double Point::getSlope( Point other ) {
// YOU WRITE THIS (for future lab)
// Note: x1 and y1 are the x and y in this object
// while x2 and y2 are the x
and y in the parameter other.
return 0.0;
} // end getSlope()
//
=================================================================
// getMidpoint())
// Determine midpoint between two points
//
// Purpose: Calculate the midpoint between points in 2 point
objects.
// The midpoint is defined by ( (x1+x2)/2.0,
(y1+y2)/2.0 ).
// Assume: This point, and the other point, have both been
initialized.
// Input: A second point object
// Output: Returns the point that is the midpoint between them.
Point Point::getMidpoint( Point other ) {
Point midpoint; // between this point and
'other'
// Initialize midpoint so that code compiles
without errors before
// implementing this function.
midpoint.x = 0.0;
midpoint.y = 0.0;
// YOU WRITE THIS (for future lab)
// Note: x1 and y1 are the x and y in this
object
// while x2
and y2 are the x and y in the parameter other.
return midpoint;
} // end getMidpoint()
*/
I have done the required work and this program will give the given output with the given output:
#include <iostream> // For cout and cin
#include <iomanip> // For formatting output, e.g.
setprecision
#include <string> // For using string data type
using namespace std; // Avoid need to use std:: qualification on cout and other things
class Point {
private:
// WRITE declarations of x and y as doubles
// x and y coordinates of the point
double x, y;
public:
// WRITE declaration of getX, getY, setX, setY, readCoords, and
printCoords
// WRITE definitions of these member functions after definition of
main().
// getX - Get the value of the x member variable
double getX();
// getY - Get the value of the y member variable
double getY();
// setX - Set the value of the x member variable
void setX(double);
// setY - Set the value of the y member variable
void setY(double);
// readCoords - Fill in the x, y values with user input
void readCoords();
// printCoords - Display in format ( x, y )
void printCoords();
// For Future Lab:
// double getSlope( Point ); // Determine slope between this point
and another point
// For Future Lab:
// Point getMidpoint( Point ); // Determine midpoint between this
point and another point
};
int main()
{
// Do "Press any key to continue..." on exit
atexit([] {system("pause"); });
Point point1; // Two points for testing methods
Point point2; //
// Obtain values for points from user.
point1.readCoords();
point2.readCoords();
// Show the values on the console.
cout << "You entered: ";
point1.printCoords();
cout << " and ";
point2.printCoords();
cout << endl;
// Create a point3 that is a pointer to a Point object.
// Initialize point3 to a Point object that you allocate using the
new operator.
// Set its x and y from point1, and print its coords.
// Finally, delete the object.
// WRITE code for point3 here.
Point *point3;
point3 = new Point;
*point3 = point1;
cout << "Third Point is: ";
point3->printCoords();
delete point3;
cout << endl;
// For future lab: Report the slope of the line connecting the
points.
//cout << "The slope between them is " <<
point1.getSlope(point2) << endl;
// For future lab: Report the midpoint of the line connecting
the points.
//cout << "The midpoint between them is ";
//point1.getMidpoint(point2).printCoords();
//cout << endl;
return 0;
}
//
=================================================================
// getX()
// return the value of the x attribute
//
// Purpose: Provide the user with access to the x value
// Input: none
// Output: returns the value of the objects x data member
//
// YOU WRITE THIS
double Point::getX() {
return x;
}
//
=================================================================
// getY()
// return the value of the y attribute
//
// Purpose: Provide the user with access to the y value
// Input: none
// Output: returns the value of the objects y data member
//
// YOU WRITE THIS
double Point::getY() {
return y;
}
//
=================================================================
// setX()
// sets the value of the x attribute
//
// Purpose: Allows the user to change the x value
// Input: the new x value
// Output: none
//
// YOU WRITE THIS
void Point::setX(double a) {
x = a;
}
//
=================================================================
// setY()
// sets the value of the y attribute
//
// Purpose: Allows the user to change the y value
// Input: the new y value
// Output: none
//
// YOU WRITE THIS
void Point::setY(double a) {
y = a;
}
//
=================================================================
// readCoords()
// fill in the x,y with user input.
//
// Purpose: Asks the user for the x,y coordinates and
// sets the values into the object.
// Input: none
// Output: none
//
void Point::readCoords() {
cout << "Please input the x: ";
cin >> x;
cout << "Please input the y: ";
cin >> y;
} // end readCoords()
//
=================================================================
// printCoords()
// display in format (x,y))
//
// Purpose: Display the x and y attributes on the console in the
format ( x, y )).
// Assume: x,y have been initialized.
// Input: none
// Output: none
//
void Point:: printCoords() {
cout << "(" << x << ", " << y <<
")";
} // end printCoords())
Hope this helps. If you have any queries or suggestions regarding the answers please leave them in the comments section so I can update and improve the answer. Thank you.