On this page:
4.1 Exam Conflicts/  DRC Accommodations
4.2 Defining equality
4.2.1 Getting started
4.2.2 List sameness
4.2.3 Tournament sameness
8.12

Lab 4: Equality and using double-dispatch🔗

Goals: Practice designing equality methods for union data types

Related files:
  javalib.jar     tester.jar  

4.1 Exam Conflicts/DRC Accommodations🔗

If you have conflicts with either of the Exams or have DRC-provided exam accommodations, please email me (blerner@ccs...) the relevant information with the subject CS2510A Exam Conflicts/DRC Accomodation. Please email me again even if you’ve already sent me your accommodation information; I’d rather have emails from you twice than miss your email by mistake.

4.2 Defining equality🔗
4.2.1 Getting started🔗

For this part of the lab we will work with classes that define publications. Start a new project and finish the following data definition, including examples of each type of publication:

;; A Publication is one of
;; - (make-article String Author Int)
;; - (make-book String Author Boolean)
;; - (make-webpage String Author)
(define-struct article [title author year-published])
(define-struct book [title author is-series?])
(define-struct webpage [title author])
 
;; An Author is a (make-author String Number)
(define-struct author [name year-of-birth])

Designing a sameness method

Define a method samePublication that will determine whether a given IPublication is the same as this publication. We may need such a method to find a desired article within some class that represents a library and everything it has access to. It may help to review the notes from Lecture 11 and Lecture 12 on how to design such methods.

Enhancing your data definition

Create a subclass of Article that represents a PressRelease. Press releases also contain the name of the company they’re promoting. Enhance your samePublication method above to produce the correct results with this new enhancement.

Do Now!

If you chose to use instanceof as the type-detector for your sameness method, did it continue to work with this extension? If you chose to use the “safe casting” approach described in the lecture notes (we skipped this in class, because it’s not a particularly useful technique!), did that work? If you chose to use double-dispatch, how much did you have to modify your code?

Do Now!

Explain why your implementation of sameness satisfies the four properties we need (reflexivity, symmetry, transitivity and totality).

4.2.2 List sameness🔗

Consider the standard ILoInt list of integers. Design a method sameList that checks whether the two lists contain the same numbers in the same order. Use double dispatch.

Extensions

Reinstate your SnocLo... and AppendLo... classes from the homework. Enhance your notion of sameness to include these lists, without using instanceof.

We know from the homework that the same list contents can now be represented by “structurally different” combinations of ConsLo..., SnocLo..., AppendLo... and MtLo... instances. We now have two potential notions of sameness:
  • Two lists are the same only if their structures are the same and their contents are the same

  • Two lists are the same if they have the same contents in the same order

Do Now!

Do both of these notions of sameness satisfy the four properties we need? Why or why not?

Decide which of these two notions of sameness is most appropriate, and write a comment in your code explaining why. Implement the second of these two notions.

4.2.3 Tournament sameness🔗

Consider the following representation of a sports tournament:

;; A Tournament is one of
;; - (make-game Team Tournament Tournament)
;; - (make-team String)
(define-struct team [name])
(define-struct game [winner game1 game2])

Two tournaments should be considered the same if they have the same winners and the same input games. There is no ordering between the two input games though, so your implementation of sameness should account for either ordering.

Do Now!

Does this notion of sameness satisfy the four properties we need? Why or why not?

Suppose that we refined our notion of a tournament to include seeds, or team rankings, such that game1 always represents the higher-ranked team, and game2 always represents the lower-ranked team. What notion of sameness should be implemented then?

Do Now!

Does this notion of sameness satisfy the four properties above? Why or why not?

Do Now!

Which notion of sameness implies the other? Make an example of a tournament such that the two notions of sameness produce different answers.

The notion of sameness for tournaments seems like a hybrid of sameness for lists and sameness for fractions: like fractions, it’s not purely structural, and simply checking the fields of the current object against the fields of the given object. It’s recursive and involving union data, like lists, but it’s not purely recurring over the data. Sameness is subtle.