Write a main method to test the Cylinder class. --------------------------------
ID: 3699455 • Letter: W
Question
Write a main method to test the Cylinder class.
------------------------------------------------------------------
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
------------------------------------------------------------------------
class Circle extends Point{
private double radius;
Circle(double xValue, double yValue, double radius){
super(xValue, yValue);
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public void printRadius(){
System.out.format("Radius of the circle: %.2f ",getRadius());
}
public double getArea(){
return Math.PI*radius*radius;
}
public void printArea(){
System.out.format("Area of the circle: %.2f ",getArea());
}
public double getCircumference(){
return Math.PI*2*radius;
}
public void printCircumference(){
System.out.format("Circumference of the circle: %.2f ",getCircumference());
}
}
---------------------------------------------------------------------------------
public class Cylinder extends Circle {
private double height;
Cylinder(double xValue, double yValue, double radius, double height) {
super(xValue, yValue, radius);
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getSurfaceArea() {
return super.getCircumference() * height;
}
public void printSurfaceArea() {
System.out.format("Surface area of the cylinder: %.2f ", getSurfaceArea());
}
public double getVolume() {
return super.getArea() * getHeight();
}
public void printVolume() {
System.out.format("Volume of the cylinder: %.2f ",getVolume());
}
}
Explanation / Answer
In the constructor of cyclinder class
this.height= height;
Need to be included along with the call to super class constructors to set coordinates and radius.
Below is the main class to test cylinder class
Main.java
___________
public class Main
{
public static void main(String[] args) {
//Create Cylinder
Cylinder c1 = new Cylinder(2,3,3,4);
System.out.println("Cylinder height: "+c1.getHeight());
System.out.println("Cylinder radius: "+c1.getRadius());
c1.printSurfaceArea();
c1.printVolume();
//Set new height, radius and point to existing Cylinder
c1.setHeight(7);
c1.setRadius(5);
c1.setPoint(3,4);
System.out.println("After setting new height,radius,point for existing Cylinder");
System.out.println("New height: "+c1.getHeight());
System.out.println("New raidus: "+c1.getRadius());
System.out.println("New point: ");
c1.printPoint();
System.out.println();
c1.printSurfaceArea();
c1.printVolume();
}
}
Output:
_________'
Cylinder height: 4.0
Cylinder radius: 3.0
Surface area of the cylinder: 75.40
Volume of the cylinder: 113.10
After setting new height,radius,point for existing Cylinder
New height: 7.0
New raidus: 5.0
New point:
[3.00, 4.00]
Surface area of the cylinder: 219.91
Volume of the cylinder: 549.78
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.