I need to revise a previously written code with a class I just wrote.
My Program:
import java.util.Scanner;
import java.util.Arrays;
public class ModifiedLab7
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("How many points? ");
int n = input.nextInt();
double[][] points = new double[n][2];
for(int i = 0; i < n; i++)
{
System.out.print("Enter the (x, y) coordintes of point " + (i+1) + ": ");
points[i][0] = input.nextDouble();
points[i][1] = input.nextDouble();
}
double[][] distances = new double[n][n];
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
distances[i][j] = distance(points[i], points[j]);
double[] averages = new double[n];
for(int i = 0; i < n; i++)
averages[i] = average( distances[i] );
int min = minimum_location( averages );
System.out.printf("The smallest average is averages[%d] = %f\n", min, averages[min]);
}
public static double distance( double[] p1, double[] p2)
{
return Math.sqrt( (p1[0]-p2[0])*(p1[0]-p2[0]) + (p1[1]-p2[1])*(p1[1]-p2[1]));
}
public static double average( double[] x )
{
double sum = 0;
for(int i = 0; i < x.length; i++)
sum += x[i];
return (x.length > 0)? sum/x.length : 0.0;
}
public static int minimum_location( double[] x )
{
int min = 0;
for(int i = 1; i < x.length; i++)
if( x[i] < x[min] )
min = i;
return min;
}
}
My Class:
class MyPoint
{
//Declare x and y variables
private double X;
private double Y;
//Create MyPoint constructor at point (0,0)
public MyPoint()
{
}
//Create MyPoint constructor with specific coordinates
public MyPoint(double X, double Y)
{
//Declare x and y variables with specific coordinates
this.X = X;
this.Y = Y;
}
//Create distance method from point to another of MyPoint type
public double Distance(MyPoint SecondPoint)
{
return Distance(this, SecondPoint);
}
//Create distance method from point to another point with specific coordinates
public static double Distance(MyPoint PointOne, MyPoint PointTwo)
{
return Math.sqrt((PointOne.X - PointTwo.X) * (PointOne.X - PointTwo.X) + (PointOne.Y - PointTwo.Y) * (PointOne.Y - PointTwo.Y));
}
//Create get x method
public double GetX()
{
return X;
}
//Create get y method
public double GetY()
{
return Y;
}
}
I tested my class already and I know it works. But just have no idea how to revise my program to use my MyPoint class