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:


Additionally,


Part 2: PointArray.java

Write the complete class definition for the PointArray class. The PointArray class consists of the following:


Additionally,


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:


Note:


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-->