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
}
}
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](/WebImages/15/package-lab5-import-javaioprintstream-import-javautilscanner-1025799-1761531093-0.webp)
![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](/WebImages/15/package-lab5-import-javaioprintstream-import-javautilscanner-1025799-1761531093-1.webp)
![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](/WebImages/15/package-lab5-import-javaioprintstream-import-javautilscanner-1025799-1761531093-2.webp)
![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](/WebImages/15/package-lab5-import-javaioprintstream-import-javautilscanner-1025799-1761531093-3.webp)