Question

In: Computer Science

Define the circle2d class that contains: Two double data fields named x and y that specify...

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 1 for the radius

A constructor that creates a circle with the specified x,y of the the circle

A method getArea() that returns the area of the circle

A method Contains(Double x, Double y) that returns true if the specified point (x,y) is inside this circle

A mothd Contains(circle2D circle) that returns true if the specified circle is sinside this circle

A method overlaps(Circle2D circle) that returns true if the specified circle overlaps with this circle.

Partial code for visualization

public static void main(String[] args) {

    Circle2D c1 = new Circle2D(2, 2, 5.5);

    System.out.println("Area is " + c1.getArea());

    System.out.println("Perimeter is " + c1.getPerimeter());

    System.out.println("c1 contains point (3, 3)? "

      + c1.contains(3, 3));

    System.out.println("c1 contains circle Circle2D(4, 5, 10.5)? "

      + c1.contains(new Circle2D(4, 5, 10.5)));

    System.out.println("c1 overlaps circle Circle2D(3, 5, 2.3)? "

      + c1.overlaps(new Circle2D(3, 5, 2.3)));

}

Java language

Solutions

Expert Solution

public class Circle2D {

    private double x, y;
    private double radius;

    public Circle2D() {
        this(0, 0, 1);
    }

    public Circle2D(double x, double y, double radius) {
        this.x = x;
        this.y = y;
        this.radius = radius;
    }

    public double getArea() {
        return Math.PI * radius * radius;
    }

    public double getPerimeter() {
        return 2 * Math.PI * radius;
    }

    private double distance(double x, double y) {
        return Math.sqrt(Math.pow(x - this.x, 2) + Math.pow(y - this.y, 2));
    }

    public boolean contains(double x, double y) {
        return distance(x, y) <= radius;
    }

    public boolean contains(Circle2D circle) {
        return distance(circle.x, circle.y) + circle.radius <= this.radius;
    }

    public boolean overlaps(Circle2D circle) {
        return distance(circle.x, circle.y) <= (this.radius + circle.radius);
    }
}

Related Solutions

java Define the Circle2D class that contains: • Two double data fields named x and y...
java Define the Circle2D class that contains: • Two double data fields named x and y that specify the center of the circle with get methods. A data field radius with a get method. • A data field radius with a get method. • A no-arg constructor that creates a default circle with (0, 0) for (x, y) and 1 for radius. • A constructor that creates a circle with the specified x, y, and radius. • A method getArea()...
Create a class named Horse that contains the following data fields: name - of type String...
Create a class named Horse that contains the following data fields: name - of type String color - of type String birthYear - of type int Include get and set methods for these fields. Next, create a subclass named RaceHorse, which contains an additional field, races (of type int), that holds the number of races in which the horse has competed and additional methods to get and set the new field. ------------------------------------ DemoHorses.java public class DemoHorses {     public static void...
Create a class named Sandwich that contains the following data fields: MainIngredient - of type String...
Create a class named Sandwich that contains the following data fields: MainIngredient - of type String Bread - of type String Price - of type Double Include get and set methods for these fields. The methods should be prefixed with 'get' or 'set' respectively, followed by the field name using camel case. For example, setMainIngredient. Use the application named TestSandwich that instantiates one Sandwich object and demonstrates the use of the set and get methods to test your class. ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------...
Introduction to Inheritance (Exercise 1) Write the class named Horse that contains data fields for the...
Introduction to Inheritance (Exercise 1) Write the class named Horse that contains data fields for the name, color, and birth year. Include get and set methods for these fields. Next, finish the subclass named RaceHorse, which contains an additional field that holds the number of races in which the horse has competed and additional methods to get and set the new field. Run the provided DemoHorses application that demonstrates using objects of each class. DemoHorses.java public class DemoHorses { public...
1. Define a class named Book that contains:  An int data field named pages that...
1. Define a class named Book that contains:  An int data field named pages that stores the number of pages in the book.  A String data field named title that represents the title of the book.  A constructor with parameters for initializing pages and title.  The getter and setter methods for all data fields.  A toString method that returns book information, including the book title and pages.  The equals method that returns true if...
Design a class named GeoPoint to represent a point with x- and y-coordinates. The class contains:  ...
Design a class named GeoPoint to represent a point with x- and y-coordinates. The class contains:   The data fields x and y that represent the coordinates with gette and setter methods. A no-arg constructor that creates a point (0, 0).   A constructor that constructs a point with specified coordinates. The method equals(GeoPoint p) that returns true if two GeoPoint objects have the same x- and y-coordinates. Write a test program that creates an array of GeoPoint objects. The size of...
in c#: Create a class named Square that contains fields for area and the length of...
in c#: Create a class named Square that contains fields for area and the length of a side and whose constructor requires a parameter for the length of one side of a Square. The constructor assigns its parameter to the length of the Square’s side field and calls a private method that computes the area field. Also include read-only properties to get a Square’s side and area. Create a class named DemoSquares that instantiates an array of ten Square objects...
Design a class named Robot. The Robot class has three private data fields: position x, position...
Design a class named Robot. The Robot class has three private data fields: position x, position y, and the direction (east, south, west, and north). Assume that positive x points to the east and positive y points to the south, and initially x = 0, y = 0, direction = "east". Create a no-arg constructor and another constructor which sets the three data fields. Create the accessors and mutators for the three data fields. Create a method named forward that...
Define a class named Document that contains an instance variable of type String named text that...
Define a class named Document that contains an instance variable of type String named text that stores any textual content for the document. Create a method named toString that returns the text field and also include a method to set this value. Next, define a class for Email that is derived from Document and includes instance variables for the sender, recipient, and title of an email message. Implement appropriate set and get methods. The body of the email message should...
In Java, design a class named MyInteger. The class contains: An int data field named value...
In Java, design a class named MyInteger. The class contains: An int data field named value that stores the int value represented by this object. A constructor that creates a MyInteger object for the specified int A get method that returns the int Methods isEven(), isOdd(), and isPrime() that return true if the value is even, odd, or prime, respectively. Static methods isEven(int), isOdd(int), and isPrime(int) that return true if the specified value is even, odd, or prime, respectively. Static...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT