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

*****JAVA****** /** This program works, but the design is not good. Redesign it

ID: 3677926 • Letter: #

Question

*****JAVA******

/** This program works, but the design is not good. Redesign it */

package javaapplication16;

import java.util.Arrays;

public class Exercise22_04 {
// Each row in points represents a point
private double[][] points;

public static void main(String[] args) {
Point[] points = new Point[100];
for (int i = 0; i < points.length; i++) {
points[i] = new Point(Math.random() * 100, Math.random() * 100);
}

System.out.println("Sort on x-coordinates");
Arrays.sort(points);
for (int i = 0; i < points.length; i++) {
System.out.println(points[i]);
}
  
System.out.println("Sort on y-coordinates");
Arrays.sort(points, new CompareY());
for (int i = 0; i < points.length; i++) {
System.out.println(points[i]);
}
}

/** Define a class for a point with x- and y- coordinates */
static class Point implements Comparable<Point> {
double x;
double y;

Point(double x, double y) {
this.x = x;
this.y = y;
}

@Override
public int compareTo(Point p2) {
if (this.x < p2.x)
return -1;
else if (this.x == p2.x) {
// Secondary order on y-coordinates
if (this.y < p2.y)
return -1;
else if (this.y == p2.y)
return 0;
else
return 1;
} else
return 1;
}
  
@Override
public String toString() {
return "(" + x + ", " + y + ")";
}
}

/**
* A comparator for comparing points on their y-coordinates. If y-coordinates
* are the same, compare their x-coordinator.
*/
static class CompareY implements java.util.Comparator<Point> {
public int compare(Point p1, Point p2) {
if (p1.y < p2.y)
return -1;
else if (p1.y == p2.y) {
// Secondary order on x-coordinates
if (p1.x < p2.x)
return -1;
else if (p1.x == p2.x)
return 0;
else
return 1;
} else
return 1;
}
}
}

Explanation / Answer

package javaapplication16;

import java.util.Arrays;

public class Exercise22_04 {
   // Each row in points represents a point
   private double[][] points;

   public static void main(String[] args) {
       Point[] points = new Point[100];
       for (int i = 0; i < points.length; i++) {
           points[i] = new Point(Math.random() * 100, Math.random() * 100);
       }
       System.out.println("Sort on x-coordinates");
       Arrays.sort(points, new CompareX());
       for (int i = 0; i < points.length; i++) {
           System.out.println(points[i]);
       }

       System.out.println("Sort on y-coordinates");
       Arrays.sort(points, new CompareY());
       for (int i = 0; i < points.length; i++) {
           System.out.println(points[i]);
       }
   }
}

/** Define a class for a point with x- and y- coordinates */
class Point {
   double x;
   double y;

   Point(double x, double y) {
       this.x = x;
       this.y = y;
   }

   @Override
   public String toString() {
       return "(" + x + ", " + y + ")";
   }
}

class CompareX implements java.util.Comparator<Point> {
   public int compare(Point p1, Point p2) {
       if (p1.x < p2.x)
           return -1;
       else if (p1.x == p2.x) {
           // Secondary order on y-coordinates
           if (p1.y < p2.y)
               return -1;
           else if (p1.y == p2.y)
               return 0;
           else
               return 1;
       } else
           return 1;
   }
}

/**
* A comparator for comparing points on their y-coordinates. If y-coordinates
* are the same, compare their x-coordinator.
*/
class CompareY implements java.util.Comparator<Point> {
   public int compare(Point p1, Point p2) {
       if (p1.y < p2.y)
           return -1;
       else if (p1.y == p2.y) {
           // Secondary order on x-coordinates
           if (p1.x < p2.x)
               return -1;
           else if (p1.x == p2.x)
               return 0;
           else
               return 1;
       } else
           return 1;
   }
}

OUTPUT:

