package lab7 import javautilArrayList import javautilList im

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);
       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); }
}

l 0verview 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 r and y (integer) values in a continuous (infinite) 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 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

Solution

solution

package com.p2p.test;

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); }
}

package com.p2p.test;

import java.util.List;
import java.util.ArrayList;

class PointList {
private List<Point> points = new ArrayList<>();
  
boolean insert(int x, int y) {
   points.add(new Point(x,y));
       return true;
// TODO
}

   @Override
   public String toString() {
       return \"(\" + points + \")\";
   }
  
  
}

package com.p2p.test;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Sample {
   static Point p=null;
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);
}
p=new Point(x,y);
double distance=p.distanceToOrigin();
System.out.println(distance);
}
  
in.close();
}
}

output

Enter x, y values (type 0 0 to exit): 2 3
PointList [points=[(2, 3)]]
3.605551275463989
Enter x, y values (type 0 0 to exit): 4 2
PointList [points=[(2, 3), (4, 2)]]
4.47213595499958
Enter x, y values (type 0 0 to exit): 2 3
PointList [points=[(2, 3), (4, 2), (2, 3)]]
3.605551275463989
Enter x, y values (type 0 0 to exit): 1 2
PointList [points=[(2, 3), (4, 2), (2, 3), (1, 2)]]
2.23606797749979
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
package lab7; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Lab7 { public static void main(String[] args) { Scanner
package lab7; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Lab7 { public static void main(String[] args) { Scanner

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site