Write a program to create a class Rectangle with the following information:
length, breadth in integer.
• Add two constructors, (a) default constructor, and (b) constructor to pass on length
and breadth of a Rectangle.
• Add a method printData( ) to print the two information about the rectangle in
console.
• Add methods printArea( ) and printPerimeter( ) to compute and print the area and
perimeter of rectangle in console.
• Create two objects of this class, r1 and r2. Show the output of both the constructors
and all method of these two objects
class Rectangle {
private int length;
private int breadth;
/**
* (a) default constructor
*/
public Rectangle() {
}
/**
* constructor to pass on length and breadth of a Rectangle
*
* @param length
* @param breadth
*/
public Rectangle(int length, int breadth) {
this.length = length;
this.breadth = breadth;
}
/**
* Add a method printData( ) to print the two information about the rectangle in
* console.
*/
public void printData() {
System.out.println("Length: " + length);
System.out.println("Breadth: " + breadth);
}
/**
* method printArea() and printPerimeter()
*/
public void printArea() {
int area = length * breadth;
System.out.println("Area: " + area);
}
/**
* print the area and perimeter of rectangle in console.
*/
public void printPerimeter() {
int perimeter = 2 * (length + breadth);
System.out.println("Perimeter: " + perimeter);
}
}
public class App {
/**
* The start point of the program
*
* @param args
*
*/
public static void main(String[] args) {
// Create two objects of this class, r1 and r2. Show the output of both the
// constructor sand all method of these two objects
Rectangle r1 = new Rectangle();
Rectangle r2 = new Rectangle(4, 4);
r1.printData();
r1.printArea();
r1.printPerimeter();
r2.printData();
r2.printArea();
r2.printPerimeter();
;
}
}
Comments
Leave a comment