Product and LineItem

Product and LineItem

In this exercise, you'll add an equals method to the Product and LineItem classes that you can use to compare the instance variables of two objects.

1.  Open the porject named ch08_ex4_EqualsTest in the ex_starts directory. This application creates an compares two Product objects and two LineItem objects using the equals method. Review this code to see how it works.

2.  Run the project. Since the equals method isn't overridden in the Product or LineItem class, the output from this application should indicate that the comparisons are based on object references and not the data the objects contain.

3.  Open the Product class, and add an equals method like the one shown in figure 8-15. Then, run the project again. This time the output should indicate that the products are being compared based on their data and not their references.

4.  Repeat step 3 for the LineItem class. This time, the comparisons for both the products and line items should be based on their data.

Powered by