Answer to Question #46549 in Java | JSP | JSF for Jarrod

Question #46549
Hi experts!

I have a program the i need to write...

i need to 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 and on y-coordinates if x-coordinates are identical.
• Define a class named CompareY that implements Comparator<Point>. Implement the compare method to compare two points on their y-coordinates and on their x-coordinates if y-coordinates are identical.
• Randomly create 100 points and apply the Array.sorts method to display the points in increasing order of their x-coordinates and in increasing order of their y-coordinates, respectively.

it has to have a Minimum of three classes, Array.sorts method, collections, selections and methods...

can anyone help me?
1
Expert's answer
2014-10-01T01:23:16-0400
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
/**
* Created with IntelliJ IDEA.
* User: Melancholia
* Date: 30.09.14
* Time: 8:54
* To change this template use File | Settings | File Templates.
*/
public class Point implements Comparable<Point> {
private int x;
private int y;
public int getX(){
return x;
}
public int getY(){
return y;
}
public Point(int x, int y){
this.x = x;
this.y = y;
}
@Override
public int compareTo(Point point) {
if(x == point.getX()){
if(y == point.getY()){
return 0;
}else if(y > point.getY()){
return 1;
}else if(y < point.getY()){
return -1;
}
}else{
if(x > point.getX()){
return 1;
}else if(x < point.getX()){
return -1;
}
}
return 0;
}
class CompareY implements Comparable<Point>{
@Override
public int compareTo(Point point) {
if(y == point.getY()){
if(x == point.getX()){
return 0;
}else if(x > point.getX()){
return 1;
}else if(x < point.getX()){
return -1;
}
}else{
if(y > point.getY()){
return 1;
}else if(y < point.getY()){
return -1;
}
}
return 0;
}
}
public static void main(String [] args){
List points = new ArrayList();
List points_2_0 = new ArrayList();
Point p1;
Point_2_0 p2;
Random random = new Random();
for(int i = 0; i < 100; i++){
int x = random.nextInt(100);
int y = random.nextInt(100);
points.add(new Point(x, y));
points_2_0.add(new Point_2_0(x, y));
}
Collections.sort(points);
Collections.sort(points_2_0);
System.out.println("X increasing order");
for(int i = 0; i < 100; i++){
p1 = (Point) points.get(i);
System.out.println(p1.getX() + " " + p1.getY());
}
System.out.println("Y increasing order");
for(int i = 0; i < 100; i++){
p2 = (Point_2_0) points_2_0.get(i);
System.out.println(p2.getX() + " " + p2.getY());
}
}
}
class Point_2_0 implements Comparable<Point_2_0>{
private int x;
private int y;
public int getX(){
return x;
}
public int getY(){
return y;
}
public Point_2_0(int x, int y){
this.x = x;
this.y = y;
}
@Override
public int compareTo(Point_2_0 point_2_0) {
if(y == point_2_0.getY()){
if(x == point_2_0.getX()){
return 0;
}else if(x > point_2_0.getX()){
return 1;
}else if(x < point_2_0.getX()){
return -1;
}
}else{
if(y > point_2_0.getY()){
return 1;
}else if(y < point_2_0.getY()){
return -1;
}
}
return 0;
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Assignment Expert
01.10.14, 17:31

Dear Jarrod, You're welcome. We are glad to be helpful. If you liked our service please press like-button beside answer field. Thank you!

Jarrod
01.10.14, 09:52

thank you!!! you are awesome:-)

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS