1 Overview For this lab you will implement the insert method
1 Overview
For this lab, you will implement the insert method and toString method of
a PointList class. The main method creates a PointList object, then prompt
the user for pairs of x and y (integer) values in a continuous (innite) loop.
Each unique pair the user enters should be inserted into the correct position in
the list according to its distance from the origin (0; 0). By constructing the list
one point at a time, we have the opportunity to easily keep it sorted without
having to sort it separately later. This technique is similar to insertion sort. Be
sure to use the appropriate overloaded add method in the ArrayList class. If
the user enters a duplicate point, it should be ignored (think about how can you
tell if it\'s a duplicate). Whenever a new point is added to the list, the contents
of the list should be printed, along with the point\'s distance from the origin.
2 Sample Output
Enter x, y values (type 0 0 to exit): 2 3
(2, 3); distance to origin: 3.61
Enter x, y values (type 0 0 to exit): 4 2
(2, 3); distance to origin: 3.61
(4, 2); distance to origin: 4.47
Enter x, y values (type 0 0 to exit): 2 3
Enter x, y values (type 0 0 to exit): 1 2
(1, 2); distance to origin: 2.24
(2, 3); distance to origin: 3.61
(4, 2); distance to origin: 4.47
Enter x, y values (type 0 0 to exit): 0 0
-------------------------------------------------------------------------------------
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ArrayList {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
PointList lst = new PointList();
while(true) {
System.out.print(\"Enter x, y values (type 0 0 to exit): \");
int x = in.nextInt();
int y = in.nextInt();
if (x == 0 && y == 0) {
break;
}
in.nextLine();
if(lst.insert(x, y)) {
System.out.println(lst);
}
}
in.close();
}
}
class PointList {
private List<Point> points = new ArrayList<>();
boolean insert(int x, int y) {
// TODO
}
@Override
public String toString() {
// TODO
}
}
class Point {
private int x, y;
private static Point zero = new Point(0, 0);
Point(int x, int y) {
this.x = x;
this.y = y;
}
double distance(Point that) {
return Math.sqrt(Math.pow(this.x - that.x, 2) + Math.pow(this.y - that.y, 2));
}
double distanceToOrigin() {
return distance(zero);
}
boolean equals(Point that) {
return this.x == that.x && this.y == that.y;
}
public String toString() { return String.format(\"(%d, %d)\", x, y); }
}
Solution
ArrayList1.java
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ArrayList1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
PointList lst = new PointList();
while(true) {
System.out.print(\"Enter x, y values (type 0 0 to exit): \");
int x = in.nextInt();
int y = in.nextInt();
if (x == 0 && y == 0) {
break;
}
in.nextLine();
if(lst.insert(x, y)) {
System.out.println(lst);
}
}
in.close();
}
}
class PointList {
private List<Point> points = new ArrayList<Point>();
boolean insert(int x, int y) {
// TODO
Point newP = new Point(x,y);
boolean flag = true;
for(Point p: points){
if(p.equals(newP)){
flag = false;
}
}
if(flag)
points.add(newP);
return flag;
}
@Override
public String toString() {
// TODO
String s =\"\";
DecimalFormat df = new DecimalFormat(\"0.00\");
for(Point p: points){
s = s+ p.toString()+\"; distance to origin: \"+df.format(p.distanceToOrigin())+\"\ \";
}
return s;
}
}
class Point {
private int x, y;
private static Point zero = new Point(0, 0);
Point(int x, int y) {
this.x = x;
this.y = y;
}
double distance(Point that) {
return Math.sqrt(Math.pow(this.x - that.x, 2) + Math.pow(this.y - that.y, 2));
}
double distanceToOrigin() {
return distance(zero);
}
boolean equals(Point that) {
return this.x == that.x && this.y == that.y;
}
public String toString() { return String.format(\"(%d, %d)\", x, y); }
}
Output:
Enter x, y values (type 0 0 to exit): 2 3
(2, 3); distance to origin: 3.61
Enter x, y values (type 0 0 to exit): 4 2
(2, 3); distance to origin: 3.61
(4, 2); distance to origin: 4.47
Enter x, y values (type 0 0 to exit): 2 3
Enter x, y values (type 0 0 to exit): 1 2
(2, 3); distance to origin: 3.61
(4, 2); distance to origin: 4.47
(1, 2); distance to origin: 2.24
Enter x, y values (type 0 0 to exit): 0 0



