If you were to get these questions on a test and could not use a computer to ans
ID: 3850247 • Letter: I
Question
If you were to get these questions on a test and could not use a computer to answer this question, only pen and paper.
How would you answer them? Please explain in detail and with step-by-step on how to approach questions like these :)
The class Point represents a planar point class Point the coordinates of the point private double x; private double public Point (double x, double y) this.x x; this y y; public double getX return this.x; public double gety return this. y; distance returns the distance between this point and a given point. public double distance (Point p) return Math.sqrt (p.x this.x) (p.x this.x) (p.y this. y) (p. y this. y)) a A static method, nearest Point, accepts an array of points (objects of type Point) and one point (an object of type Point), and returns that point in the array which is closest to the given point. Create that method. b A circle in the plane, with radius r and midpoint at the origin, is given by the following equation A static method. internal Points, accepts an array of points (objects oftype Point) and the radius of a circle, and returns those points (in an array that are inside the given circle. Create that method. c Create an array of points (objects of type Point) and a point (an object of type Point). Establish also the radius ofthe circle. Use the method nearestPoint to determine the point in the array that is closest to the given point. Then use the method internal Points to determine the points that are inside the circle.Explanation / Answer
It is very easy to answer these type of questions. As you have said you need to implement in a test without running it, I'll just provide the methods: -
a) Here we need to check the nearest point: -
public static Point nearestPoint(Point[] points,Point point) {
// definition method having array of type Point and a Point object as parameters
// Here we need to iterate the array to get nearest point to the point given as a parameter
double minDistance = points[0].distance(point); // get initial distance
Point nearest = points[0]; // variable to get nearest one, initialized with first member of array
for(int i = 1; i < points.length;i++) { // for loop to iterate array of points
if(points[i].distance(point) < minDistance) { // check if distance is less update the variables
minDistance = points[i].distance(point);
nearest = points[i];
}
}
return nearest; // returning obtained nearest one
}
b) method to return the points which lies inside circle, we assume here that circle has center(0,0)
public static Point[] internalPoints(Point[] points, double radius) {
// definition method having array of type Point and a Point object as parameters
// Here we need to iterate the array to check which point is internal and add it to new array
Point[] newPoints = new Point[points.length]; // array to hold internal points
double x,y; // x and y points of the Point
for (int i = 0; i < points.length; i++) { // for loop to iterate array of points
//getting x and y of point
x = points[i].getX();
y = points[i].getY();
//checking if the sum of squares is less than or equal to radius
if((x*x + y*y) <= radius) { //if yes then insert it into array
newPoints[i] = points[i];
}
}
return newPoints; // returning array containing internal points.
}
c. main method to call the above two functions: -
public static void main(String[] args) {
// main method for third part to call the above two created methods
// creating point object looking into constructor defined in the question
Point p1 = new Point(4.5,2.5);
Point p2 = new Point(2.3,4);
Point points[] = new Point[2]; // creating array of points
//adding array elements
points[0] = p1;
points[1] = p2;
Point p3 = new Point(3,4);
double radius = 4;
// printing the nearest point
System.out.println("Nearest point to point p3: "+ nearestPoint(points,p3));
Point[] internalPoints = internalPoints(points,radius); // internal points array
}
Note that for these type of questions, we just need to see the class definition given in question and use the methods and structure of the class that is defined.