On this page:
Intro
Recall
Zipper with Focus
Adding functionality:   Repeat
Adding functionality:   Randomize
6.12

Lab 21: Focus with Zippers

Intro

You’ll work in this lab with your 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.

Recall

During last Friday’s lecture we implemented a ListZipper. This data structure allowed us to perform fast iteration in any direction inside a list. We’ve had experience with zippers last semester, when we implemented editable textboxes in the student languages.

Zipper with Focus

Today we’re going to implement a slight variation on a ListZipper: a FocusZipper.

Consider the textbox: the cursor sits between two lists of characters. As you enter text the context is extended with additional characters. You can easily move backward and forward through the text, and at any point the cursor is between two elements in the zipper.

Now consider a playlist of music: there are songs that have already played, one song currently playing, and songs that have yet to play. We can play the previous song with a single step and we can play the next song with a single step, but there is always one song currently playing.

While a list zipper places the focus between two elements, a focus zipper places the focus on a particular element.

Ex 1: Implement the constructor of a FocusZipper. It must set the focus on the first element of the given list and throw a runtime exception if there are no elements in the given list.

Ex 2: Design the methods FocusZipper.left() and FocusZipper.right(). If there are no more elements to the left or right (resp.), you should return the current FocusZipper.

Ex 3: Design the methods FocusZipper.start() and FocusZipper.end(), which focus on the first and last elements in the zipper, resp.

Adding functionality: Repeat

A common bit of functionality in a playlist is a repeat option, which will restart the playlist from the first song once the last song has ended.

Ex 4: Design the methods FocusZipper.leftCycle() and FocusZipper.rightCycle(), which work similarly to the left and right you’ve already implemented. Instead of stopping at the first and last elements in the zipper (resp.), these methods should loop to the other side of the zipper.

Adding functionality: Randomize

Another common bit of functionality in a playlist is a random option, which will randomly choose songs in the playlist to play. We need a few more helper methods to implement a random song choice.

Ex 5: Design the method FocusZipper.count(), which returns the Integer count of all the elements in zipper.

Ex 6: Design the method FocusZipper.nth(Integer n), which shifts focus in the zipper n elements to the right of the current zipper. For simplicity, assume that n is non-negative and use rightCycle in your implementation.

Ex 7: Implement the method FocusZipper.random(), which shifts focus to a random element in the zipper.