Lab 12: JSON Equality
Intro
You’ll work in this lab with your recently-assigned lab partners.
The two of you will work as a team to solve problems. At any time, one of you will be the Head and the other will be the Hands. The Head does the thinking and the Hands does the typing. Hands type only what the Head tells them to, but you’re free to discuss any issues that pop up. You should switch off during the lab to make sure each of you get practice problem solving, dealing with syntax, and getting finger exercises on the keyboard.
You should start this lab with this project skeleton. Unzip the file into your IdeaProjects directory and open it with IntelliJ to get started.
JSON Reminder
Recall the data definition for JSON from last semester (translated a bit to Java):
/* A Json is one of: |
* - new JInt(Integer) |
* - new JStr(String) |
* - LoJson |
* - Assoc |
* Interp: JSON data |
*/ |
|
/* A LoJson is one of: |
* - new Mt() |
* - new Cons(Json first, LoJson rest) |
* Interp: A list of Json data |
*/ |
|
/* An Assoc is one of: |
* - new MtAssoc() |
* - new ConsAssoc(String key, Json value, Assoc rest) |
* Interp: associations between string keys and Json values |
*/ |
We’ll be implementing an equals method for the provided JSON classes. The overall structure will be similar to the sameShape method implemented during Monday’s lecture.
Simple Equality
We want to implement the structural equality for each class that extends the abstract class Json.
As shown in lecture, the equals method itself should be quite simple. Each of the implementing classes JInt, JStr, Mt, Cons, MtAssoc, and ConsAssoc should have the following as their equals implementation:
public Boolean equals(Json j) { |
return j.isEqual(this); |
} |
The isEqual method in each of those six classes does the real work.
Ex 1: Implement the six base-case isEqual methods, one for each of the possible specific arguments in JInt, JStr, Mt, Cons, MtAssoc, and ConsAssoc.
For example, the isEqual(JInt ji) method is provided in the skeleton.
public Boolean isEqual(JInt ji) { |
return false; |
} |
Ex 2: Implement the six isEqual methods, one in each of the six JSON classes.
Beyond Simple Equality
In real-world JSON, the order of elements in both regular and association lists does not matter.
Ex 3: Implement an order-independent isEqual method for Cons and ConsAssoc using the length, exists, and forAll methods.