Eboard 09 (Section 2): Dictionaries and Associative Arrays

You are probably being recorded (and transcribed) [at least if Sam is here and can get the technology working]

Approximate overview

  • Preliminaries
    • Notes and news
    • Upcoming work
    • Tokens
  • MP4 / Design challenges
  • Questions
  • Set up for MP4
  • Work on MP3

Preliminaries

News / Etc.

  • I’m still here. I’m unsure about whether I’ll be here on Tuesday.
  • Some time today, you should receive an invitation to join a testing repository on GitHub. Please make sure to accept that invitation.
  • If I don’t respond to email within 24 hours, it’s probably lost in my mailbox. Email again.
    • Similar for DM.

Struggles

Comment from student: “I feel like I’m struggling.”

  • Many of you are. It’s the nature of the beast. Semi-(un)cooperative tools and a (fill-in-adjective) professor don’t help.
  • Survey!
    • I feel like I’m struggling a lot in 207 [6]
    • I feel like I’m struggling a bit in 207 [10]
    • I don’t feel like I’m struggling in 207 [4]
  • I worry that folks aren’t asking enough questions. You really shouldn’t spend more than five or ten minutes stuck on a problem before you reach out for help.
    • For example, I hear many people had difficulty with “the parameters can appear in (almost) any order on MP1”. Yet I received no questions on that issue.
    • If you’re struggling, someone else is also struggling, and asking on the Questions channel will help them.

Upcoming work

Tokens

If you’d like to suggest token events, please let me know in advance of class.

Academic/Scholarly

  • Thursday, 2024-09-26, 4:00–5:00 p.m., Science 3821 (I think). CS Extras: Study abroad for CS majors
  • Sunday, 2024-09-29, 7:00–8:00 p.m., Science 3819. Mentor Session
  • Tuesday, 2024-10-01, Noon–1:00 p.m., JRC 224A (Day PDR). CS Table

Cultural

  • Friday, 2024-09-27, 7:00–9:00 p.m., Wall Theatre. Neverland Players
  • Saturday, 2024-09-28, 2:30–4:30 p.m., Wall Theatre. Neverland Players
  • Saturday, 2024-09-28, 7:00–9:00 p.m., Wall Theatre. Neverland Players
  • Sunday, 2024-09-29, 2:30–4:30 p.m., Wall Theatre. Neverland Players
  • Tuesday, 2024-10-01, 11:00a.m.–Noon, Bucksbaum 131. Royce Wolf Recital

Multicultural

  • Friday, 2024-09-27, 4:00-5:00 p.m., HSSC N1170 - Global Living Room. Middle of Everywhere (Nicaragua)
  • Tuesday, 2024-10-01, 1:00–2:15 p.m., Steiner 205. Crip SpaceTime watch party

Peer

Wellness

  • Tuesday, 2024-10-01, 4:30–6:30 p.m., BRAC P103 (Dance Studio). Wellness Yoga
  • Tuesday, 2024-10-01, 4:30–6:30 p.m., ???. Forest Bathing (signup required)

Misc

  • Friday, 2024-09-27, 7:30–10:00 p.m., Central Park. Dessert and Dance Reception for Renfrow Hall
  • Saturday, 2024-09-27, Noon–1:00 p.m., Renfrow Hall. Renfrow Hall Dedication
  • Friday, 2024-10-04–Sunday, 2024-10-06. JRC 101. Pioneer Weekend.
    • Like a hack-a-thon. Spend the weekend developing an idea and a corresponding plan. Get money. Get support for carrying out your project. Get something on your re’sume’.

Other good things (no tokens)

  • Friday, 2024-09-27, 5:30–8:30 p.m., Natatorium. Men’s and Women’s Swimming Time Trials
  • Friday, 2024-09-27, 6:00–8:00 p.m., Darby Gym. Volleyball vs. Central
  • Saturday, 2024-09-28, 11:00 a.m.–1:00 p.m., Springer Field. Men’s Soccer vs. Lake Forest
  • Saturday, 2024-09-28, 1:30 p.m.–3:30 p.m., Springer Field. Women’s Soccer vs. Lake Forest
  • Monday, 2024-09-30, 8:00–10:00 p.m., Bob’s Underground. Open Mic Night
  • Tuesday, 2024-10-01, 4:30–6:30 p.m., Springer Field. Men’s Soccer vs. Knox
  • Tuesday, 2024-10-01, 7:00–9:00 p.m., Darby. Volleyball vs. Beloit
  • Wednesday, 2024-10-02, 4:00–6:00 p.m., Springer Field. Women’s Soccer vs. Knox
  • Friday, 2024-10-04, 5:30–8:30 p.m., Natatorium. Scarlet and Black

