Constuctors - Java

Constructor is a special member function in object oriented programming because the classname and method name should be same. it has several characterstics

Characterstics

Types of Constructors

WRITE A PROGRAM ON CONSTRUCTORS

class Box
{
Box()
{
System.out.println("This is sample");
}
}
class Sample
{
public static void main(String args[])
{
Box k=new Box();
}
}

WRITE A PROGRAM TO FIND THE SUM OF TWO NUMBERS USING PARAMETERIZED CONSTRUCTOR.

class Sum
{
Sum(int x,int y)
{
for(int i=0;i<5;i++)
{
System.out.println(y);
}
}
}
class Sample
{
public static void main(String args[])
{
Sum s=new Sum(10,20);
}
}

WRITE A PROGRAM TO FIND THE AREA OF RECTANGLE AND AREA OF TRIANGLE USING PARAMETERIZED AND OVER LOADING CONSTURCTORS.

class Area
{                                                                            
Area(int b,int h)                                             
{                                                                   
System.out.println("Area of rectangle"+(b*h));                       
}                                                                    
Area(int x,double y)                                                 
{                                                                     
System.out.println("Area of triangle"+(x*y)/2);                      
}                                                                    

        Area()                                                                
{                                                                    
int r=10;                                                   
System.out.println("Area of circle"+(3.14*r*r));              
}                                                                    
}                                                                            
class Sample                                                                 
{                                                                             
public static void main(String args[])                               
{                                                                    
Area a=new Area (10,20);                                      
Area b=new Area (20,30.5);                                    
Area c=new Area ();                                           
}                                                                     

}                      

 

For
Online Classes

Contact Us: 9885348743