Answer to Question #321119 in Java | JSP | JSF for Drill

Question #321119

Consider a rectangular shape as an object that has two sides length and width as key attributes, another property is to check if the shape is a square or not. Apart from getters, setters and constructors, your class should have the following functionality listed below:


A) isSquare() : returns true if the values of length and width are equal

B) calculateArea() : Calculates the area of the shape provide with the respective formula, returning the Area with the formula used

C) calculatePerimeter(): Calculates the perimeter of the shape returning the result and formula used

D) Provide a toString() : to represent all the shape attributes and the value returned by the isSquare, including results for the Area and Perimeter as provided above


1. Implement class modelled above and test its functionality as specified below


Run1

Enter the length and width values separated by a space: 10 8

Enter the length and width values separated by a space: 6 6



1
Expert's answer
2022-03-30T14:09:57-0400


import java.util.*;


class Rectangle {
	private double width;
	private double length;


	public Rectangle() {
	}


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


	public double calculateArea() {
		return width * length;
	}


	public double calculatePerimeter() {
		return 2 * (width + length);
	}


	public boolean isSquare() {
		return width == length;
	}


	public String toString() {


		return "Width: " + width + "\nLength: " + length + "\nArea: " + calculateArea() + "\nPerimeter: "
				+ calculatePerimeter() + "\nThe rectangle is " + (isSquare() ? "square" : "not square");
	}
}


class App {


	public static void main(String[] args) {
		Scanner keyboard = new Scanner(System.in);
		System.out.print("Enter the length and width values separated by a space: ");
		double width = keyboard.nextDouble();
		double length = keyboard.nextDouble();
		Rectangle r = new Rectangle(width, length);
		System.out.println(r.toString());
		keyboard.close();
	}
}

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