Question: 9.29 M03-ACT02-Ordering and Comparators Objective: In this activity, you will create a class to represent sortable orders placed online at the Acme international shipping company.

9.29 M03-ACT02-Ordering and Comparators

Objective: In this activity, you will create a class to represent sortable orders placed online at the Acme international shipping company. You will practice implementing interfaces, as well as the equals and hashCode methods.

Step 1: The AcmeOrder class

The class should store the following private fields:

orderWeight (of type double)

orderYear (of type Year)

orderItems (of type List)

Next, add a constructor that takes:

the order weight

the order year

a list of items (of type: List)

Initialize each field of the class in the constructor (no getters or setters are needed in this activity).

Lastly, override the toString() method such that AcmeOrder objects can be rendered like so:

(w=[orderWeight], yr=[orderYear], items=[orderItems])

Step 2: Implementing Comparable

To make lists containing AcmeOrder objects sortable via Java's Collections.sort(..) method, make the class implement Comparable and override the required compareTo(..) method.

We'll define the natural ordering for AcmeOrder objects to be based on both orderWeight and orderYear. Here is some pseudocode to help guide your implementation of the compareTo method:

Note: you can test to see if one Year object a comes before another, b by writing: a.isBefore(b) (there is also an isAfter(..) method)

logic for compareTo(o): // <- "o" as in "other" AcmeOrder object if this order's weight < o's order weight AND this.orderYear comes before o.orderYear then return some negative number else if this order's weight > o's order weight AND this.orderYear comes after o.orderYear then return some positive number else return zero (i.e.: this and o are considered equivalent in terms of ordering) 

Step 3: Implementing Equals(..), and hashCode()

Next, override the equals(..) and hashCode() methods in the AcmeOrder class. We'll consider the orderYear, orderWeight, and orderItems fields to be significant -- meaning these fields should factor into the logic you place in your equals method.

As discussed in the class notes on Equals and HashCode, your implementation of hashCode should just call the Objects.hash(..) utility method -- passing in each significant field. Return the result of this call and that will serve as the hash value for this object.

The (read-only) tester class will instantiate some order objects, test your equals method, and sort a list of orders using Collections.sort(..).

Handin: Be sure that you're on submit mode; the last submission you make before the deadline will be the one we grade.

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!