Free Response Practice Question (with solution)- Distance Array
<-- Back to Sales Array | Home to Additional FRQ Practice--> |
Distance Array
Part 1: Point.java
Write the complete class definition for the Point class. The Point class consists of the following:
- int x: represents the x coordinate of a point.
- int y: represents the y coordinate of a point.
Additionally,
- The Point class contains a default constructor, that initializes a generic point with all value set to 0 (zero).
- The class also consists of a parameterised constructor Point (int x, int y), to initialize the x and y coordinates of a point to the given values.
- Accessor methods for all the properties of the Point class.
Part 2: PointArray.java
Write the complete class definition for the PointArray class. The PointArray class consists of the following:
- pointArr: represents as array of Point objects.
Additionally,
- The PointArray class contains a default constructor that initializes the array pointArr with 5 generic point objects. The values of the point objects are: P1(0,0), P2(1,1), P3(2,2), P4(3,3) and P5(4,4).
- double findMaxDistance(Point p) method that calculates the distance between the point p object and each point object stored in its array. It stores the corresponding distances in another array and then calculates and returns the maximum distance value.
- Point getFarthestPoint() method returns the Point object that is farthest from the point p for which the distance values have been calculated.
Part 3: DistanceRunner.java
Write the complete class definition for the runner class named DistanceRunner class, that makes use of the other two classes. The main method of the DistanceRunner class should do the following:
- Use the generic constructor to initialize an object of PointArray (pointA).
- Read a point object from the user (Point p).
- Use the helper method (findMaxDistance(Point p)), to find and print the maximum distance between point p and the points in the array in the pointA object.
- Use the helper method (getFarthestPoint()), to calculate and print the details of the farthest point.
Note:
- Use the formula given below to calculate the distance between two points on the coordinate plane:
Solution:
Part 1: Point.java
public class Point { int x; int y; public Point() { this.x = 0; this.y = 0; } public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } public String toString() { return "("+this.x+","+this.y+")"; } }
Part 2:PointArray.java
public class PointArray { Point[] pointArr; private Point point; public PointArray() { pointArr = new Point[5]; pointArr[0]= new Point(0,0); pointArr[1]= new Point(1,1); pointArr[2]= new Point(2,2); pointArr[3]= new Point(3,3); pointArr[4]= new Point(4,4); System.out.println("Points in the array are: "); for (int k=0; k < pointArr.length; k++) { System.out.print(pointArr[k]); } } public double findMaxDistance(Point p) { double[] distance = new double[pointArr.length]; double max=0.0; for (int i=0; i< distance.length; i++) { distance[i] = getDistance(p, pointArr[i]); if (max < distance[i]) { max = distance[i]; point = pointArr[i]; } } return max; } private double getDistance(Point a, Point b) { double n1 = (b.getX()-a.getX()) * (b.getX()-a.getX()); double n2 = (b.getY()-a.getY()) * (b.getY()-a.getY()); return Math.sqrt(n1+n2); } public Point getFarthestPoint() { return point; } }
Part 3: DistanceRunner.java
import java.util.InputMismatchException; import java.util.Scanner; public class DistanceRunner { public static void main(String args[]) { PointArray pointA= new PointArray(); try (Scanner input = new Scanner(System.in)) { int x=0, y=0; System.out.println("\nEnter the point from which the distance is to be calculated: "); System.out.print("Enter x coordinate: "); x = input.nextInt(); System.out.print("Enter y coordinate: "); y = input.nextInt(); System.out.println("Max distance is: "+ pointA.findMaxDistance(new Point(x,y))); System.out.println("from point: "+ pointA.getFarthestPoint()); } catch (InputMismatchException e) { System.out.println("Error reading input"); } } }
Java project files (with Runner code):
<-- Back to Sales Array | Home to Additional FRQ Practice--> |