JAVA | UML Design 1 | The Point and Circle Classes

Perahin
0

 Suppose You are given a UML design of Circle and Point like below:


>> Your task is to code this UML design

It contains:

1. Two private member variables: a radius (double) and a center (an instance of Point class, which we created earlier).


2. The constructors, public getters and setters.

3. Methods getCenterX()setCenterX()getCenterY()setCenterY()getCenterXY()setCenterXY(), etc.

4. A toString() a method that returns a string description of this an instance in the format of "Circle[center=(x,y),radius=r]". You should re-use the Point's toString() to print "(x,y)".

5. A distance(Circle another) a method that returns the distance from the center of this instance to the center of the given Circle instance (called another).


SOLUTION:

#Point.java:


 public class Point {
    private int x, y;

    Point(){
        this.x = 0;
        this.y = 0;
    }
    Point(int x, int y){
        this.x = x;
        this.y = y;
    }

    public void setX(int x) {
        this.x = x;
    }
    public void setY(int y) {
        this.y = y;
    }
    public int getX() {
        return x;
    }
    public int getY() {
        return y;
    }
    public String toString(){
        return "("+x+","+y+")";
    }
    public int[] getXY(){
        int p[] ={x,y};
        return p;
    }
    public void setXY(int x, int y){
        this.x = x;
        this.y = y;
    }

    public double distance(int x, int y){
        return Math.sqrt(Math.pow((this.x-x),2)-Math.pow((this.y-y),2));
    }
    public double distance(Point p){
        return Math.sqrt(Math.pow((this.x-p.x),2)-Math.pow((this.y-p.y),2));
    }
    public double distance(){
        return Math.sqrt(Math.pow(this.x,2)-Math.pow(this.y,2));
    }
}
 

#Circle.java:

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

    Circle(){
        center = new Point();
        radius  = 1;
    }
    Circle(int x, int y, int radius){
        center = new Point(x,y);
        this.radius = radius;
    }
    Circle(Point center, double radius){
        this.center = center;
        this.radius = radius;
    }
    public double getRadius() {
        return radius;
    }
    public Point getCenter() {
        return center;
    }
    public void setRadius(double radius) {
        this.radius = radius;
    }
    public void setP(Point center) {
        this.center = center;
    }

    public int getCenterX(){
        return center.getX();
    }
    public void setCenterX(int x){
        center.setX(x);
    }
    public int getCenterY(){
        return center.getY();
    }
    public void setCenterY(int y){
        center.setY(y);
    }

    public int[] getCenterXY(){
        return center.getXY();
    }
    public void setCenterXY(int x, int y){
        center.setXY(x,y);
    }

    public String toString(){
        return "Circle[center= "+center+",radius= "+radius+"]";
    }

    double getArea(){
        return 3.1416*radius*radius;
    }
    double getCircumference(){
        return 2*3.1416*radius;
    }
    double distance(Circle c){
        return center.distance(c.getCenterX(),c.getCenterY());
    }

}

#TO CHECK:
#main.java:

public class CircleMain {
    public static void main(String[] args) {
        Point p1 = new Point(5,5);
        Circle c1 = new Circle(1,1,2);
        Circle c2 = new Circle(p1,5);
        System.out.println("C1:"+c1);
        System.out.println("Area: "+c1.getArea());
        System.out.println("Circumference: "+c1.getCircumference());
        System.out.println("C2:"+c2);
        System.out.println("Area: "+c2.getArea());
        System.out.println("Circumference: "+c2.getCircumference());
        System.out.println("Distance between c1 and c2: "+c1.distance(c2));
    }
}

Post a Comment

0Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !