Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

While using the MyPoint and Circle classes that were created, now create a class that represents a cylinder and a class to represent a rectangle.

While using the MyPoint and Circle classes that were created, now create a class that represents a cylinder and a class to represent a rectangle. First step is to create a UML diagram that would include MyPoint, circle, cylinder, and rectangle. After this write cylinder.java and rectangle.java. Cylinders are circles with a height, so include 2 private data members. ( Circle base and integer height). Cylinders should include the standard accessor/mutator methods, object methods, (toString, equlas, clone) as well so that it calculates its volume. Rectangles are a shape with an intger width and height with an anchor MyPoint to determine the position. Rectangles should include the standard accessor/mutator methods, object methods (toString, equlas, clone) and operations to calculate its area and perimeter.

Please attact a PNG of your UML diagram and the cylinder.java and rectangle.java files.

Circle.java

image text in transcribed

image text in transcribed

Mypoint.java

image text in transcribed

image text in transcribed

ShapeTest.java

image text in transcribed

Calculates the area of the circle * @returns the area of the circle in pixels public double area() return Math.PI radius radius; *Calculates the circumference of the circle * greturns the circumference of the circle in pixels public double circumference() return Math.PI 2 radius; * Creates a text representation of the circle greturn a string representing the circle public String tostring() String description"This circle has a radius of " this.radius+ " pixels and is located at " + this.center.tostring); return description; Compares the circle objects @param c the circle to compare against Oreturns true if both circles have the same radius and center; false otherwise public boolean equals (Circle c) f(c.getcenter() .equals(this.center) && c.getRadus() this.radius) return true; else return false s Creates a circle with the same radius and the same center s returns a Circle object with the same radius and center public circle clone) Circle cnew Circle(this.radius, this.center.clone)) return c; //end of Circle class * Represents a point in an x,y coordinate plane that begins at e,e in the upper left corner import java.lang.Math; public class MyPoint //data members private int x; private int y; Creates a point at (e,e) public MyPoint () this.x = 0; this . y = 0; Creates a point at (x,y) param x the x-coordinate (e or greater) * @param y the y-coordinate (e or greater) public MyPoint (int x, int y) this.x = x; this . y = y; * Returns the x value of the point public int getx(O return this.x; Returns the y value of the point public int gety) return this.y; Sets the x value of the point param x the x-coordinate (e or greater) public void setX(int x) { if(x>-0) this.x x; } Sets the y value of the point param y the y-coordinate (e or greater) public void sety(int y) f if(y>-e) this.y-y; Sets the x and y values of the point param x the x-coordinate (e or greater) param y the y-coordinate (e or greater) public void setY(int x, int y) this.x = x; * Creates a text representation of the point greturn a string representing the point public string tostring() public string tostring() String description"MyPoint: (+ this.x +"this.y")"; return description; Compares the MyPoint objects param p the point to compare against greturns true if both points have the same x and y value; false otherwise public boolean equals (MyPoint p) if(p.getx() this.x && p.gett() this.y ) return true; else return false *Creates a point with the same x and y values Oreturns a MyPoint object with the same x and y values public MyPoint clone) MyPoint p return p; new MyPoint (this.x, this.y); Calculates the distance between this point and the point specified in the parameter param x the x-coordinate param y the y-coordinate s Greturns distance between the two points public double distance(int x, int y) double dist; double xsq (x-this.x) * (x-this.x) ; double ysq = (y-this . y) * (y-this.y); distMath.sqrt(xsq ysa); return dist; Calculates the distance between this point and the point specified in the parameter @param p the point to calcuate the distance from * returns distance between the two points public double distance(MyPoint p) return this.distance(p.getx), p.getyo) ia * Tests the Cylinder and Rectangle classes * @author public class ShapeTest public static void main(String args[]) //Create the objects MyPoint p1new MyPoint(5, 5); Rectangle r = new Rectangle(10, 10, p1); Circle basenew Circle (20, p1); Cylinder c = new Cylinder(30, base); //Testing the rectangle System. out.println("Rectangle r: " + rtoString()); System.out.println("Rectangle r area: r.area()); System.out.println("Rectangle r perimeter: " r.perimeter()); //Testing the cylinder System.out.println("Cylinder c: " c.toString)); System.out.println("Cylinder c volume: " c.volume()); //Testing the circle System.out.println("Circle base: "base.toString()); System.out.println("Circle base area: "base.area()); System.out.println("Circle base circumference: " base.circumference)); Output should look like this: Rectangle r: Rectangle: width of 10 and height of 10, anchored in the upper left at MyPoint: (5,5) Rectangle r area: 100 Rectangle r perimeter: 40 Cylinder c: Cylinder: height of 30, radius of 20 and centered at MyPoint: (5,5) Cylinder c volume: 37699.11184307752 Circle base: This circle has a radius of 20 pixels and is located at MyPoint: (5,5) Circle base area: 1256.6370614359173 Circle base circumference: 125.66370614359172 //end of ShapeTest class

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

I am paid fairly for the work I do.

Answered: 1 week ago

Question

I receive the training I need to do my job well.

Answered: 1 week ago