CJCoding With Joseph
15free / daySign inCreate account

A Comparator is a separate object that tells Java how to order two objects of some other class. Because it lives outside that class, one class can have several orderings. Product is provided and does not implement Comparable. Write two comparators for it.

Requirements: 1. ByPrice (the public class) implements Comparator<Product>. Its compare(Product a, Product b) puts the cheaper product first. 2. ByName (a second class in the same file, not public) implements Comparator<Product>. Its compare orders products by name from A to Z. 3. Each compare returns a negative number when a comes first, a positive number when b comes first, and zero when they tie. 4. Compare the prices with Double.compare. Subtracting two prices and casting to int would call 9.5 and 9.75 equal. 5. Do not change Product. The import line is already at the top of the file.

The tests sort an array with Arrays.sort(items, new ByPrice()) and then with new ByName().

Do NOT write a main method.

Expected output

Eraser (0.75)
Pen (1.5)
Notebook (4.25)
Stapler (12.0)
--
Eraser (0.75)
Notebook (4.25)
Pen (1.5)
Stapler (12.0)

= newline. The output ends with a newline, so print one after the last line.

Hints4 available
Topics
InterfacesClasses
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (sysout, psvm, fori)

Your output

Run your code to see the output here...

Test cases

Run your code to see test case results.