3. Object Oriented Programming (a) With reference to Object Oriented programming
ID: 3920921 • Letter: 3
Question
3. Object Oriented Programming (a) With reference to Object Oriented programming, what is meant by the terms Encap- 13] (b) The Polar Coordinate System is a two-dimensional coordinate system in which each point on a plane is determined by a distance from a fixed point and an angle from a sulation and Data Hiding? fixed direction. i. Design a class, PolarPoint that stores a fixed origin Point (that is shared by all instances of the PolarPoint class and is initialed using Point origin = new Point (0,0) ;) and a distance and angle from the reference point. You can assume that the angle is always relative to an infinite line extending to [51 the right of the point (see diagram). (3, bu) (0, 0) (4, 210) ii. Converting between polar and cartesian coordinate systems can be done using the sine and cosine operations. Write a method that returns a Point in the cartesian coordinate system. You may find the Math.cos (double a) and Math.sin (double a) meth- ods useful iii. Cartesian coordinates can be converted back to polar coordinates using ry(xorign)(y -origin)2 atan2((y-yorigin), (1-Zorigin)) Write a constructor method that creates a PolarPoint object from a Point object. For this question you may need to use the Math.sqrt (double a) and Math.atan2 (double y, double x) methods. iv. Write a function that calculates the shortest straight-line distance between two PolarPoint objects (Hint: This may be easier using the cartesian coordinate system)Explanation / Answer
a) Data hiding and encapsulation both are the important concept of object oriented programming. Encapsulation means wrapping the implementation of data member and methods inside a class. When implementation of all the data member and methods inside a class are encapsulated, the method name can only describe what action it can perform on an object of that class. Data Hiding Mapmeans protecting the members of a class from an illegal or unauthorized access. The main difference between data hiding and encapsulation is that data hiding focus more on data security and encapsulation focuses more on hiding the complexity of the system.
b) Please find the code below.
CODE
=============
class Point {
double x, y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
/**
* @return the x
*/
public double getX() {
return x;
}
/**
* @param x the x to set
*/
public void setX(int x) {
this.x = x;
}
/**
* @return the y
*/
public double getY() {
return y;
}
/**
* @param y the y to set
*/
public void setY(int y) {
this.y = y;
}
}
public class PolarPoint {
Point origin;
double distance, angle;
public PolarPoint(Point p) {
origin = new Point(0, 0);
distance = Math.sqrt(Math.pow(p.getX() - origin.getX(), 2) + Math.pow(p.getY() - origin.getY(), 2));
angle = Math.atan2(p.getY() - origin.getY(), p.getX() - origin.getX());
}
public Point getCartesianPoint() {
double x = distance * Math.cos(angle) + origin.getX();
double y = distance * Math.sin(angle) + origin.getY();
return new Point(x, y);
}
public static double getDistance(PolarPoint p1, PolarPoint p2) {
Point pc1 = p1.getCartesianPoint();
Point pc2 = p2.getCartesianPoint();
double dist = Math.sqrt(Math.pow(pc1.getX() - pc2.getX(), 2) + Math.pow(pc1.getY() - pc2.getY(), 2));
return dist;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.