0% found this document useful (0 votes)
46 views

Length Width SC: Package Import Class Private Private New

The document defines a Rectangle class with length and width properties. It includes methods to set/get length and width, calculate area, and create a new rectangle with dimensions multiplied by a given factor. The main method demonstrates creating a rectangle, displaying its properties and area, and generating a new rectangle with resized dimensions.

Uploaded by

sunil kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Length Width SC: Package Import Class Private Private New

The document defines a Rectangle class with length and width properties. It includes methods to set/get length and width, calculate area, and create a new rectangle with dimensions multiplied by a given factor. The main method demonstrates creating a rectangle, displaying its properties and area, and generating a new rectangle with resized dimensions.

Uploaded by

sunil kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

package com.hcl.

pack;
import java.util.Scanner;

class Rectangle {

private Integer length;


private Integer width;
Scanner sc = new Scanner(System.in);

System.out.println("Enter the length of the rectangle");


length = sc.nextInt();

System.out.println("Enter the width of the rectangle");


width = sc.nextInt();

public Rectangle(Integer length, Integer width){


this.width=width;
this.length=width;

}
public void setLength(Integer length){
this.length=length;
}
public void setWidth(Integer width){
this.width=width;
}
public Integer getLength(){
return length;
}
public Integer getWidth(){
return width;
}

public Integer area() {

Integer area = length*width;


return area;

}
public void display(){
System.out.println("Area of the Rectangle:"+area);

Rectangle dimensionChange(Integer newDimension){

Rectangle rectangleObject = new


Rectangle(length*newDimension,width*newDimension);

return rectangleObject;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.println("Enter the length of the rectangle");


Integer length = sc.nextInt();
System.out.println("Enter the width of the rectangle");
Integer width = sc.nextInt();
System.out.println("Rectangle Dimension");
Rectangle rec = new Rectangle(length,width);
System.out.println("Length:"+rec.getLength());
System.out.println("Width:"+rec.getWidth());
rec.display();
System.out.println("Enter the new dimension");
Integer dimension = sc.nextInt();

Rectangle rec1= rec.dimensionChange(dimension);

if(rec1 instanceof Rectangle){


System.out.println("Rectangle Dimension");
System.out.println("Length:"+rec1.getLength());
System.out.println("Width:"+rec1.getWidth());

rec1.display();
}

You might also like