Sorts rectangle objects

Assignment Help Basic Computer Science
Reference no: EM13673927

Consider the following program that sorts Rectangle objects:

public class SortDemo
{
public static void main(String[] args)
{
Rectangle rect1 = new Rectangle(5, 10, 20, 30);
Rectangle rect2 = new Rectangle(10, 20, 30, 15);
Rectangle rect3 = new Rectangle(20, 30, 45, 10);

// Put the rectangles into a list
ArrayList<Rectangle> list = new ArrayList<Rectangle>();
list.add(rect1);
list.add(rect2);
list.add(rect3);

// Call the library sort method
Collections.sort(list);

// Print out the sorted list
for (int i = 0; i < list.size(); i++)
{
Rectangle r = list.get(i);
System.out.println(r.getWidth() + " " + r.getHeight());
}
}
}

When you compile the program, you will get an error message. What is the error message? What is the reason for the error message?

- Error message is "Cannot find symbol - class Rectangle"
- Error is because there's no imports or references to a rectangle class

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, comp must be constructed as an object of some class that implements the Comparator interface.

What method(s) must that class implement? (Hint: Look up the Comparator interface in the API documentation.)

3.3. Implement a class RectangleComparator whose compare method compares two rectangles.

The method should return:
a positive integer if the area of the first rectangle is larger than the area of the second rectangle
a negative integer if the area of the first rectangle is smaller than the area of the second rectangle
0 if the two rectangles have the same area

Part of the code has been provided for you below:

import java.util.Comparator;
import java.awt.Rectangle;

public class RectangleComparator implements Comparator<Rectangle>
{
/**
Compares two Rectangle objects.
@param r1 the first rectangle
@param r2 the second rectangle
@return 1 if the area of the first rectangle is larger than the area of
the second rectangle, -1 if the area of the first rectangle is
smaller than the area of the second rectangle or 0 if the two
rectangles have the same area
*/
public int compare(Rectangle r1, Rectangle r2)
{
. . .
}
}

3.4. Write a test program that adds the three rectangles below to a list, constructs a rectangle comparator, sorts the list, and prints the sorted list and the expected values.

Rectangle rect1 = new Rectangle(5, 10, 20, 30);
Rectangle rect2 = new Rectangle(10, 20, 30, 15);
Rectangle rect3 = new Rectangle(20, 30, 45, 10);

What is your test program?

3.5. A very specialized class, such as the RectangleComparator, can be defined inside the method that uses it.

Reorganize your program so that the RectangleComparator class is defined inside the main method of your test class.

What is your main method now?

Reference no: EM13673927

Questions Cloud

Create a gui application with jframe : Create a GUI application with JFrame that contains five labels describing reasons that a customer might not buy a specific product (e.g. "too expensive"). Place a JButton on the JFrame, and code its functionality so that every time the user cl..
Write an essay paper addressing what is coso : Write an essay paper addressing what is COSO
The database specification : The database specification
Patient care applications : Patient care applications
Sorts rectangle objects : Sorts Rectangle objects
Get in the way of leading a more fulfilling life : Do soft addictions "get in the way of leading a more fulfilling life"?
Explain the difference between quoting and paraphrasing : Explain the difference between quoting and paraphrasing. Which needs proper documentation
Cultural norms geographic location and ethics : Cultural Norms, Geographic Location, and Ethics
Describe the character of chines girl : Describe the character of chines girl (Ni Kan ) from the short story Two Kinds by Amy Tan.

Reviews

Write a Review

Basic Computer Science Questions & Answers

  Explaining project manager-s role in project management

Describe in scholarly detail project manager's role in project management and job responsibilities related with position.

  Describe two different kinds of communication software

Is Web search a database query? Explain your answer.

  Describe operating model for business process integration

Describe the chosen operating model in terms of business process integration. Compare the selected organization to the sample organizations using the chosen operating model in terms of business process integration.

  Identify as integers from the input file

Design and implement a program that counts the number of integer values in a text input file. Produce a table listing the values you identify as integers from the input file.

  Write program will predict the size of population organisms

Write a program that will predict the size of a population of organisms

  Write the code to compute s on the basis of these values

In physics, a common useful equation for finding the position s of a body in linear motion at a given time t, based on its initial position s0, initial velocity v0, and rate of acceleration a, is the following:

  Write a segment of java code that will rotate the elements

Write a segment of Java code that will rotate the elements of an array by one position, moving the initial element to the end, like.

  Returns the bottom of the stack instead of the top

This function returns the bottom of the Stack instead of the top

  Describe the necessary hardware and software components

Your company wants to create an application that allows employees to dial in from a remote location and, using a single connection, access their voice mail, e-mail, and data files.

  Proposed dss design

Proposed DSS Design

  Use the raptor to complete the problem

Use the Raptor to complete the problem, Input names of students from the user, terminated by "ZZZ", 0, 0, 0, and create a data file grades with records of the following form: student (string), test (integer), test 2 (integer), test 3 (integer)

  What is the worst-case cost of finding records

What is the worst-case cost of finding records

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd