hw3

cpp

School

Dartmouth College *

*We aren’t endorsed by this school

Course

PROGRAMMIN

Subject

Computer Science

Date

Nov 24, 2024

Type

cpp

Pages

5

Uploaded by ProfWildcatMaster683

Report
/*********************************************** Author: Shreya Mishra Date: Sept. 22nd, 2023 Purpose: The user has to type the definition of class labeled "Point" which holds and changes the position of a point inside the plane. We must declare and implement the following member functions: A default constructor that initializes the points location to (0, 0). A value constructor that allows setting the initial location using given x and y coordinates. Accessor and mutator functions to retrieve and update the current coordinates of the point. A member function to move the point by a specified amount along the horizontal and vertical directions. For example, if the current coordinates are (3, 4) and moved by (1, 2), it means moving one unit to the right and two units. A member function to rotate the point by 90 degrees clockwise around the origin. Sources of Help: Time Spent: 3-4 hours ***********************************************/ #include <iostream> using namespace std; class Point{ private: double x,y; public: Point(); //default constructor Point(double,double); //value constructor // functions that sets the point coordinates (mutator) void setX(double); void setY(double); // function to move the point in vertical and horizontal directions void moveP(double,double); // function to rotate the point 90 degrees around the origin clockwise void rotateP(); // functions to retrieve the current coordinates of the point
(accessor) double getX()const; double getY()const; }; //(1) We start with (1, 2). This point is rotated 90 degrees, four times, getting back to the original point. void test_one() { Point p1(1, 2); cout << "Original point is: x= " << p1.getX() << " y= " << p1.getY() << endl; p1.rotateP(); cout << "First rotation point is: x= " << p1.getX() << " y= " << p1.getY() << endl; p1.rotateP(); cout << "Second rotation point is: x= " << p1.getX() << " y= " << p1.getY() << endl; p1.rotateP(); cout << "Third rotation point is: x= " << p1.getX() << " y= " << p1.getY() << endl; p1.rotateP(); cout << "After four rotations the point is: x= " << p1.getX() << " y= " << p1.getY() << endl; } //(2) The point(3, 4) is set and moved by(1, 1), that is, the point moves one up, one right. void test_two() { Point p2(3, 4); cout << "Original point is: x= " << p2.getX() << " y= " << p2.getY() << endl; p2.moveP(1, 1); cout << "After moving point is: x= " << p2.getX() << " y= " << p2.getY() << endl; } //(3) A second point, (5, -4) is set and moved by (-5, 4) 5 left, 4 up. Then we move it back to the origin, (0, 0). void test_three() { Point p2(5, -4); cout << "Original point is: x= " << p2.getX() << " y= " << p2.getY() << endl; p2.moveP(-5, 4); cout << "After moving point is: x= " << p2.getX() << " y= " << p2.getY() << endl; }
// The default constructor Point::Point(){ x=0.0; y=0.0; } //The double constructor Point::Point(double x1, double y1) { x = x1; y = y1; } // sets x to X void Point::setX(double X){ x=X; } // sets y to Y void Point::setY(double Y){ y=Y; } // get value of x double Point::getX() const{ return x; } // get value of y double Point::getY() const{ return y; } //moves the point horizontally and vertically void Point::moveP(double horiz, double vert) { x += horiz; y += vert; } // rotate 90 degrees clockwise // flip x & y & change the sign of new y void Point::rotateP() { double temp; temp = x; x = y;
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
y = temp; y = -1 * y; } //Your driver should test all functions void driver() { Point A; double numX, numY; int rotations, totalRotations; cout << "Please enter a x value: "; cin >> numX; cout << "Please enter a y value: "; cin >> numY; A.setX(numX); A.setY(numY); cout << "Your point is currently at (" << A.getX() << "," << A.getY() << ")."; cout << endl << "Please enter the horizontal distance to move: "; cin >> numX; cout << "Please enter the vertical distance to move: "; cin >> numY; A.moveP(numX, numY); cout << "Your point is now at (" << A.getX() << "," << A.getY() << ")."; cout << endl << "Please enter the amount of times you would like the point to be rotated: "; cin >> rotations; totalRotations = rotations; for (; rotations > 0; rotations--) { A.rotateP(); } cout << "After rotating " << totalRotations << " times, your point is now at (" << A.getX() << "," << A.getY() << ")."; cout << endl; }
int main(int argc, const char* argv[]) { test_one(); test_two(); test_three(); driver(); return 0; } /* Computing III -- COMP.2010 Honor Statement The practice of good ethical behavior is essential for maintaining good order in the classroom, providing an enriching learning experience for students, and as training as a practicing computing professional upon graduation. This practice is manifested in the Universitys Academic Integrity policy. Students are expected to strictly avoid academic dishonesty and adhere to the Academic Integrity policy as outlined in the course catalog. Violations will be dealt with as outlined therein. All programming assignments in this class are to be done by the student alone. No outside help is permitted except the instructor and approved tutors. I certify that the work submitted with this assignment is mine and was generated in a manner consistent with this document, the course academic policy on the course website on Blackboard, and the UMass Lowell academic code. Date Sept. 24th, 2023 Name: Shreya Mishra */;