java program for Area and perimeter of rectangle and square using constructor overloading

/*Area and perimeter of rectangle and square using constructor overloading.

Constructor overloading means there will be two or more constructor and the name of all constructors will be same only number of arguments will be different.

when you pass arguments value in the constructor it matches the number of arguments value with the defined constructor arguments and executes that constructor automatically

Here Box is constructor.
*/
import java.io.*;
import java.util.*;
//Instance variable  of Box.

class Box{
double length;
double width;

//Constructor for rectangle when two parameter are passed (area).
Box(double l,double w){
length=l;
width=w;
}
//method for calculating area of rectangle.
double AreaRectangle(){
return length*width;
}
//method for calculating permeter of rectangle.
double PerimeterRectangle(){
return 2*(length + width);
}


//Constructor for square when one value is initialized(area).
Box(double l){
length=l;
}
//method for calculating area of square.
double AreaSquare(){
return length*length*length;
}
//method for calculating perimeter of square.
double perimeterSquare(){
return 4*length;
}
}
//Main class from where program execution begins.
class Square_Rectangle_Const_Overloading{
public static void main(String args[]){
//creating object Rectangle name rectangle.
Box rectangle = new Box(10,20);
//creting object name square.
Box square = new Box(10);
System.out.println("Area of rectangle is " + rectangle.AreaRectangle());
System.out.println("Area of square is " + square.AreaSquare());
System.out.println("Perimeter of Rectangle " + rectangle.PerimeterRectangle());
System.out.println("Perimeter of Square " + square.perimeterSquare());
}
}
OUTPUT-
Area of rectangle is 200.0
Area of square is 1000.0
Perimeter of Rectangle 60.0
Perimeter of Square 40.0



Comments

Popular posts from this blog

cpanel exam CPSP Answers

How to install zimbra collaboration suite 8.8.11 on CentOS 7

awstats installation