Q:

(Sort points in a plane) Write a program that meets the following requirements:

0

(Sort points in a plane) Write a program that meets the following requirements:

Define a class named Point with two data fields x and y to represent a point’s x- and y-coordinates. Implement the Comparable interface for comparing the points on x-coordinates. If two points have the same x-coordinates, compare their y-coordinates.

Define a class named CompareY that implements Comparator<point>. Implement the compare method to compare two points on their y-coordinates. If two points have the same y-coordinates, compare their x-coordinates.

Randomly create 100 points and apply the Arrays.sort method to display the points in increasing order of their x-coordinates and in increasing order of their y-coordinates, respectively.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import java.util.Comparator;

public class CompareY implements Comparator<Point> {

	/** Method compares two points on their y-coordinates */
	public int compare(Point p1, Point p2) {
		double x1 = p1.getX();
		double y1 = p1.getY();
		double x2 = p2.getX();
		double y2 = p2.getY();

		if (y1 == y2) {
			// If two points have the same y-coordinates,
			// compare their x-coordinates.
			if (x1 < x2)
				return -1;
			else if (x1 == x2)
				return 0;
			else
				return 1;
		}
		else if (y1 < y2)
			return -1;
		else 
			return 1;

	}
}

Exercise_20_04.java

/**********************************************************************************
* (Sort points in a plane) Write a program that meets the following requirements: *
*                                                                                 *
* â–  Define a class named Point with two data fields x and y to represent a        *
*   point’s x- and y-coordinates. Implement the Comparable interface for          *
* 	 comparing the points on x-coordinates. If two points have the same            *
*   x-coordinates, compare their y-coordinates.                                   *
*                                                                                 *
* â–  Define a class named CompareY that implements Comparator<Point>. Implement    *
*   the compare method to compare two points on their y-coordinates. If two       *
* 	 points have the same y-coordinates, compare their x-coordinates.              *
*                                                                                 *
* â–  Randomly create 100 points and apply the Arrays.sort method to display the    *
*   points in increasing order of their x-coordinates and in increasing order of  *
*   their y-coordinates, respectively.                                            *
**********************************************************************************/
import java.util.*;
public class Exercise_20_04 {
	public static void main(String[] args) {
		// Randomly create 100 points
		Point[] points = new Point[100];
		for (int i = 0; i < points.length; i++) {
			points[i] = new Point((double)(Math.random() * 5),
				(double)(Math.random() * 5));
		}

		// Display the points in increasing order of their x-coordinates
		Arrays.sort(points);
		List<Point> list1 = Arrays.asList(points);
		System.out.println("\nPoints in increasing order of their x-coordinates:");
		System.out.println(list1);
		
		
		// Display the points in increasing order of their y-coordinates
		Arrays.sort(points, new CompareY());
		List<Point> list2 = Arrays.asList(points);
		System.out.println("\nPoints in increasing order of their y-coordinates:");
		System.out.println(list2);
	}
}

Point.java

public class Point implements Comparable<Point> {
	private double x;
	private double y;

	Point() {}

	// Construct a Point of x, y
	Point(double x, double y) {
		this.x = x;
		this.y = y;
	}

	/** Set x to specified value */
	public void setX(double x) {
		this.x = x;
	}

	/** Set y to specified value */
	public void setY(double y) {
		this.y = y;
	}

	/** Return x */
	public double getX() {
		return x;
	}

	/** Return y */
	public double getY() {
		return y;
	}

	@Override // Override the compareTo method in the Comparable class
	public int compareTo(Point point) {
		if (x == point.getX()) {
			// If two points have the same x-coordinates,
			// compare their y-coordinates.
			if (y > point.getY())
				return 1;
			else if (y < point.getY())
				return -1;
			else
				return 0;
		}
		else if (x > point.getX())
			return 1;
		else
			return -1;
	}

	@Override // Override the toString method in the Object class
	public String toString() {
		return "(" + String.format("%.2f", x) + ", " 
			+ String.format("%.2f", y) + ")";
	}
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now