Design Experience

TLAs

  • ADT: Abstract Data Type
  • SAM: Strategy, Applications, Methods (for designing ADTs)
  • AAA: Arrangement, Algorithms, Analysis (for designing Data Structures)
  • TLA: Three letter acronym

The ADT (SAM), which we’ll call “Dictionary”

We are designing an ADT similar to the association lists or hashes that you may have learned in CSC-151.

Strategy

Generalize arrays so that we can use “any” comparable object as an index rather than just integers. (We use the term “key” rather than “index” just to be confusing.)

Applications

  • A literal dictionary: We look up descriptions of words based on the word.
  • Math genius: Input expressions, get back value
    • Maybe not. Here, we’re doing calculations. We’re thinking of Dictionaries as storing values.
  • Phone book: Keys are names, values are phone numbers.

Methods

public class Dictionary<K,V>

Think about parameter types, return types, exceptions.

  • public void set(K key, V val) throws NullKeyException - sets the value assigned to key.
    • Question: What happens if the key doesn’t already exist? Design decision: Add it.
    • Question: What happens if the key already exists? Design decision: Replace the value associated with the key.
  • public V get(K key) throws NoSuchKeyException, NullKeyException - get the value with the given key.
  • public void remove(K key) throws NullKeyException - remove entry with the given key. If there is no such entry, does nothing.
  • public boolean hasKey(K key) throws NullKeyException - determine if the key exists.

More methods (from the assignment)

  • public String toString() - Build a string of the form “{key:value, key:value, key:value, … key:value}”.
  • public Dictionary<K,V> clone() - Make a copy of the dictionary.

Even more methods

  • public void swap(K key1, K key2) - swap the values associated with key1 and key2. (Beyond the basics.)
  • public void um(void) - Pauses.
  • public void updateKey(K oldkey, K newKey) - change a key.
    • set(newKey, get(oldKey)) + remove(oldKey)
  • public void equals(Dictionary<K,V> other) - check if the other dictionary has exactly the same set of key/value pairs as this dictionary.

Things we don’t want

  • sort

Notes

  • Dictionaries are also called “Tables”, “Maps”, “Hashes” (even though they aren’t always implemented as hashes), and some other things.
  • We will make our dictionaries dynamic; they can always fit another thing (unless we run out of memory, but that’s a RuntimeException).
  • We’ve learned about Generics, so we can use them here. Yay!
  • We will assume key1 and key2 are equal if key1.equals(key2), which is the Java standard.

The data structure (AAA): Associative Arrays

Arrangement

See whiteboard or assignment.

  Pair<K,V> associations;
  int size;

Algorithms

get(K key) - go through the array, and for each element, see if the key in the pair is equal to the key. If so, return the value. If we run out of pairs, through a NoSuchKeyException.

set(K key, V val) - go through the array. For each elment, see if the keys match. If so, replace the value. If we run out of elements, add a new pair. (Perhaps use hasKey to help.) When adding a new pair, we may have to grow the array.

  • How do we grow an array? Arrays.copyOf(T[] original, int newLength)
  • How do we initialize the array? I forget. But I’ve given you the code. in the assignment. Here’s an approximation of one technique.
  public AssociativeArray<K,V> {
    Pair<K,V> associations;

    @SuppressWarnings
    public AssociativeArray() {
      this.associations = new (Pair<K,V>) new Object[BASE_SIZE];
      this.size = 0;
    } // AssociativeArray
    
  } // AssociativeArray<K,V>

boolean hasKey(K key) - Loop through every single value. If we find a matching key, return true. If you finish looking at everything, return false.

void remove(K key) (v1) - Loop through every value. If we find a matching key, you should have an index. Copy over every element but that one into a new array.

