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...
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...
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...
I need this done in JAVA. Define a class named Cash. The class contains the following...
I need this done in JAVA. Define a class named Cash. The class contains the following public elements: A Double stored property that contains the amount of money (dollars and cents) described by an object of the class. A read-only, computed property. It calculates and returns the minimum number of U.S. bills and coins that add up to the amount in the stored property.  The return value is an Int array of length 9 that contains (beginning with index 0 of...
Design a class named Account that contains: A private int data field named id for the...
Design a class named Account that contains: A private int data field named id for the account. A private double data field named balance for the account. A private double data field named annualInterestRate that stores the current interest rate. A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0. The accessor and mutator methods for id, balance, and annualInterestRate. A method named getMonthlyInterestRate() that returns the monthly interest rate. A method named withdraw(amount)...
Design a class named Account that contains: A private String data field named accountNumber for the...
Design a class named Account that contains: A private String data field named accountNumber for the account (default AC000). A private double data field named balance for the account (default 0). A private double data field named annualIntRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default account. A constructor...
Write a Data Element Class named Property that has fields tohold the property name, the...
Write a Data Element Class named Property that has fields to hold the property name, the city where the property is located, the rent amount, the owner's name, and the Plot to be occupied by the property, along with getters and setters to access and set these fields. Write a parameterized constructor (i.e., takes values for the fields as parameters) and a copy constructor (takes a Property object as the parameter). Follow the Javadoc file provided.Write a Data Element Class...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT