Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using the PointList class, write a Java program which will allow the user to ent

ID: 3754952 • Letter: U

Question

Using the PointList class, write a Java program which will allow the user to enter a set of points from the 2D Cartesian plane and find out certain things about this set of points. The user gets to choose how many points they want to enter during any given run of the program as long as they enter 3 or more points. The program should store the points entered by the user in a PointList object.

After the points are entered by the user, the user gets to choose from the following menu items. The functionality for the menu items should be implemented as methods in your PointList class:

1. Determine if 3 points can create a triangle and the type of triangle it would be.

2. Determine the point which is the farthest outlier to the other points.

3. Find the upper left and lower right points of the smallest rectangle that can contain all the points

4. Exit

Here are some additional implementation details for the 3 menu items:

1. The user should be able to pick the 3 points they want to use. If you determine a triangle can be made, tell the user that and output whether the triangle is acute, right, or obtuse. If a triangle can't be formed, tell the user it can't be formed.

2. The farthest outlier can be determined by the largest total distance to all other points.

3. The points can be on the edge of the rectangle you find.

Here is the PointList class. In the question, what I was referring to was just meant to be the class separate from the main class that has the methods listed above in it to be called in the main class through a menu, but I'll post it anyway in case it has any info that would make answering the question easier:

Explanation / Answer

You can discuss more with me in the comments, should you have any query.

import java.util.Scanner;

public class PointList {

   //data member
   double points[][];

   //constructor
   public PointList(int len){
       points = new double[len][2];

       Scanner input = new Scanner(System.in);
       for (int i = 0; i < len; i++) {
           System.out.println("Enter the coordinates of a point:");
           System.out.println("Enter the x value: ");
           points[i][0] = input.nextDouble();
           System.out.println("Enter the y value: ");
           points[i][1] = input.nextDouble();
       }
   } //close constructor

   //method to find distance between two points
   public double distance(double x1, double y1, double x2, double y2){
       return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
   }

   //find the pair of points that has the smallest distance between them
   public void findClosestPair(){
       int p1 = 0, p2 = 1;

       double shortDist = distance(points[p1][0], points[p1][1], points[p2][0], points[p2][1]);

       for (int i = 0; i < points.length; i++) {
           for (int j = i+1; j < points.length; j++) {
               double dist = distance(points[i][0], points[i][1], points[j][0], points[j][1]);
               if(shortDist>dist){
                   p1=i;
                   p2=j;
                   shortDist = dist;
               }
           }
       }

       System.out.println("The closest two points are ("+
               points[p1][0]+", "+points[p1][1]+") and (" +
               points[p2][0]+", "+points[p2][1]+")");
   }

   public String typeOfTriangle(double[] p1, double[] p2, double[] p3) {
      
       double x1 = p1[0];
       double x2 = p2[0];
       double x3 = p3[0];
      
       double y1 = p1[1];
       double y2 = p2[1];
       double y3 = p3[1];

       //Calculate change in x for distance formula
       double delta_x1 = (x2-x1)*(x2-x1);
       double delta_x2 = (x3-x2)*(x3-x2);
       double delta_x3 = (x3-x1)*(x3-x1);

       //Calculate change in y for distance formula
       double delta_y1 = (y2-y1)*(y2-y1);
       double delta_y2 = (y3-y2)*(y3-y2);
       double delta_y3 = (y3-y1)*(y3-y1);
      
       //Calculate distance foe each of the three sides if not CO-LINEAR
       double lengthSide1 = Math.sqrt(delta_x1 + delta_y1);
      
       double lengthSide2 = Math.sqrt(delta_x2 + delta_y2);
      
       double lengthSide3 = Math.sqrt(delta_x3 + delta_y3);
      

       if ((lengthSide1*lengthSide1 + lengthSide2*lengthSide2 == lengthSide3*lengthSide3) ||
               (lengthSide1*lengthSide1 + lengthSide3*lengthSide3 == lengthSide2*lengthSide2) ||
               (lengthSide2*lengthSide2 + lengthSide3*lengthSide3 == lengthSide1*lengthSide1)) {
           return "Right Triangle!";
       }
       else if ((lengthSide1*lengthSide1 + lengthSide2*lengthSide2 > lengthSide3*lengthSide3) ||
               (lengthSide1*lengthSide1 + lengthSide3*lengthSide3 > lengthSide2*lengthSide2) ||
               (lengthSide2*lengthSide2 + lengthSide3*lengthSide3 > lengthSide1*lengthSide1)) {
           return "Acute Triangle!";
       }
       else if ((x2-x1)*(y3-y2)==(y2-y1)*(x3-x2)) {
           return "Can't form a triangle";
       }
       else {
           return "Obtuse Triangle!";
       }

   }
  
   public void outlier(int numberOfPoints) {
       // can be determined by the largest distance to all other points
   }
  
   public void leftRightPoints() {
       // edge of the rectangle
   }
}

import java.util.Scanner;

public class MainClass {

   public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
       System.out.println("How many points do you want to compare?");
       int size = input.nextInt();

       PointList p1 = new PointList(size);

       //double x1 = p1.points[0][0];
       //double y1 = p1.points[0][1];
       //double x2 = p1.points[1][0];
       //double y2 = p1.points[1][1];

       //System.out.println("The distance between the first two points is "+p1.distance(x1,y1,x2,y2));

       //p1.findClosestPair();
       System.out.println("Enter the number corresponding to the function to choose a function:");
       System.out.println("1.If 3 points can form a triangle");
       System.out.println("2.Farthest outlier");
       System.out.println("3.Upper left and lower right points of the smallest triangle");
       System.out.println("4.Exit");
      
       int menuInput = input.nextInt();
      
       if(menuInput==1) {
           System.out.println("Choose first point of the triangle by telling the point number, e.g. 3. This will take the 3rd point that you entered earlier ");
           int point1 = input.nextInt();
          
           System.out.println("Choose second point by telling the point number");
           int point2 = input.nextInt();
          
           System.out.println("Choose third point by telling the point number");
           int point3 = input.nextInt();
          
           System.out.println(p1.typeOfTriangle(p1.points[point1-1],p1.points[point2-1],p1.points[point3-1]));
          
       }
      
       if(menuInput==2) {
           p1.outlier(size);
       }
      
       if(menuInput==3) {
           p1.leftRightPoints();
       }
      
       if(menuInput==4) {
           System.out.println("Exited");
       }
      
      
      
      
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote