Access Millions of academic & study documents

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

Content type
User Generated
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
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<Disk>{
private double x;

Sign up to view the full document!

lock_open Sign Up
Showing Page:
2/4
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 1;
else
return 0;
}

Sign up to view the full document!

lock_open Sign Up
Showing Page:
3/4

Sign up to view the full document!

lock_open Sign Up
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.
Studypool
4.7
Indeed
4.5
Sitejabber
4.4