java program to calculate area and volume using constructor

/*constructor is a special type of method in which the name of the method is same as the class and there is no return type constructor are used to initialize the value of data.*/

//Area and volume of cylinder using constructor.
import java.util.*;
class Vol{
double radius;
double height;
Vol(double r,double h){
radius = r;
height = h;
double vol(){
return 3.14*radius*radius*height;
class Area{
    double radius;
    double height;
    Area(double r,double h){
    double area(){
        return (2*3.14*radius*height) + (2*3.14*radius*radius);

class Area_Vol_Cylin_Const{
public static void main(String args[]){
Vol cylinder=new Vol(7,10);
System.out.println(“volume of cylinder is “+cylinder.vol());
Area area=new Area(7,10);
System.out.println(“Area is “+area.area());

volume of cylinder is 1538.6000000000001
Area is 747.32

Leave a Comment

Your email address will not be published. Required fields are marked *

Open chat