Question

In: Computer Science

(Enable Rectangle comparable) Rewrite the Rectangle class in Listing 13.3 to extend GeometricObject and implement the...

(Enable Rectangle comparable) Rewrite the Rectangle class in Listing 13.3 to extend GeometricObject and implement the Comparable interface. Override the equals method in the Object class. Two Rectangle objects are equal if their areas are the same. Draw the UML diagram that involves Rectangle, GeometricObject, and Comparable. Also include explain paragraph.

Solutions

Expert Solution

SOLUTION-

code -

Program code screen shot:

Program code:

Program code:

//define the GeometricObject of the abstract class

public abstract class GeometricObject

{

     private String color = "white";

     private boolean filled;

     private java.util.Date dateCreated;

     // Construct a default geometric object

     protected GeometricObject()

     {

          dateCreated = new java.util.Date();

     }

     // Construct a default geometric object with

     //color and filled value

     protected GeometricObject(String color, boolean filled)

     {

          dateCreated = new java.util.Date();

          this.color = color;

          this.filled = filled;

     }

     // Returns color

     public String getColor()

     {

          return color;

     }

     // assign a new color

     public void setColor(String color)

     {

          this.color = color;

     }

     // returns filled value

     public boolean isFilled()

     {

          return filled;

     }

     // Set a new filled value

     public void setFilled(boolean filled)

     {

          this.filled = filled;

     }

     // returns creation date

     public java.util.Date getDateCreated()

     {

          return dateCreated;

     }

     // returns string representation of object

     public String toString()

     {

          return "created on "+dateCreated+"\ncolor: "+color+" and filled: "+filled;

     }

     public abstract double getArea();

     public abstract double getPerimeter();

}

// Defines rectangle class

public class Rectangle extends GeometricObject

{

     public double width;

     public double height;

     // construct a rectangle

     public Rectangle(double width, double height)

     {

          this.width = width;

          this.height = height;

     }

     // method to find out perimeter

     public double getPerimeter()

     {

          return 2 * (width + height);

     }

     // method to find out area

     public double getArea()

     {

          return width * height;

     }

     // method to check equality

     public boolean equals(Object obj)

     {

          System.out.println(this.getArea()+" "+((Rectangle)obj).getArea());

          if(this.getArea() == ((Rectangle)obj).getArea())

              return true;

          return false;

     }

}

//Definition of the ComparableRectangle that implements the Comparable

//interface

public class ComparableRectangle extends Rectangle implements Comparable

{

     //constructor

     public ComparableRectangle(double width, double height)

     {

          super(width, height);

     }

     //overriding the compareTo method

     public int compareTo(Object o)

     {

          //logic to compare the Areas of the Rectangle objects

          if(getArea() > ((ComparableRectangle)o).getArea())

              return 1;

          else if(getArea()

                   < ((ComparableRectangle)o).getArea())

              return -1;

          else

              return 0;

     }

}

//definition of the TestEqualRectangle

public class TestEqualRectangle

{

     public static void main(String[] args)

     {

          //create objects of the Rectangle classes

          Rectangle cc = new Rectangle(6, 7);

          Rectangle cc1 = new Rectangle(8, 8);

          //call the equals function to compare the two

          //objects

          boolean res = cc.equals(cc1);

          //print the results

          System.out.println("Result: "+res);

     }

}

Sample Output:

42.0 64.0

Result: false

UML diagram:


IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK YOU!!!!!!!!----------


Related Solutions

DO THIS IN C#,Design and implement class Rectangle to represent a rectangle object. The class defines...
DO THIS IN C#,Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: 1. Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 1.0 in the default constructor. 2. A non-argument constructor method to create a default rectangle. 3. Another constructor method to create a rectangle with user-specified height and width. 4. Method getArea() that returns...
IN C++!!! Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class...
IN C++!!! Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 0 in the default constructor. A non-argument constructor method to create a default rectangle. Another constructor method to create a rectangle with user-specified height and width. Method getArea() that returns the area. Method...
Design and implement a class Rectangle to represent a rectangle. You should provide two Constructors for...
Design and implement a class Rectangle to represent a rectangle. You should provide two Constructors for the class, the first being the default constructor and the second which takes the basic dimensions and sets up the private member variables with the appropriate initial values. Methods should be provided that allow a user of the class to find out the length, width, area and perimeter of the shape plus a toString()method to print the values of the basic dimensions. Now implement...
3.2. Unfortunately, you cannot modify the Rectangle class so that it implements the Comparable interface. The...
3.2. Unfortunately, you cannot modify the Rectangle class so that it implements the Comparable interface. The Rectangle class is part of the standard library, and you cannot modify library classes. Fortunately, there is a second sort method that you can use to sort a list of objects of any class, even if the class doesn't implement the Comparable interface. Comparator<T> comp = . . .; // for example, Comparator<Rectangle> comp = new RectangleComparator(); Collections.sort(list, comp); Comparator is an interface. Therefore,...
ASAP (Use BigInteger for the Rational class) Redesign and implement the Rational class in Listing 13.13...
ASAP (Use BigInteger for the Rational class) Redesign and implement the Rational class in Listing 13.13 using BigInteger for the numerator and denominator. Use the code at https://liveexample.pearsoncmg.com/test/Exercise13_15Test.txt to test your implementation. Here is a sample run: Sample Run Enter the first rational number: 3 454 Enter the second second number: 7 2389 3/454 + 7/2389 = 10345/1084606 3/454 - 7/2389 = 3989/1084606 3/454 * 7/2389 = 21/1084606 3/454 / 7/2389 = 7167/3178 7/2389 is 0.0029300962745918793 Class Name: Exercise13_15 If...
PUT IN JAVA PROGRAMMING The Rectangle class: Design a class named Rectangle to represent a rectangle....
PUT IN JAVA PROGRAMMING The Rectangle class: Design a class named Rectangle to represent a rectangle. The class contains: • Two double data fields named width and height that specify the width and height of a rectangle. The default values are 1 for both width and height. • A no-arg (default) constructor that creates a default rectangle. • A constructor that creates a rectangle with the specified width and height. • A method named findArea() that finds the area of...
In java design and code a class named comparableTriangle that extends Triangle and implements Comparable. Implement...
In java design and code a class named comparableTriangle that extends Triangle and implements Comparable. Implement the compareTo method to compare the triangles on the basis of similarity. Draw the UML diagram for your classes. Write a Driver class (class which instantiates the comparableTriangle objects) to determine if two instances of ComparableTriangle objects are similar (sample output below). It should prompt the user to enter the 3 sides of each triangle and then display whether or not the are similar...
c++ E2b: Design a class named Rectangle to represent a rectangle. The class contains:
using c++E2b: Design a class named Rectangle to represent a rectangle. The class contains:(1) Two double data members named width and height which specifies the width and height of the rectangle .(2) A no-arg constructor that creates a rectangle with width 1 and height 1.(3) A constructor that creates a rectangle with the specified width and height .(4) A function named getArea() that returns the area of this rectangle .(5) A function named getPerimeter() that returns the perimeter of this...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The class contains: Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with specified width and height A method name getWidth() return the value of width A method named getHeight() returns value of...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The class contains: Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with specified width and height A method name getWidth() return the value of width A method named getHeight() returns value of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT