Program:-
/*Q1.create a class Cylinder and use getter and setter to set its radius and height*/
class Cylinder{
private int radius;
private int height;
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
this.radius = radius;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
/*Q2.use Q1 to calculate surface area and volume of the cylinder.*/
public double SurfaceArea(){
return Math.PI * radius * radius * height;
}
public double Volume(){
return (2 * Math.PI * radius * height) + (2 * Math.PI * radius * radius );
}
/*Q3.use a constructor and repeat Q1.
public Cylinder(int radius , int height ){
this.radius = radius;
this.height = height;
}
*/
}
//Q4.Overload a constructor used to initiate a rectangle of length 4 and breath 5 for using custom parameter.
class Rectangle1{
private int length;
private int breath;
public Rectangle1() {
length = 4;
breath = 5;
}
public Rectangle1(int length, int breath) {
this.length = length;
this.breath = breath;
}
public int getLength() {
return length;
}
public int getBreath() {
return breath;
}
}
//Q5.Repeat Q1. for a Sphere.
class Sphere{
private int radius;
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
this.radius = radius;
}
public double surfaceArea(){
return 4 * Math.PI * radius * radius;
}
public double volume(){
return (4 * Math.PI * radius * radius * radius)/3;
}
}
public class PracticeSet9V44 {
public static void main(String[] args) {
//Q1.
System.out.println("Q1.");
Cylinder a = new Cylinder();
a.setRadius(12);
a.setHeight(9);
System.out.println("The radius of cylinder is : "+a.getRadius());
System.out.println("The height of cylinder is : "+a.getHeight() +"\n");
//Q2.
System.out.println("Q2.");
System.out.println("The Surface area of cylinder is : "+a.SurfaceArea());
System.out.println("The Volume of cylinder is : "+a.Volume()+"\n");
/* Q3.
System.out.println("Q3.");
Cylinder b = new Cylinder(12,9);
System.out.println("The radius of cylinder is : "+b.getRadius());
System.out.println("The height of cylinder is : "+b.getHeight() +"\n");*/
//Q4.
System.out.println("Q4.");
Rectangle1 c = new Rectangle1();
System.out.println("The length of Rectangle is : "+c.getLength());
System.out.println("The breath of Rectangle is : "+c.getBreath()+"\n");
Rectangle1 d = new Rectangle1(4,5);
System.out.println("The length of Rectangle is : "+d.getLength());
System.out.println("The breath of Rectangle is : "+d.getBreath()+"\n");
Sphere e = new Sphere();
e.setRadius(12);
System.out.println("The radius of sphere is : " + e.getRadius());
System.out.println("The Surface area of sphere is : " + e.surfaceArea());
System.out.println("The volume of sphere is : " + e.volume());
}
}



