Chapter 10 Exercise 11:
10.11 (Geometry: the Circle2D class) Define the Circle2D class that contains:
■ Two double data fields named x and y that specify the center of the circle with getter methods.
■ A data field radius with a getter method.
■ A no-arg constructor that creates a default circle with (0, 0) for (x, y) and 1for radius.
■ A constructor that creates a circle with the specified x, y, and radius.
■ A method getArea() that returns the area of the circle.
■ A method getPerimeter() that returns the perimeter of the circle.
■ A method contains(double x, double y) that returns true if the specified point (x, y) is inside
this circle (see Figure 10.21a).
■ A method contains(Circle2D circle) that returns true if the specified circle is inside
this circle (see Figure 10.21b).
■ A method overlaps(Circle2D circle) that returns true if the specified circle
overlaps with this circle (see Figure 10.21c).
Write a test program that creates a Circle2D object c1 (new Circle2D(2, 2, 5.5)),
displays its area and perimeter, and displays the result of c1.contains(3, 3),
c1.contains(new Circle2D(4, 5, 10.5)), and c1.overlaps(new Circle2D(3, 5, 2.3)).
10.11 (Geometry: the Circle2D class) Define the Circle2D class that contains:
■ Two double data fields named x and y that specify the center of the circle with getter methods.
■ A data field radius with a getter method.
■ A no-arg constructor that creates a default circle with (0, 0) for (x, y) and 1for radius.
■ A constructor that creates a circle with the specified x, y, and radius.
■ A method getArea() that returns the area of the circle.
■ A method getPerimeter() that returns the perimeter of the circle.
■ A method contains(double x, double y) that returns true if the specified point (x, y) is inside
this circle (see Figure 10.21a).
■ A method contains(Circle2D circle) that returns true if the specified circle is inside
this circle (see Figure 10.21b).
■ A method overlaps(Circle2D circle) that returns true if the specified circle
overlaps with this circle (see Figure 10.21c).
Write a test program that creates a Circle2D object c1 (new Circle2D(2, 2, 5.5)),
displays its area and perimeter, and displays the result of c1.contains(3, 3),
c1.contains(new Circle2D(4, 5, 10.5)), and c1.overlaps(new Circle2D(3, 5, 2.3)).
import javafx.scene.shape.Circle; class Circle2D extends GeometricObject { private double x; private double y; private double radius; public Circle2D(Circle c) { this(c.getCenterX(), c.getCenterY(), c.getRadius()); } public Circle2D(double x, double y, double radius) { this.x = x; this.y = y; this.radius = radius; } public Circle2D() { this(0, 0, 1); } public double getX() { return x; } public void setX(double x) { this.x = x; } public double getY() { return y; } public void setY(double y) { this.y = y; } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } @Override public double getArea() { return radius * radius * Math.PI; } @Override public double getPerimeter() { return 2 * radius * Math.PI; } public boolean contains(Circle2D circle2D) { double distance = getPoint().distance(circle2D.x, circle2D.y); if (distance <= Math.abs(this.radius - circle2D.radius)) { return true; } else { return false; } } public static boolean c1ContainsC2(Circle c1, Circle c2) { Circle2D cir1 = new Circle2D(c1); Circle2D cir2 =new Circle2D(c2); return cir1.contains(cir2); } public static boolean c1OverlapsC2(Circle c1, Circle c2) { Circle2D cir1 = new Circle2D(c1); Circle2D cir2 =new Circle2D(c2); if (cir1.contains(cir2) || cir2.contains(cir1)) return false; return cir1.overlaps(cir2); } public boolean overlaps(Circle2D circle2D) { double distance = getPoint().distance(circle2D.x, circle2D.y); if (distance <= this.radius + circle2D.radius) return true; else return false; } private MyPoint getPoint() { return new MyPoint(this.x, this.y); } @Override public String toString() { return "Circle2D{" + "x=" + x + ", y=" + y + ", radius=" + radius + '}'; } public boolean contains(double x, double y) { double distance = getPoint().distance(x, y); if (distance <= radius) return true; else return false; } }
public class Exercise_11 { public static void main(String[] args) { Circle2D c1 = new Circle2D(2, 2, 5.5); System.out.println("C1 area = " + c1.getArea() + " perimeter = " + c1.getPerimeter()); System.out.println("Does c1 contain coordinate (3,3)? " + c1.contains(3, 3)); System.out.println("Does c1 contain circle 2? " + c1.contains(new Circle2D(4, 5, 10.5))); System.out.println("Does c1 overlap circle 3? " + c1.overlaps(new Circle2D(3, 5, 2.3))); } }
Exception in thread "main" java.lang.NoClassDefFoundError: Circle2D
ReplyDeleteat Exercise_11.main(Exercise_11.java:6)
Caused by: java.lang.ClassNotFoundException: Circle2D
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more