Question

In: Physics

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() 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 (a) • A method contains(Circle2D circle) that returns true if the specified circle is inside this circle(b) • A method overlaps(Circle2D circle) that returns true if the specified circle overlaps with this circle(c)

Solutions

Expert Solution

import java.lang.Math;

public class Circle2D {
        
        // Data fields for center of the circle.
        private double x;
        private double y;
        
        // Data field for the radius of the circle.
        private double radius;
        
        // A no-argument constructor that creates a circle with (0,0) for (x,y) and 1 for radius.
        Circle2D() {
                x = 0;
                y = 0;
                radius = 1;
                }
        
        // A constructor that creates a circle with the specified x, y, and radius.
        Circle2D(double x, double y, double radius) {
                        this.x = x;
                        this.y = y;
                        this.radius = radius;
                }
        
        // A get method for x.
        public double getX() {
                return x;
        }
        
        // A get method for y.
        public double getY() {
                return y;
        }

        // A get method for radius.
        public double getRadius() {
                return radius;
        }
        
        //  A method that returns the area of the circle. 
        public double getArea() {
                double Area = Math.PI *radius*radius; // Area of circle pi*r^2.
                return Area;
        }
        
         // A method that returns the perimeter of the circle.
        public double getPerimeter() {
                double Perimeter = 2*Math.PI *radius; // Perimeter of circle 2*pi*r.
                return Perimeter;
        }
        
        /* A method that returns true if the specified point (x, y) is inside this circle.
         * Logic : If the distance of given point(x,y) from center is less then radius of
         * the circle, then the point will lie inside the circle.
         */
        public boolean Contains(double x, double y) {
                double SqrDist = Math.pow((this.x - x),2) + Math.pow((this.y - y),2);
                double Dist = Math.sqrt(SqrDist);
                if (Dist < radius ) {
                        return true;
                } else {
                        return false;
                }
        }
        
        /* A method that returns true if the specified circle is inside this circle.
         * Logic : If the sum of the distance between the centers of the circles and
         *  smaller radius is lesser than the bigger radius, then smaller is inside the bigger circle.
         */ 
        public boolean Contains(Circle2D circle) {
                double SqrCentersDist = Math.pow((this.x - circle.getX()),2) + Math.pow((this.y - circle.getY()),2);
                double CentersDist = Math.sqrt(SqrCentersDist);
                double SmallRadius = circle.getRadius();
                
                if ( (CentersDist + SmallRadius) < radius) {
                        return true;
                } else {
                        return false;
                }
                
        }
        
        /* A method that returns true if the specified circle overlaps with this circle.
         * Logic : If sum of the distance between the centers of the circles is less than
         * sum of smaller radius and big radius and greater than difference of big radius and    small radius.
         * then these two circle will overlap.
         */
    public boolean Overlaps(Circle2D circle) {
        double SqrCentersDist = Math.pow((this.x - circle.getX()),2) + Math.pow((this.y - circle.getY()),2);
        double CentersDist = Math.sqrt(SqrCentersDist);
        double SmallRadius = circle.getRadius();
        
        if ((CentersDist<(radius + SmallRadius)) && (CentersDist>(radius - SmallRadius))) {
            return true;
        } else {
            return false;
        }
        
    }
        
}

=======================================================================================

If you want to test this class:

public class Test {

        public static void main(String[] args) {
                // TODO Auto-generated method stub
                Circle2D circle1 = new Circle2D(5,5,10);
                Circle2D circle2 = new Circle2D(5,5,2);
                
                System.out.println("Center = (" + circle1.getX() + ", " + circle1.getY() + ")" );
                System.out.println("Radius = " + circle1.getRadius());
                System.out.println("area = " + circle1.getArea());
                System.out.println("parameter = " + circle1.getPerimeter());
                System.out.println("Is circle1 Contains (1.5, 0.5) = " + circle1.Contains(1.5, 0.5));
                System.out.println("Is circle1 Contains circle2 = " + circle1.Contains(circle2));
                
        }

}

Related Solutions

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)...
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...
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...
Java - Design a class named Account that contains: A private String data field named accountNumber...
Java - 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....
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...
COMPLETE JAVA CODE public class Point2 { private double x; private double y;    /** *...
COMPLETE JAVA CODE public class Point2 { private double x; private double y;    /** * Create a point with coordinates <code>(0, 0)</code>. */ public Point2() { complete JAVA code this.set(0.0, 0.0); COMPLETE CODE }    /** * Create a point with coordinates <code>(newX, newY)</code>. * * @param newX the x-coordinate of the point * @param newY the y-coordinate of the point */ public Point2(double newX, double newY) { complete Java code this.set(newX, newY); }    /** * Create a...
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data...
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data field named id for the account (default 0). 2. A private double data field named balance for the account (default 0). 3. A private double data field named annualInterestRate that stores the current interest rate (default 0). 4. A private Date data field named dateCreated that stores the date when the account was created. 5. A no-arg constructor that creates a default account. 6....
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...
In JAVA please... Define a class named Payment that contains an instance variable "paymentAmount" (non-static member...
In JAVA please... Define a class named Payment that contains an instance variable "paymentAmount" (non-static member variable) of type double that stores the amount of the payment and appropriate accessor (getPaymentAmount() ) and mutator methods. Also create a method named paymentDetails that outputs an English sentence to describe the amount of the payment. Override toString() method to call the paymentDetails() method to print the contents of payment amount and any other details not included in paymentDetails(). Define a class named...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT