Question

In: Computer Science

public class Point { protected double x, y; // coordinates of the Point //Default constructor public...

public class Point
{
        protected double x, y; // coordinates of the Point

                //Default constructor
        public Point()
        {
        setPoint( 0, 0 );
        }

                //Constructor with parameters
        public Point(double xValue, double yValue )
        {
        setPoint(xValue, yValue );
        }

                // set x and y coordinates of Point
        public void setPoint(double xValue, double yValue )
        {
        x = xValue;
        y = yValue;
        }

                // get x coordinate
        public double getX()
        {
        return x;
        }

                // get y coordinate
        public double getY()
        {
        return y;
        }

                // convert point into String representation
        public String toString()
        {
        return "[" + String.format("%.2f", x)
               + ", " + String.format("%.2f", y) + "]";
        }

     //Method to compare two points
        public boolean equals(Point otherPoint)
        {
        return(x == otherPoint.x &&
               y == otherPoint.y);
        }

     //Method to compare two points
        public void makeCopy(Point otherPoint)
        {
        x = otherPoint.x;
        y = otherPoint.y;
        }

        public Point getCopy()
        {
        Point temp = new Point();

        temp.x = x;
        temp.y = y;

        return temp;
        }

                // print method
        public void printPoint()
        {
        System.out.print("[" + String.format("%.2f", x)
                       + ", " + String.format("%.2f", y) + "]");
        }

}  // end class Point

**********************************************************************************************************************************************************************

Every circle has a center and radius. Given the radius, we can determine the circle’s area and circumference. Given the center, we can determine its position in the x-y plane. The center of a circle is a point in the x-y plane. Please do the following:

  1. Design the class Circle that can store the radius and the center of the circle based on the class Point (above). You should be able to perform the usual operation on a circle, such as setting radius, printing the radius, calculating and printing the area and circumference, and carrying out the usual operations on the center.
  2. Write a test program to test your program.

Solutions

Expert Solution

//Java code

public class Point
{
    protected double x, y; // coordinates of the Point

    //Default constructor
    public Point()
    {
        setPoint( 0, 0 );
    }

    //Constructor with parameters
    public Point(double xValue, double yValue )
    {
        setPoint(xValue, yValue );
    }

    // set x and y coordinates of Point
    public void setPoint(double xValue, double yValue )
    {
        x = xValue;
        y = yValue;
    }

    // get x coordinate
    public double getX()
    {
        return x;
    }

    // get y coordinate
    public double getY()
    {
        return y;
    }

    // convert point into String representation
    public String toString()
    {
        return "[" + String.format("%.2f", x)
                + ", " + String.format("%.2f", y) + "]";
    }

    //Method to compare two points
    public boolean equals(Point otherPoint)
    {
        return(x == otherPoint.x &&
                y == otherPoint.y);
    }

    //Method to compare two points
    public void makeCopy(Point otherPoint)
    {
        x = otherPoint.x;
        y = otherPoint.y;
    }

    public Point getCopy()
    {
        Point temp = new Point();

        temp.x = x;
        temp.y = y;

        return temp;
    }

    // print method
    public void printPoint()
    {
        System.out.print("[" + String.format("%.2f", x)
                + ", " + String.format("%.2f", y) + "]");
    }

}  // end class Point

//=====================================

public class Circle {
    private double radius;
    private Point center;

    //Constructor

    public Circle(double radius, Point center) {
        this.radius = radius;
        this.center = center;
    }
    //Default Constructor
    public Circle()
    {
        radius = 0;
        center = new Point();
    }
    //getters and setters

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

    public Point getCenter() {
        return center;
    }

    public void setCenter(Point center) {
        this.center = center;
    }
    //Calculate Area
    public double getArea()
    {
        return Math.PI*radius*radius;
    }
    public double getCircumference()
    {
        return 2* Math.PI* radius;
    }
    public double getDistance()
    {
        return Math.sqrt((center.x*center.x) +(center.y*center.y));
    }

    @Override
    public String toString() {
        return "Center: "+center+" radius: "+radius;
    }
}

//============================================

public class TestCircle {
    public static void main(String[] args)
    {
        Circle circle = new Circle(10,new Point(50,60));
        System.out.println(circle);
        System.out.println("Area: "+circle.getArea());
        System.out.println("Circumference: "+circle.getCircumference());
        System.out.println("Distance from origin: "+circle.getDistance());
        System.out.println("Done....");
    }
}

//Output

//If you need any help regarding this solution ............ please leave a comment ..... thanks


Related Solutions

in C++ Write a definition for a class ‘point’ to describe a point with coordinates (X,Y)...
in C++ Write a definition for a class ‘point’ to describe a point with coordinates (X,Y) in 2-D space. The private data members are the coordinates X and Y of type ‘double’. The public member functions are: A default constructor (no parameters) to initialize X and Y to zero. An explicit value constructor with two parameters to initialize the values of X and Y. Two functions, one to set the X value and the other to set the Y value...
Given the definition for a Point class that holds the coordinates of the point as double...
Given the definition for a Point class that holds the coordinates of the point as double values x and y, write a function called pt_dist that takes two points and returns the straight-line distance between them (as a double). Use two ways, pt_dist function version and the pt_dist method version. In main, include two if else tests for each, If passed "TEST PASSED, DIST IS " else "Test Failed, dist is ". Hint: Rhymes with Bythagorean Beorem. #include <iostream> #include...
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 MyPoint to represent a point with x- and y-coordinates. The class should...
Design a class named MyPoint to represent a point with x- and y-coordinates. The class should contain: Two data fields x and y that represent the coordinates. A no-arg constructor that creates a point at (0, 0). A constructor that creates a point with specified coordinates. Get methods for data fields x and y respectively. A method named distance that returns the distance from this point to another point with specified x- and y-coordinates. Use the formula: root (x2-x1)2 +...
Define the following class: class XYPoint { public: // Contructors including copy and default constructor //...
Define the following class: class XYPoint { public: // Contructors including copy and default constructor // Destructors ? If none, explain why double getX(); double getY(); // Overload Compare operators <, >, ==, >=, <=, points are compare using their distance to the origin: i.e. SQR(X^2+Y^2) private: double x, y; };
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y;...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y; public MyPoint() { this(0, 0); } public MyPoint(double x, double y) { this.x = x; this.y = y; } // Returns the distance between this MyPoint and other public double distance(MyPoint other) { return Math.sqrt(Math.pow(other.x - x, 2) + Math.pow(other.y - y, 2)); } // Determines if this MyPoint is equivalent to another MyPoint public boolean equals(MyPoint other) { return this.x == other.x &&...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y;...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y; public MyPoint() { this(0, 0); } public MyPoint(double x, double y) { this.x = x; this.y = y; } // Returns the distance between this MyPoint and other public double distance(MyPoint other) { return Math.sqrt(Math.pow(other.x - x, 2) + Math.pow(other.y - y, 2)); } // Determines if this MyPoint is equivalent to another MyPoint public boolean equals(MyPoint other) { return this.x == other.x &&...
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...
public class GroceryCart { private static final int DEFAULT_CAPACITY = 10; /* * Default constructor with...
public class GroceryCart { private static final int DEFAULT_CAPACITY = 10; /* * Default constructor with zero arguments. This constructs a grocery * cart with a default capacity of ten items. */ public GroceryCart() { } /* * Alternate constructor which takes in a maxCapacity. This maxCapacity * determines how many items can fit inside of this groceryCart. */ public GroceryCart(int maxCapacity) { } /* * Adds an item to the grocery cart. Returns true if the item was added...
class Point3d { public: Point3d(double x, double y, double z); Point3d(const point3d& p); void setX(double x);...
class Point3d { public: Point3d(double x, double y, double z); Point3d(const point3d& p); void setX(double x); void setY(double y); void setZ(double z); double getX() const; double getY() const; double getZ() const; point3d& operator=(const point3d& rhs); private: double x; double y; double z; }; Given the Point class above, complete the following: 1. Write just the signature for the overloaded addition operator that would add the respective x,y, and z from two point3d objects. 2. What is the output of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT