Creates an arraylist of point objects initially empty then


Use a Point class with the distanceToOrigin method that returns the distance to the origin point (0; 0). The main method defined below (template) class creates an ArrayList of Point objects (initially empty), then prompt the user for pairs of x and y (integer) values in a continuous (innite) loop. The main method calls an insert method to insert each unique pair the user enters 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.

What to implement

You will implement the method distanceToOrigin and insert in the provided code template.

3 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

----------------------------------------------------------------------------------------------------------------------------------------------

package lab7;


import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;


public class Lab7 {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

List lst = new ArrayList<>();

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(insert(new Point(x, y), lst)) {

System.out.println(print(lst));

}


}

in.close();

}

static String print(List lst) {

String ret = "";

for(Point p : lst) {

ret = ret + String.format("%s; distance to origin: %2.2fn", p, p.distanceToOrigin());

}

return ret;

}

static boolean insert(Point point, List lst) {

// TODO:

// insert "point" into "lst" so that "lst" remains sorted in increasing order by "distance to orgin"

// return true if "point" is not already in "lst"

// return false otherwise

}

}


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() {

// TODO: return distance to (0, 0)

}

@Override

public boolean equals(Object that) {

boolean ret = false;

if(that instanceof Point) {

Point thatPoint = (Point) that;

ret = x == thatPoint.x && y == thatPoint.y;

}

return ret;

}

@Override

public String toString() { return String.format("(%d, %d)", x, y); }

}

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Creates an arraylist of point objects initially empty then
Reference No:- TGS02709893

Now Priced at $10 (50% Discount)

Recommended (91%)

Rated (4.3/5)