Sort on x-coordinates
(0.21010557165790367, 5.29476534112624)
(0.8719861472595292, 84.39709763085288)
(1.7813697524156291, 83.40623549182443)
(3.202888818903682, 18.79748971721743)
(5.024873078546522, 11.785365759356825)
(5.655974874629466, 61.15410958665704)
(6.945883310075962, 80.93366100551265)
(9.118294759080714, 57.30966090511589)
(9.754551959678304, 52.94754849087047)
(9.989252934993065, 75.10885491984091)
(12.488792347373167, 67.70473893043457)
(13.330149517482127, 60.809973785266536)
(14.742238211936687, 43.555602515000366)
(18.561566603115974, 35.80683927198835)
(18.686849231733248, 54.87397385377086)
(21.72845263373444, 12.135737712433482)
(21.93960592260207, 17.64883890753406)
(24.24345553414391, 90.24773756519474)
(24.643231431947132, 26.57474546069708)
(24.716266797791665, 54.92360780525048)
(25.82830365175819, 99.4986420248358)
(26.075837048961258, 77.38591453754044)
(26.435408052211894, 97.6294426372793)
(26.780568047792507, 46.345444369269174)
(28.064316707922508, 94.0798628770722)
(29.027407086748013, 40.220420768435915)
(29.738355726925334, 24.457016206325257)
(29.781573718969078, 82.30817356290567)
(30.765321738532315, 73.69693300540528)
(31.27519936344244, 79.70311308420908)
(31.50979092100481, 42.73945497206162)
(31.771829494474744, 91.05149280618402)
(36.647018488313144, 12.247953702077508)
(36.989086655013345, 75.16705881303493)
(37.68004467763323, 79.69294443640858)
(38.42785176105738, 26.559860850977913)
(40.59123744563456, 26.508258450406473)
(40.837407682949, 20.214600900084022)
(42.14206720184198, 20.836207638089565)
(42.68959683097391, 69.47209721564685)
(43.53288970540048, 36.342725885131365)
(44.47979192496887, 37.273063937705686)
(45.90494637449195, 23.30443532572787)
(46.44781699169965, 50.54672251170127)
(46.58336331939482, 69.52258206750288)
(49.212045213009695, 69.16038242078038)
(49.62197930869273, 3.4698922744801575)
(49.892161517867414, 76.0593578268211)
(50.20265524175505, 99.37160851411886)
(51.84186839705567, 78.32552988767307)
(52.00624111478105, 18.647428774495232)
(52.26126847907787, 89.37686355836792)
(52.761360737755524, 14.403870689010478)
(53.275965651758185, 29.52238989545224)
(53.627347190725025, 82.95152631647153)
(54.22848646541713, 95.57161917559715)
(55.71503386576424, 61.166304253941675)
(57.044827595237415, 9.588485885835285)
(57.117154388002476, 70.42294696037924)
(58.416189757415616, 89.49198883553323)
(60.06120372694913, 59.520573417235624)
(63.30626932349336, 6.518000751307951)
(63.68557213753533, 38.69489039719984)
(63.81085268577984, 74.57226801434214)
(64.69859606314947, 74.04307676752157)
(68.26893234851433, 47.344095517730246)
(70.20074526032076, 53.10105472737633)
(70.68822521581791, 68.7731136441602)
(71.24746089676954, 49.06841663080384)
(71.97301494471078, 63.83984369363122)
(72.217154530117, 10.119967796925234)
(74.20922990096952, 32.37261653171571)
(77.12619792761991, 43.57054835283677)
(77.31116237614158, 42.708184130420044)
(77.65771337606785, 36.997378843742865)
(77.94191703355766, 32.45790966805524)
(78.40010777481042, 65.28741910742582)
(80.48784130495405, 24.019897189675575)
(82.58170025989955, 65.72518510256826)
(83.29386976635885, 23.687491046722485)
(85.58400434382432, 15.887716628388858)
(86.10142852985456, 9.64755741301837)
(86.41284009169543, 11.114696756739272)
(86.69625358563863, 90.5208923471861)
(87.61832122703962, 62.620166338896496)
(88.0850046559385, 92.88158993226986)
(88.14418457806858, 20.86420910946637)
(88.28893315861903, 93.97899189216079)
(88.37028635349708, 71.94206989761031)
(89.15586052620121, 75.50123980363225)
(89.60303835450968, 59.74158697606321)
(91.28281342668151, 13.287976727351335)
(93.15991209243765, 8.059928853470966)
(94.51062548725059, 63.134156859356914)
(95.18098206092392, 10.816115705456008)
(97.72178596551935, 71.43627061232507)
(98.53267182872627, 77.78673155167371)
(99.0206650347876, 61.668616581919814)
(99.40264025534388, 61.946433983088355)
(99.45134685256144, 26.526681495239878)
Sort on y-coordinates
(49.62197930869273, 3.4698922744801575)
(0.21010557165790367, 5.29476534112624)
(63.30626932349336, 6.518000751307951)
(93.15991209243765, 8.059928853470966)
(57.044827595237415, 9.588485885835285)
(86.10142852985456, 9.64755741301837)
(72.217154530117, 10.119967796925234)
(95.18098206092392, 10.816115705456008)
(86.41284009169543, 11.114696756739272)
(5.024873078546522, 11.785365759356825)
(21.72845263373444, 12.135737712433482)
(36.647018488313144, 12.247953702077508)
(91.28281342668151, 13.287976727351335)
(52.761360737755524, 14.403870689010478)
(85.58400434382432, 15.887716628388858)
(21.93960592260207, 17.64883890753406)
(52.00624111478105, 18.647428774495232)
(3.202888818903682, 18.79748971721743)
(40.837407682949, 20.214600900084022)
(42.14206720184198, 20.836207638089565)
(88.14418457806858, 20.86420910946637)
(45.90494637449195, 23.30443532572787)
(83.29386976635885, 23.687491046722485)
(80.48784130495405, 24.019897189675575)
(29.738355726925334, 24.457016206325257)
(40.59123744563456, 26.508258450406473)
(99.45134685256144, 26.526681495239878)
(38.42785176105738, 26.559860850977913)
(24.643231431947132, 26.57474546069708)
(53.275965651758185, 29.52238989545224)
(74.20922990096952, 32.37261653171571)
(77.94191703355766, 32.45790966805524)
(18.561566603115974, 35.80683927198835)
(43.53288970540048, 36.342725885131365)
(77.65771337606785, 36.997378843742865)
(44.47979192496887, 37.273063937705686)
(63.68557213753533, 38.69489039719984)
(29.027407086748013, 40.220420768435915)
(77.31116237614158, 42.708184130420044)
(31.50979092100481, 42.73945497206162)
(14.742238211936687, 43.555602515000366)
(77.12619792761991, 43.57054835283677)
(26.780568047792507, 46.345444369269174)
(68.26893234851433, 47.344095517730246)
(71.24746089676954, 49.06841663080384)
(46.44781699169965, 50.54672251170127)
(9.754551959678304, 52.94754849087047)
(70.20074526032076, 53.10105472737633)
(18.686849231733248, 54.87397385377086)
(24.716266797791665, 54.92360780525048)
(9.118294759080714, 57.30966090511589)
(60.06120372694913, 59.520573417235624)
(89.60303835450968, 59.74158697606321)
(13.330149517482127, 60.809973785266536)
(5.655974874629466, 61.15410958665704)
(55.71503386576424, 61.166304253941675)
(99.0206650347876, 61.668616581919814)
(99.40264025534388, 61.946433983088355)
(87.61832122703962, 62.620166338896496)
(94.51062548725059, 63.134156859356914)
(71.97301494471078, 63.83984369363122)
(78.40010777481042, 65.28741910742582)
(82.58170025989955, 65.72518510256826)
(12.488792347373167, 67.70473893043457)
(70.68822521581791, 68.7731136441602)
(49.212045213009695, 69.16038242078038)
(42.68959683097391, 69.47209721564685)
(46.58336331939482, 69.52258206750288)
(57.117154388002476, 70.42294696037924)
(97.72178596551935, 71.43627061232507)
(88.37028635349708, 71.94206989761031)
(30.765321738532315, 73.69693300540528)
(64.69859606314947, 74.04307676752157)
(63.81085268577984, 74.57226801434214)
(9.989252934993065, 75.10885491984091)
(36.989086655013345, 75.16705881303493)
(89.15586052620121, 75.50123980363225)
(49.892161517867414, 76.0593578268211)
(26.075837048961258, 77.38591453754044)
(98.53267182872627, 77.78673155167371)
(51.84186839705567, 78.32552988767307)
(37.68004467763323, 79.69294443640858)
(31.27519936344244, 79.70311308420908)
(6.945883310075962, 80.93366100551265)
(29.781573718969078, 82.30817356290567)
(53.627347190725025, 82.95152631647153)
(1.7813697524156291, 83.40623549182443)
(0.8719861472595292, 84.39709763085288)
(52.26126847907787, 89.37686355836792)
(58.416189757415616, 89.49198883553323)
(24.24345553414391, 90.24773756519474)
(86.69625358563863, 90.5208923471861)
(31.771829494474744, 91.05149280618402)
(88.0850046559385, 92.88158993226986)
(88.28893315861903, 93.97899189216079)
(28.064316707922508, 94.0798628770722)
(54.22848646541713, 95.57161917559715)
(26.435408052211894, 97.6294426372793)
(50.20265524175505, 99.37160851411886)
(25.82830365175819, 99.4986420248358)