Template package lab5 import javaioPrintStream import javaut
Template
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
}
}
Lab 5 October 7, 2016 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 (r1, yi and (2, ) is 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:23 Enter x and y values:3 4 Enter x and y values:13 Enter x and y values:2 4 (2, 3) (3, 4) (2, 4)Solution
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) {
int k=0;
for(int i=0;i<points.length;i++)
{
for(int j=0;j<points.length;j++)
{
if(i!=j)
{
lines[k++]=new Line(points[i],points[j]);
}
}
}
}
// Save the parameter lines in the field lines
LineCollection(Line[] lines) {
// TODO
this.lines=lines;
}
// Return the line with the longest distance in this collection
Line longestLine() {
// TODO
double max=lines[0].length();
int L=0;
for(int i=1;i<lines.length;i++)
{
if(lines[i].length()>max)
{
max=lines[i].length();
L=i;
}
}
return lines[L];
}
}
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;
length=start.distance(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.equals(start) || p.equals(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.toString()+\",\"+end.toString()+\")\";
}
}
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+\",\"+y+\")\";
}
// Return the Euclidean distance between this point and \"that\" point.
// Hint: Math class has sqrt method
double distance(Point that) {
return Math.sqrt(Math.pow((that.x-this.x),2)+Math.pow((that.y-this.y),2));
}
// 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(Point that) {
if(that.x==this.x && that.y ==this.y)
return true;
else
return false;
}
}
![Template package lab5; import java.io.PrintStream; import java.util.Scanner; public class Lab5 { public static void main(String[] args) { Scanner in = new Scann Template package lab5; import java.io.PrintStream; import java.util.Scanner; public class Lab5 { public static void main(String[] args) { Scanner in = new Scann](/WebImages/31/template-package-lab5-import-javaioprintstream-import-javaut-1088662-1761572846-0.webp)
![Template package lab5; import java.io.PrintStream; import java.util.Scanner; public class Lab5 { public static void main(String[] args) { Scanner in = new Scann Template package lab5; import java.io.PrintStream; import java.util.Scanner; public class Lab5 { public static void main(String[] args) { Scanner in = new Scann](/WebImages/31/template-package-lab5-import-javaioprintstream-import-javaut-1088662-1761572846-1.webp)
![Template package lab5; import java.io.PrintStream; import java.util.Scanner; public class Lab5 { public static void main(String[] args) { Scanner in = new Scann Template package lab5; import java.io.PrintStream; import java.util.Scanner; public class Lab5 { public static void main(String[] args) { Scanner in = new Scann](/WebImages/31/template-package-lab5-import-javaioprintstream-import-javaut-1088662-1761572846-2.webp)
![Template package lab5; import java.io.PrintStream; import java.util.Scanner; public class Lab5 { public static void main(String[] args) { Scanner in = new Scann Template package lab5; import java.io.PrintStream; import java.util.Scanner; public class Lab5 { public static void main(String[] args) { Scanner in = new Scann](/WebImages/31/template-package-lab5-import-javaioprintstream-import-javaut-1088662-1761572846-3.webp)
![Template package lab5; import java.io.PrintStream; import java.util.Scanner; public class Lab5 { public static void main(String[] args) { Scanner in = new Scann Template package lab5; import java.io.PrintStream; import java.util.Scanner; public class Lab5 { public static void main(String[] args) { Scanner in = new Scann](/WebImages/31/template-package-lab5-import-javaioprintstream-import-javaut-1088662-1761572846-4.webp)
![Template package lab5; import java.io.PrintStream; import java.util.Scanner; public class Lab5 { public static void main(String[] args) { Scanner in = new Scann Template package lab5; import java.io.PrintStream; import java.util.Scanner; public class Lab5 { public static void main(String[] args) { Scanner in = new Scann](/WebImages/31/template-package-lab5-import-javaioprintstream-import-javaut-1088662-1761572846-5.webp)