How do you this? This program builds on the previous assignment. A typical cylin
ID: 3798881 • Letter: H
Question
How do you this?
This program builds on the previous assignment. A typical cylinder is built on the basis of a circle. In your last assignment you created a class Circle. Your task for this assignment is to use the idea of Aggregation to build a class called Cylinder. The private members of the Cylinder should be a Circle object as well as a doublevariable for the height.
Create appropriate constructors for the Cylinder class including a copy constructor. You may also want to create a copy constructor for the Circle class as well.
Along with the appropriate getters and setters, create methods that determine the area of a cylinder (using methods that the Circle object has already provided), and the volume of a cylinder.
Explanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
public class Circle
{
private double radius;
public Circle() //default constructor
{
radius = 0.0;
}
public Circle(double radius) //parameterized constructor
{
this.radius = radius;
}
public Circle(Circle c) //copy constructor
{
radius = c.radius;
}
public double getRadius()
{
return radius;
}
public double area() //compute area
{
return 3.14*radius*radius;
}
}
public class Cylinder
{
private Circle cir; //circle class object aggregation
private double height;
public Cylinder() //default constructor
{
height = 0.0;
}
public Cylinder(Circle cir1,double height) //parameterized constructor
{
cir = cir1; // using copy constructor of Circle
this.height = height;
}
public Cylinder(Cylinder c) //copy constructor
{
height = c.height;
}
public double area() //compute area of cylinder
{
return (2*3.14*cir.getRadius()*height + 2*cir.area());
}
}
public class TestCylinder
{
public static void main (String[] args)
{
Circle circle1 = new Circle(4.4);
System.out.println("Area of circle : "+circle1.area());
Cylinder cylinder1 = new Cylinder(circle1,6.7);
System.out.println(" Area of cylinder : "+cylinder1.area());
}
}
Output:
Area of circle : 60.7904
Area of cylinder : 306.715
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.