السلام عليكم
عندي كتابة كود
السؤال
The coordinates of any point in a rectangular grid are two integers. The X-coordinate increases going East along the X-axis, and the Y-coordinate increases going North along the Y-axis.
You are to implement a class Point subject to the following guidelines:
a) Define data members ( X, and Y) that represent a coordinate for any point
b) Define the following member functions:
void moveNorth(); void moveSouth(); void moveEast(); void moveWest(); The four functions move the current Point object one unit length in the specified direction. For example if the current Point is at the coordinates X=3, Y=2, then a call to moveEast() will update the coordinates to X=4, Y=2.
c) The Point class should implement also the following operation:
int distance (Point p); The function computes the distance between the current points and P. The distance is computed by walking along horizontal and vertical lines in the grid. For example, if the current Point is at the coordinates X=3, Y=2, and the Point p is at the coordinates X=-2, Y=7, then the distance will be 10: we need to walk Math.abs(3-(-2)) = 5 units along the horizontal axis, and Math.abs(2-7) = 5 units along the vertical axis.
d) In the main, implement the below menu:
If the user chooses choice 6, the program should ask for the X and Y coordinates of the new point in order to compute the distance
الحل تبعي يطلع فيه ايرور
الحل
#include <iostream>
#include <string>
using namespace std;
class Point implements PointI {
private:
int xcoord, ycoord;
public:
Point (int x, int y) { xcoord=x; ycoord=y; }
int getX () { return xcoord; }
int getY () { return ycoord; }
void moveNorth () { ycoord++; }
void moveSouth () { ycoord--; }
void moveEast () { xcoord++; }
void moveWest () { xcoord--; }
int distance (Point p) {
int px = p.getX();
int py = p.getY();
int distance = 0;
distance = distance + Math.abs(xcoord - px);
distance = distance + Math.abs(ycoord - py);
return distance;
}
String toString () {
return "X= " + xcoord + ", Y= " + ycoord;
}
}

