asked 38.9k views
1 vote
Use the script below as a starting point to create a Rectangle class. Your rectangle class must have the following methods;A constructor that takes a length and width as parametersgetArea - returns the area of the rectangleisSquare - returns True if length and width are equivalentoverloaded equivalence - compares the length and width of two rectangles and returns true if they are equivalent

asked
User Vinodh
by
7.4k points

1 Answer

1 vote

Answer:

The solution code is written in Java.

  1. public class Rectangle {
  2. private double length;
  3. private double width;
  4. public Rectangle(double length, double width){
  5. this.length = length;
  6. this.width = width;
  7. }
  8. public double getArea(){
  9. return this.length * this.width;
  10. }
  11. public boolean rectangleSquare(){
  12. if(this.length == this.width){
  13. return true;
  14. }else{
  15. return false;
  16. }
  17. }
  18. public boolean equivalence(Rectangle rect){
  19. if(this.width == rect.width && this.length == rect.length){
  20. return true;
  21. }else{
  22. return false;
  23. }
  24. }
  25. }

Step-by-step explanation:

Create a class, Rectangle (Line 1) and define two private attributes, width and length (Line 3 - 4). The reason to define the attributes as double type is because the width and length are presumed to be either an integer or a decimal value.

Create constructor which takes width and length as parameters (Line 6).

Next, the require methods, getArea(), isSquare() and overloaded equivalence() along with their expected parameters and return output, are created accordingly in Line 11 -28.

answered
User Pratik Fagadiya
by
8.5k points

No related questions found