# 6 (15 pnts) Write a Java class Disk that represents a 2D disk cente

Content type
User Generated
Rating
Showing Page:
1/4
6. (15 pnts) Write a Java class Disk that represents a 2D
disk centered at a point with coordinates and and with
should provide a constructor that takes three parameters: x
coordinate of the center, y coordinate of the center and the
disk. The class should implement interface. The disks
should be compared based on their area. The area of a
disk can be
computed using
area =pi * r2.
Your class should implement all methods needed for the
following code to compile and run successfully:
Random rand = new Random();
Disk [] myDisks = new Disk[10];
for(int i =0; i< myDisks.length; i++)
myDisk[i] = new Disk(rand.nextDouble(),
rand.nextDouble(), rand.nextDouble);
Array.sort(myDisks);
Solution
import java.util.Arrays;
import java.util.Random;
public class Disk implements Comparable<Disk>{
private double x;

Showing Page:
2/4
private double y;
/**
* @param x
* @param y
*/
public Disk(double x, double y, double radius) {
this.x = x;
this.y = y;
}
public double area(){
}
@Override
public int compareTo(Disk o) {
if(this.area() < o.area())
return -1;
else if(this.area() > o.area())
return 1;
else
return 0;
}

Showing Page:
3/4

End of Preview - Want to read all 4 pages?
Access Now
Unformatted Attachment Preview
6. (15 pnts) Write a Java class Disk that represents a 2D disk centered at a point with coordinates and and with radius . The class should provide a constructor that takes three parameters: x coordinate of the center, y coordinate of the center and the radius of the disk. The class should implement interface. The disks should be compared based on their area. The area of a disk can be computed using area =pi * r2. Your class should implement all methods needed for the following code to compile and run successfully: Random rand = new Random(); Disk [] myDisks = new Disk[10]; for(int i =0; i< myDisks.length; i++) myDisk[i] = new Disk(rand.nextDouble(), rand.nextDouble(), rand.nextDouble); Array.sort(myDisks); Solution import java.util.Arrays; import java.util.Random; public class Disk implements Comparable{ private double x; private double y; private double radius; /** * @param x * @param y * @param radius */ public Disk(double x, double y, double radius) { this.x = x; this.y = y; this.radius = radius; } public double area(){ return Math.PI*radius*radius; } @Override public int compareTo(Disk o) { if(this.area() < o.area()) return -1; else if(this.area() > o.area()) return ...
Purchase document to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

### Review

Anonymous
I was having a hard time with this subject, and this was a great help.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4