A point in the x-y plane is represented by its x- coordinate and y coordinate. D
ID: 3858726 • Letter: A
Question
A point in the x-y plane is represented by its x- coordinate and y coordinate. Design the class Point that can store and process a point in the x-y plane. You should then perform operations on a point, such as showing the point, setting the coordinates of the point, printing the coordinates of the point, returning the x-coordinate, and returning the y-coordinate. Every circle has a center and a radius. Given the radius, we can determine the circle's area and circumference. Given the center, we can determine its position in the plane. The center of a circle is a point in the x-y plane. Design the class Circle that can store the radius and center of the circle. Because the center is a point in the x-y plane and you designed the class to capture the properties of a point above, you must derive the class Circle from the class Point. You should be able to perform the usual operations on a circle, such as setting the radius, printing the radius, calculating and printing the area and circumference, and carrying out the usual operations on the center. Every cylinder has a base and height, where the base is a circle. Design the class Cylinder that can capture the properties of a cylinder and perform the usual operations on a cylinder. Derive this class from the class Circle designed above. Some of the operations that can be performed on a cylinder are as follows: calculate and print the volume, calculate and print the surface area, set the height, set the radius of the base, and set the center of the base. Write a program to test various operations on a point, circle and cylinder. Your program should be well documented both internally and externallyExplanation / Answer
Point.java
public class Point {
//Declaring instance variables
private int x,y;
//parameterized constructor
public Point(int x, int y) {
super();
this.x = x;
this.y = y;
}
//Setters and getters
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
//Displaying the Point class content
@Override
public String toString() {
return "Point Center [x=" + x + ", y=" + y + "]";
}
}
_____________
Circle.java
public class Circle extends Point {
//Declaring instance variable
private double radius;
//Parameterized constructor
public Circle(int x, int y, double radius) {
super(x, y);
this.radius = radius;
}
//Setters and getters
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
//This method will calculate the area of the circle
public double calArea()
{
return Math.PI*getRadius()*getRadius();
}
//This method will calculate the circumference of the circle
public double calCircumference()
{
return 2*Math.PI*getRadius();
}
//This method will display the contents of the Circle class
@Override
public String toString() {
return super.toString()+" Circle [radius=" + radius + "]";
}
}
________________
Cylinder.java
public class Cylinder extends Circle {
//Declaring variable
private double height;
//parameterized constructor
public Cylinder(int x, int y, double radius, double height) {
super(x, y, radius);
this.height = height;
}
//Setters and getters
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
//this method will calculate the volume of the cylinder
public double calVolume()
{
return super.calArea()*getHeight();
}
//this method will calculate the surface area of the cylinder
public double calSurfaceArea()
{
return 2 * super.calArea() + (2 * Math.PI * getRadius() * getHeight());
}
//This method will display the contents of the Cylinder class content
@Override
public String toString() {
return super.toString()+" Cylinder [height=" + height + "]";
}
}
_________________
TestClass.java
import java.text.DecimalFormat;
public class TestClass {
public static void main(String[] args) {
// DecimalFormat class is used to format the output
DecimalFormat df = new DecimalFormat("#.##");
// Creating the Point class Obejct
Point p = new Point(3, -2);
System.out.println("Point # " + p.toString());
// Creating the Circle class Object
Circle c = new Circle(2, -1, 5.5);
System.out.println(" Circle # :" + c.toString());
// Displaying the Area of the circle
System.out.println("Area of circle :" + df.format(c.calArea()));
// Displaying the circumference of the circle
System.out.println("Circumference of circle :"+ df.format(c.calCircumference()));
// Creating the Cylinder class object
Cylinder cy = new Cylinder(2, -2, 4.5, 5);
System.out.println(" Cylinder # " + cy.toString());
// Displaying the Surface area of the cylinder
System.out.println("Surface Area of Cylinder :"+ df.format(cy.calSurfaceArea()));
// Displaying the Volume of the cylinder
System.out.println("Volume of the Cylinder :"+ df.format(cy.calVolume()));
}
}
_______________
Output:
Point #
Point Center [x=3, y=-2]
Circle #
:Point Center [x=2, y=-1] Circle [radius=5.5]
Area of circle :95.03
Circumference of circle :34.56
Cylinder #
Point Center [x=2, y=-2] Circle [radius=4.5] Cylinder [height=5.0]
Surface Area of Cylinder :268.61
Volume of the Cylinder :318.09
______________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.