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.io.*;
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){
        radius=r;
        height=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());

}
}
OUTPUT-
volume of cylinder is 1538.6000000000001
Area is 747.32

Comments

Popular posts from this blog

cpanel exam CPSP Answers

How to install zimbra collaboration suite 8.8.11 on CentOS 7

awstats installation