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

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...
(Rectangle Class) Create class Rectangle. The class has attributes length and width, each of which defaults...
(Rectangle Class) Create class Rectangle. The class has attributes length and width, each of which defaults to 1. It has read-only properties that calculate the Perimeter and the Area of the rectangle. It has properties for both length and width. The set accessors should verify that length and width are each floating-point numbers greater than 0.0 and less than 20.0. Write an app to test class Rectangle. this is c sharp program please type the whole program.
Write a program to have a Car class which is comparable. Make the Car class comparable...
Write a program to have a Car class which is comparable. Make the Car class comparable and use speed to compare cars. Write a method in Main class that finds the minimum of 4 things (generically). Create 4 cars and pass the cars to the minimum method and find the smallest car. Have a toString method for the Car class. The main program should be able to find out how many cars are created so far by calling a static...
1. Can you extend an abstract class? In what situation can you not inherit/extend a class?...
1. Can you extend an abstract class? In what situation can you not inherit/extend a class? 2. Can you still make it an abstract class if a class does not have any abstract methods?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT