Question: Lab Demo Stable Create a project called sortingStability. It includes two classes: Rectangle.java and DemoStable.javaClass Rectangle: Implement class Rectangle based on the UML class diagram

Lab Demo Stable Create a project called sortingStability.

It includes two classes: Rectangle.java and DemoStable.javaClass Rectangle:

Implement class Rectangle based on the UML class diagram below

private int length

private int width

<< constructior>> rectangle length: int, width: int

public get length

public get width

public perimeter

public area

tostring

The toString method should return a string of the form [lengthxwidth] e.g.[4x7]Class DemoStable:

Class DemoStable is the test client of class Rectangle. It includes the main method. In main do the following; Create an array of Rectangles and initialize it with the following rectangles:

[4x2], [3x5], [4x3], [6x2], [3x4], [4x4], [6x4], [12x2], [2x4], [4x6], and [2x12]

Print the elements of the array separated by a comma and delimited with brackets. Include a left-aligned label. Expected Output at this point:

rectangles : [[4x2], [3x5], [4x3], [6x2], [3x4], [4x4], [6x4], [12x2], [2x4], [4x6], [2x12]]

In Class Rectangle:

Implement the interface Comparable It should compare Rectanbles based on their length. If a Rectangle is longer (taller) it is regarded as greater.

Class DemoStable:

Use Sedgewicks class Selection to sort the array of Rectangles. It will sort the Rectangles based on their natural order (by length). Print the sorted array of Rectangles. Make the output look like the expected output below

Expected Output at this point:

rectangles : [[4x2], [3x5], [4x3], [6x2], [3x4], [4x4], [6x4], [12x2], [2x4], [4x6], [2x12]] sorted by length : [[2x4], [2x12], [3x4], [3x5], [4x3], [4x4], [4x2], [4x6], [6x4], [6x2], [12x2]]

Notice: The rectangles are sorted by length but within the same length not by area. E.g. [4x4] is listed before [4x2]

In Class Rectangle:

Inside class Rectangle (after the methods) include a nested class. The class is called CompareByArea and it implements the interface Comparator

Here is the class header with an empty class body to help you get starteds:

private static class CompareByArea implements Comparator { // TODO 

} Implement method compare from the interface Comparator.

It should compare the Rectangles based on their area. To make the comparator available to other classes well include a constant right below the fields like this:

public static final Comparator BY_AREA = new CompareByArea(); Notice that class Rectangle provides now a Comparator BY_AREA just like class String provides CASE_INSENSITIVE_ORDER

Class DemoStable:

Modify the code in the main methode as described below: Change the method call from class Selection. This time use the overloaded sort method that accepts two arguments: the array and a comparator. It sorts the array by area. Update the label and print the array. Make sure to update the label, too.

Expected Output at this point:

rectangles : [[4x2], [3x5], [4x3], [6x2], [3x4], [4x4], [6x4], [12x2], [2x4], [4x6], [2x12]] sorted by area : [[4x2], [2x4], [4x3], [6x2], [3x4], [3x5], [4x4], [12x2], [6x4], [4x6], [2x12]]

Notice: The rectangles are sorted by area but within the same area not by length. E.g. [6x2] is listed before [3x4]

Now you are ready to use Merge sort and to demonstrate that it is stable.

Class DemoStable:

Expand on the main method by adding the following code:

Use Sedgewicks class Merge to sort the array of Rectangles. It will sort them based on their natural order (by length). Print the sorted array of Rectangles. Please use descriptive labels as shown below.

rectangles : [[4x2], [3x5], [4x3], [6x2], [3x4], [4x4], [6x4], [12x2], [2x4], [4x6], [2x12]] sorted by area : [[4x2], [2x4], [4x3], [6x2], [3x4], [3x5], [4x4], [12x2], [6x4], [4x6], [2x12]] sorted by length : [[2x4], [2x12], [3x4], [3x5], [4x2], [4x3], [4x4], [4x6], [6x2], [6x4], [12x2]]

Notice: The rectangles are sorted by length but within the same length the original order by area is preserved

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!