Assignment 4
Due Monday, May 7



This assignment introduces you to programming in Scala and to some of its more interesting features.

Step 1

Download and install the Scala system from here.

If you use emacs, there is a nice Scala mode available here. Follow the instructions in the README file.

Step 2

Review the following materials found at the Scala web site:

Also, review the Scala code that I discussed in class on April 16 and 23, which is posted on the course web page.

In the following steps, you will be writing Scala code. Be sure to use Scala's pattern matching facility wherever possible.

Step 3

In the Scala API, there is a trait called Ordered. Navigate from the API page to the page for Ordered by typing "Ordered" in the search box in the upper left and clicking on the link for Ordered that appears. Review, at the bottom of the Ordered page, the "Abstract Value Members" and the "Concrete Value Members". A class implementing the Ordered trait will only have to define abstract value members, namely just the compare() method. The concrete value members (e.g. <, ≤, etc.) are already defined in terms of compare(). Click on "compare" to see what the behavior of compare() should be.

Define a class, however you like, that implements the Ordered trait, allowing two objects of your class to be compared to each other. Be sure that your class has the toString() method defined in a way that makes printing of objects of your class looks reasonable.

Step 4

In a singleton class called Sort (i.e. a class defined by "object Sort {...}"), define the following the following generic methods, each one parameterized by a type A that implements the Ordered trait.

Step 5

Define a generic class SortedList, parameterized by a type E that implements the Ordered trait, that represents a kind of list where the elements, of type E, are always in sorted order. The constructor should take an unsorted List[E] as a parameter. The SortedList class should define (at least) the methods below. Notice that none of the methods actually modify the "this" object.

Don't forget, a SortedList must remain sorted at all times! Be sure to use the methods defined by your singleton class Sort where possible.

Step 6

Define a singleton class containing a main method that fully tests your SortedList class. To do so, you will need to created SortedList's whose elements are objects of the class you defined in Step 3 above. Be sure that your main method uses for-comprehensions that result in the map, filter, flatmap, and foreach methods of the SortedList class being called.

Submitting your work

Send an email message to Prof. Goldberg (goldberg@cs.nyu.edu) that has the subject line "OOP Assignment 4". Attached to the message should be a zipped folder containing the following:

The zipped folder should have your name on it. Please do not put any .class files or example files in the folder.