Answer to Question #212242 in Java | JSP | JSF for nimra

Question #212242

  Write a program using class ‘Rectangle’ to compute the area and perimeter of rectangle. The Class rectangle should have the following members:

·        Two data members’ length and width.

·        One member function ‘setData’ to assign values to data members ‘length’ & ‘width’. It should also verify that length and width between 0.0 and 25.0 

·        One member function ‘area’ to compute and print the area of rectangle.

Area =length * width

·        One member function ‘perimeter’ to compute and print the perimeter of rectangle.

Perimeter =2*(length * width)



1
Expert's answer
2021-06-30T07:12:44-0400
public class Rectangle {
	int width;
	int length;


	public void setData(int width, int length) {
		if (width > 0 && width <= 25) {
			this.width = width;
		} else {
			System.out.println("Wrong value!");
		}
		if (length > 0 && length <= 25) {
			this.length = length;
		} else {
			System.out.println("Wrong value!");
		}
	}


	public int area() {
		int area = length * width;
		return area;
	}


	public int perimeter() {
		int perimeter = 2 * (length + width);
		return perimeter;
	}


	public static void main(String[] args) {
		Rectangle rectangle = new Rectangle();
		rectangle.setData(5, 7);
		System.out.println("Rectangle area:" + rectangle.area());
		System.out.println("Rectangle perimeter:" + rectangle.perimeter());


	}
}

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
APPROVED BY CLIENTS