Answer to Question #300272 in Java | JSP | JSF for Abusiwe

Question #300272

c) The Rectangle class contains:





• Two instance variables width (double) and length (double).





• Three constructors as shown. The no-arg constructor initializes the width and length to 1.0.





• Getter and setter for all the instance variables.





• Methods getArea() and getPerimeter().





• Override the inherited toString() method, to return "A Rectangle with width=xxx and length=zzz, which is a subclass of yyy", where yyy is the output of the toString() method from the superclass.






1
Expert's answer
2022-02-20T09:59:56-0500


class Rectangle {
	private double length;
	private double width;


	public Rectangle() {
		this.length = 1.0;
		this.width = 1.0;
	}


	public Rectangle(double side) {
		this.length = side;
		this.width = side;
	}


	public Rectangle(double length, double width) {
		this.length = length;
		this.width = width;
	}


	public double getArea() {
		return this.length * this.width;
	}


	public double getPerimeter() {
		return 2 * (this.length + this.width);
	}


	/**
	 * @return the length
	 */
	public double getLength() {
		return length;
	}


	/**
	 * @param length the length to set
	 */
	public void setLength(double length) {
		this.length = length;
	}


	/**
	 * @return the width
	 */
	public double getWidth() {
		return width;
	}


	/**
	 * @param width the width to set
	 */
	public void setWidth(double width) {
		this.width = width;
	}


	public String toString() {
		return "A Rectangle with width=" + width + " and length=" + length + ", which is a subclass of "
				+ super.toString();
	}
}

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