Cylinder class java program demo.
In this post, we will create a Cylinder java class and a demo main program to calculate the volume of the cylinder.
Java Program
public class Cylinder { private double height; private double radius; /** * @param height * @param radius */ public Cylinder(double height, double radius) { this.height = height; this.radius = radius; } /** * @return the height */ public double getHeight() { return height; } /** * @param height the height to set */ public void setHeight(double height) { this.height = height; } /** * @return the radius */ public double getRadius() { return radius; } /** * @param radius the radius to set */ public void setRadius(double radius) { this.radius = radius; } //calculating Volume of the cylinder public double calculateVolume() { return Math.PI * radius * radius * height; } }
// Main class public class CylinderMain { public static void main(String args[]) { // Creating two Cylinder objects Cylinder c1 = new Cylinder(5.0,3.5); Cylinder c2 = new Cylinder(15.0,5.5); System.out.println("--------------Output----------"); System.out.println("Height of C1 = " + c1.getHeight()); System.out.println("Radius of C1 = " + c1.getRadius()); System.out.println("Volume of C1 = " + c1.calculateVolume()); System.out.println("Height of C2 = " + c2.getHeight()); System.out.println("Radius of C2 = " + c2.getRadius()); System.out.println("Volume of C2 = " + c2.calculateVolume()); } }
Code Screenshot
Output
————–Output———-
Height of C1 = 5.0
Radius of C1 = 3.5
Volume of C1 = 192.42255003237483
Height of C2 = 15.0
Radius of C2 = 5.5
Volume of C2 = 1425.4976665663685