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.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){
//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){
//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());
Area of rectangle is 200.0
Area of square is 1000.0
Perimeter of Rectangle 60.0
Perimeter of Square 40.0

Leave a Comment

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