Thank you!
import java.util.*;
public class FinalExam {
public static void main(String[] args) {
FinalExam fe = new FinalExam();
Point[] parray = new Point[8];// there are only 8 Points
fe.inputPoints(parray);
System.out.println("All the Points in this plain");
for (Point p : parray) {
System.out.println(p);
}
Point[] pair = fe.shortestDistance(parray);
System.out.println("Points with shortest distance between them");
System.out.println(pair[0] + " and " + pair[1]);
System.out.println("Point Closest to zero is : "
+ fe.closestToZero(parray));
}
public void inputPoints(Point[] parray) {
Scanner input = new Scanner(System.in);
System.out.println("Enter 8 points " + "\n");
int points = input.nextInt();
parray = new Point[points];
}
public Point[] shortestDistance(Point[] parray) {
Point[] row = new Point[2];
[color="#00BFFF"]double shortestD = distance(parray[0][0], parray[0][1], parray[1][0],
parray[1][1]);[/color]
for (int i = 0; i < parray.length; i++) {
for (int j = i + 1; j < parray.length; j++) {
[color="#00BFFF"]double distance = distance(parray[i][0], parray[i][1],
parray[j][0], parray[j][1]); [/color]
if (distance < shortestD) {
row[0] = parray[i];
row[1] = parray[j];
shortestD = distance;
}
}
}
return row;
}
public Point closestToZero(Point[] parray) {
Point ctz = new Point();
for (int i = 0; i < parray.length; i++) {
[color="#00BFFF"] ctz = Math.sqrt(Math.pow(parray[i].getX(), 2) + Math.pow(parray[i].getY(), 2));[/color]
}
return ctz;
}
class Point {
double x = 0;
double y = 0;
public Point() {
}
public Point(double x, double y) {
}
public double distance(Point p) {
Point[] pair = new Point[2];
double shortestD = 0;
for (int i = 0; i < pair.length; i++) {
for (int j = i + 1; j < pair.length; j++) {
double distance = pair[i].distance(pair[j]);
shortestD = Math.sqrt(Math.pow((this.x - p.x), 2) + Math.pow((this.y - p.y), 2));
if (distance < shortestD) {
pair[0] = pair[i];
pair[1] = pair[j];
shortestD = distance;
return shortestD;
}
}
}
return shortestD;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
@Override
public String toString() {
System.out.println();
return super.toString();
}
}
}
This post has been edited by smohd: 05 August 2012 - 01:40 PM
Reason for edit:: Code tags added. Please use [code] tags when posting codes

New Topic/Question
Reply


MultiQuote


|