void remove(K key) (v2) - Loop through every value. If we find a matching key, you should have an index. Shift everything up starting at that index.

void remove(K key) (v3) - Loop through every value. If we find a matching key, you should have an index. Put a null there. [Sam’s year’s of experience suggest that putting nulls in the middle may complicate our lives.]

void remove(K key) (v4) - Loop through every value. If we find a matching key, you should have an index. Put the last thing there. (The last thing is at index size-1.

Analysis

Linear: Does this grow directly with the number of elements in the AA?

Constant: Is it independent of the number of elements in the AA?

get - Linear: We have to look at the key/value pairs, and may potentially look at all of them.

set - Linear: We have to look at the key/value pairs, …

hasKey - Linear: We have to look at the key/value pairs

remove (v2) (shift) - Linear: We have to look at the key/value pairs. We also have to shift potentially all the key/value pairs.

remove (v4) (moves the last thing to the right place) - Linear: We have to look at all the key/value pairs. But we only have constant work once we’ve found it.

  • In theory, v2 and v4 are equivalent. In practice, v4 is faster.

Notes

  • Your classmate said something very important in describing get.
    • We’re doing the same thing (or nearly the same thing) in each of the four main methods. You shouldn’t repeat the code. (DRY) So …. we should write a helper.
  • Another classmate made a helpful followup when discussing remove.
    • The helper should return the index of the matching key.

The assignment

See the assignment and the repo.

Testing

Write tests early. Pull other tests. Tell me when you add tests.

Questions

Will we have merge conflicts?

I hope not. But part of the point is to get you used to working on a large collaborative repository.

How are we going to work with two repositories?

The testing repository gets cloned into the middle of the main repository. GitHub + VSCode are smart enough to deal with two overlapping repos.

You will only turn in the code for AssociativeArrays.

Questions

Questions on SoLAs

Will you add tips for each LA so that we don’t go in the wrong direction?

Part of the broader goal of the course is that you develop skills at figuring out how to describe your skills.

But yes, I will try to add tips. I may also add some verbally in class (or ask you to come up with some).

If we are just having a reading in composition, why do we have to answer an LA on that?

Because you’ve been dealing with the more basic form of composition since you started writing objects, and the LA only asks you to explore the more basic form of composition.

Also: You don’t have to answer tha LA. You can hold off another week or more.

Questions on MPs

I’m spending significantly more than five hours on mini-projects. Any suggestions?

Ask for help (preferably on the Questions channel) after you’re stuck for five minutes. Generally, if you can’t solve it in five minutes, you also won’t solve it in thirty minutes.

Will the redos for the mini-projects be posted soon?

Yes.

How many group mini-projects will there be?

Probably between three and four.

Do both partners need to turn in a token for a late group mini-project?

Yes.

Other administrative questions

Will we get grade reports soon?

Yes. Probably this weekend.

Will we get other grades before the grade report?

Probably.

Questions on MP3

What is a “good commit”?

It has a good description.

It’s not “I committed everything”.

Smaller is better.

Does it count if the commit is on a branch?

Yes.

We couldn’t push and pull. Why?

Next time, send me the message.

If someone has changed the repo, you’ll need to pull before you push.

When you pull, you may get merge conflicts. If you do, read through the file that has the merge conflicts, choose which code to keep, add and commit.

What is going on with the git fetch upstream?

You are working with a fork of another repository.

That other repository may be updated (e.g.,when Sam realizes he screwed up Grid.eqv or Sam adds more tests).

You need a way to upll the updates from the primary repository into your fork (and then into your clone).

Two mechanisms.

Click the magic button on GitHub.

Create an upstream link and type magic commands.

Questions on the readings

Is the way inheritance works similar to the way that pointers do?

Inheritance requires pointers (or “references”) behind the scenes, but it’s more the concept that you automatically delegate method calls to the parent class, which I’d say is independent of pointers.

What should I do if I want to inherit from three classes, ex SmileStudentTeacherChef? (I felt composition + inheritence could only deal with 2 parent classes.)

Use composition. Composition can handle an arbitrary number of delegated classes. It’s just a bit more work on your end.

Other questions

MP4 Setup

Please follow the setup instructions for MP4.

MP3 Work Time

If you like, talk to your parter about MP3.