Answer to Question #287937 in Java | JSP | JSF for Yash

Question #287937

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

1
Expert's answer
2022-01-16T13:04:26-0500


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();
		;


	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog