package lab5 import javaioPrintStream import javautilScanner

package lab5;

import java.io.PrintStream;
import java.util.Scanner;

public class Lab5 {
   public static void main(String[] args) {
       Scanner in = new Scanner(System.in);
       PrintStream out = System.out;

       out.print(\"Enter number of points: \");
       int n = in.nextInt();
       Point[] points = new Point[n];
      
       in.nextLine(); // clear input buffer

       for(int i=0; i<n; i++) {
           out.print(\"Enter x and y values: \");
           String[] xy = in.nextLine().split(\" \");
           int x = Integer.parseInt(xy[0]);
           int y = Integer.parseInt(xy[1]);
           points[i] = new Point(x, y);
       }
      
       largestDistance(points);
      
       in.close();
   }
  
   // Print all points and mark two points with the longest distance with *
   static void largestDistance(Point[] points) {
       Line longestLine = new LineCollection(points).longestLine();
      
       for(int i=0; i < points.length; i++) {
           Point point = points[i];
           System.out.printf((longestLine.contains(point) ? \"*\" : \" \") + \"%s\ \", point);
       }
   }
}

// A collection of lines
class LineCollection {
   private Line[] lines;
  
   // Create a collection of lines by pairing up points in a point array
   // Do not include lines from a point to itself
   // Do not include both a line and its opposite (e.g. (p1, p2) and (p2, p1) are opposite of each other. Only keep one)
   LineCollection(Point[] points) {
       // TODO
   }
  
   // Save the parameter lines in the field lines
   LineCollection(Line[] lines) {
       // TODO
   }
  
   // Return the line with the longest distance in this collection
   Line longestLine() {
       // TODO
   }
}

class Line {
   private final Point start, end;
   private double length;
  
   // A line consists of two points: start and end
   // Compute and save the distance between the two points in the field \"length\"
   Line(Point start, Point end) {
       // TODO
   }
  
   // Return length
   double length() {
       // TODO
   }

   // Return true if and only if p is either the start or the end point of this line
   boolean contains(Point p) {
       // TODO
   }
  
   // Return string representation of a line: e.g. ((1, 2), (2, 3)) where (1, 2) is the start and (2, 3) is the end point
   public String toString() {
       // TODO
   }
}

class Point {
   private final int x, y;

   Point(int x, int y) {
       // TODO
   }

   // Return string represention of a point: e.g. (1, 2) is a point with x = 1 and y = 2
   public String toString() {
       // TODO
   }

   // Return the Euclidean distance between this point and \"that\" point.
   // Hint: Math class has sqrt method
   double distance(Point that) {
       // TODO
   }
  
   // Return true if and only if \"that\" is a Point object and
   // the x, y coordinates of this point is the same as the x, y coordinates of \"that\".
   public boolean equals(Object that) {
       // TODO
   }
}

1 Overview For this lab, you will implement a class Point which represents a two-dimensional point in the Cartesian plane, a class Line with a start and end point, and a class LineCollection that stores an array of points and it can find the line with the largest distance The provided main method prompts the user for an integer n, and then prompt the user for n pairs of r and y (integer) values. It stores these values as Point instances in an array for processing later. The provided largestDistance method takes an array of point objects and print the points in the order that the user entered them using the object\'s toString method. It also marks the two points that have the largest distance with a star Distance The distance between points (Ti, y/) and (22, y2) is (x2 - z1)2 + (y2 - yi)2 2 What to implement? Please read the template code to see what is required to implement. You may assume that the points are all unique Sample output Enter number of points:5 Enter x and y values:1 2 Enter x and y values:2 3 Enter x and y values:3 4 Enter x and y values 1 3 Enter x and y values:2 4 (2, 3) (2, 4)

Solution

I provided the codes ffor classes Line.java and Point.java.

Point.java:

package lab5;

public class Point
{
    private final int x, y;

   Point(int x, int y)
   {
    this.x=x;
    this.y=y;
   }

   // Return string represention of a point: e.g. (1, 2) is a point with x = 1 and y = 2
    public String toString()
    {
        return \"x=\"+x+\" and y=\"+y;
    }

   // Return the Euclidean distance between this point and \"that\" point.
    // Hint: Math class has sqrt method

    double distance(Point that) {
       double dx = this.x - that.x;
          double dy = this.y - that.y;
          return Math.sqrt(dx*dx + dy*dy);
    }
  
    // Return true if and only if \"that\" is a Point object and
    // the x, y coordinates of this point is the same as the x, y coordinates of \"that\".

    public boolean equals(Object that)
    {
    if ((x == ((Point) that).x && y == ((Point) that).y))
    {
            return true;
        }
    return false;
    }
}

Line.java:

package lab5;

public class Line
{
    private final Point start, end;
    private double length;
  
    // A line consists of two points: start and end
    // Compute and save the distance between the two points in the field \"length\"
    Line(Point start, Point end)
    {
     this.start=start;
     this.end=end;

    }
  
    // Return length
    double length()
    {
        return length;
    }

   // Return true if and only if p is either the start or the end point of this line
    boolean contains(Point p)
    {
    if(p==start || p==end)
      return true;
    else
      return false;
      
    }
  
    // Return string representation of a line: e.g. ((1, 2), (2, 3)) where (1, 2) is the start and (2, 3) is the end point
    public String toString()
    {
        return \"( \"+start+ \" ,\"+end+\" )\";
    }
}

package lab5; import java.io.PrintStream; import java.util.Scanner; public class Lab5 { public static void main(String[] args) { Scanner in = new Scanner(System
package lab5; import java.io.PrintStream; import java.util.Scanner; public class Lab5 { public static void main(String[] args) { Scanner in = new Scanner(System
package lab5; import java.io.PrintStream; import java.util.Scanner; public class Lab5 { public static void main(String[] args) { Scanner in = new Scanner(System
package lab5; import java.io.PrintStream; import java.util.Scanner; public class Lab5 { public static void main(String[] args) { Scanner in = new Scanner(System

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site