182k views
4 votes
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:

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)
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).

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.

Tester.java class:

import java.time.Year;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Tester {

public static void main(String[] args) {

AcmeOrder o1 = new AcmeOrder(3.4,
Year.of(2019), List.of("cereal", "milk", "butter"));

AcmeOrder o2 = new AcmeOrder(1.2,
Year.of(2017), List.of("book"));

AcmeOrder o3 = new AcmeOrder(0.5,
Year.of(2009), List.of("magazine1", "magazine2", "magazine3"));

AcmeOrder o4 = new AcmeOrder(30,
Year.of(2021), List.of("old-tv", "plasma-tv"));

AcmeOrder o5 = new AcmeOrder(7,
Year.of(2019), List.of("plasma-tv"));

AcmeOrder o6 = new AcmeOrder(7,
Year.of(2019), List.of("plasma-tv"));

// wrapping the List.of(..) call inside of new ArrayList<>(..).. this is to make the resulting
// list mutable (since it needs to get mutated in the Collections.sort(..) call)
List orders = new ArrayList<>(List.of(o1, o2, o3, o4, o5, o6));

System.out.println("before sorting: ");
for (AcmeOrder o : orders) {
System.out.println(o);
}
System.out.println("------------------");
Collections.sort(orders);

System.out.println("after sorting: ");
for (AcmeOrder o : orders) {
System.out.println(o);
}

}
}
AcmeOrder.java class:

import java.time.Year;
import java.util.List;
import java.util.Objects;

// todo: AcmeOrder class goes here

1 Answer

4 votes

Answer: step 1 hope that helps

Step-by-step explanation:

User Hypaethral
by
8.0k points