Question: Please create a UML diagram for this code. MyPoints.java public class MyPoint { private int x; private int y; public MyPoint() { this.x=0; this.y=0; }
Please create a UML diagram for this code.
-
MyPoints.java
public class MyPoint { private int x; private int y; public MyPoint() { this.x=0; this.y=0; } public MyPoint(int x,int y) { if(x>0 && y>0) { this.x=x; this.y=y; } }
public int getX() { return x; }
public void setX(int x) { if(x>0) this.x = x; }
public int getY() { return y; }
public void setY(int y) { if(y>0) this.y = y; } public void setXY(int x,int y) { if(x>0 && y>0) { this.x=x; this.y=y; } }
public boolean equals(MyPoint p) { return p.getX()==this.x && p.getY()== this.y; } @Override public MyPoint clone() { MyPoint p=new MyPoint(this.x,this.y); return p; } public double distnace(int x,int y) { double dist; double xsq=(x-this.x)*(x-this.x); double ysq=(y-this.y)*(y-this.y); dist=Math.sqrt(xsq+ysq); return dist; } public double distance(MyPoint p) { return this.distnace(p.getX(), p.getY()); }
@Override public String toString() { return "MyPoint(" +this.x + "," + this.y + ")"; } }
Circle.java
public class Circle { private int radius; private MyPoint center;
public Circle(int radius) { this.radius = radius; }
public Circle(int radius, MyPoint center) { this.radius = radius; this.center = center; }
public int getRadius() { return this.radius; }
public MyPoint getCenter() { return this.center; }
public void setRadius(int radius) { this.radius = radius; }
public void setCenter(MyPoint center) { this.center = center; } public void setCenter(int x,int y) { if(x>0 && y>0) { this.center=new MyPoint(x,y); } } public double area() { return Math.PI*this.radius*this.radius; } public double circumference() { return Math.PI*2*radius; }
@Override public String toString() { return "This Circle has radius of "+ radius + " pixels and is located at " + this.center.toString(); } public boolean equals(Circle c) { if(c.getCenter().equals(this.center)&& c.getRadius()==this.radius) return true; else return false; } @Override public Circle clone() { Circle c=new Circle(this.radius,this.center.clone()); return c; } }
Cylinder.java
public class Cylinder { private Circle base; private int height;
public Cylinder(int height) { this.height = height; }
public Cylinder(int height,Circle base) { this.base = base; this.height = height; }
public Circle getBase() { return base; }
public int getHeight() { return height; }
public void setBase(Circle base) { this.base = base; }
public void setHeight(int height) { this.height = height; } public void setCircle(int radius,MyPoint center) { this.base=new Circle(radius,center); } public double volume() { return Math.PI*base.getRadius()*base.getRadius()*this.height; } @Override public String toString() { return "cylinder height of"+this.height+" and "+base.toString(); } public boolean equals(Cylinder c) { if(c.getHeight()==this.height && c.getBase().equals(base)) return true; else return false; } public Cylinder clone() { Cylinder c=new Cylinder(this.height,this.base.clone()); return c; } }
Rectangle.java
public class Rectangle { private int width,height; private MyPoint position;
public Rectangle (int width, int height) { this.width = width; this.height = height; }
public Rectangle (int width, int height, MyPoint position) { this.width = width; this.height = height; this.position = position; }
public int getWidth() { return width; }
public int getHeight() { return height; }
public MyPoint getP() { return position; }
public void setWidth(int width) { this.width = width; }
public void setHeight(int height) { this.height = height; }
public void setPosition(MyPoint p) { this.position = p; } public void setPosition(int x,int y) { if(x>0 && y>0) { this.position=new MyPoint(x,y); } } public int area() { return height*width; } public int perimeter() { return 2*(height+width); }
@Override public String toString() { return "width of "+ width + " height=" + height + ", anchored in the upper left at position=" +position.toString(); } public boolean equals(Rectangle r) { if(r.getP().equals(this.position)&& r.getHeight()==this.height && r.getWidth()==this.width) return true; else return false; } @Override public Rectangle clone() { Rectangle r=new Rectangle(this.width,this.height,this.position.clone()); return r; } }
ShapeTest.java
public class ShapeTest {
public static void main(String[] args) { MyPoint p1=new MyPoint(5,5); Rectangle r=new Rectangle(10,10,p1); Circle base= new Circle(20,p1); Cylinder c=new Cylinder(30,base); System.out.println("Rectangle r: "+r.toString()); System.out.println("Rectangle r area :"+r.area()); System.out.println("Rectangle r perimeter :"+r.perimeter()); System.out.println("Cylinder c:"+c.toString()); System.out.println("Cylinder c volume: "+c.volume()); System.out.println("Circle base: "+base.toString()); System.out.println("Circle base area: "+base.area()); System.out.println("Circle base circumference: "+base.circumference()); } }
output
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
