lab05 : Sorting

num ready? description assigned MW lect due MW lect assigned TR lect due TR lect
lab05 true Sorting Wed 02/05 12:30PM Thu 02/13 11:59PM Tue 02/04 11:00AM Thu 02/13 11:59PM

In this lab:

Step-by-Step

Step 0: Set up your repo

Create your repo

Clone this empty repo into your ~/cs56 directory, or wherever you prefer to work.

Also clone this repo as a “sibling” (i.e. side by side in the same directory) with your lab05 repo:

Your starter code will be your lab03 repo, with a small number of additional files from: https://github.com/ucsb-cs56-w20/STARTER_lab05. So we’ll define a remote for your lab03 repo:

git remote add lab03 git@github.com:ucsb-cs56-w20/lab03-cgaucho1-ldelplaya22.git

Pull from your lab03 repo into your lab05 repo, and then push to github.

git pull lab03 master
git push origin master

Now, we’ll copy a few files from the https://github.com/ucsb-cs56-w20/STARTER_lab05 repo into your new lab05 repo. That’s described in Step 1 below.

Step 1: Copy code for a new class and a new set of tests into your repo.

Though there is starter code for this week, it is not a full repo; instead it has just a few that you need to copy to their proper spots. In that repo, https://github.com/ucsb-cs56-w20/STARTER_lab05, you’ll find these files:

File Where to copy it in your lab05 repo
Menu.java src/main/java/edu/ucsb/cs56/pconrad/menuitem
MenuTest.java src/test/java/edu/ucsb/cs56/pconrad/menuitem

It is important that you copy Menu.java under src/main/java/… and MenuTest.java under src/test/java/…—if you don’t, you’ll have trouble compiling the test code. You’ll get errors saying that org.junit is not defined, etc. So pay attention to this detail.

To copy these to their proper spots, you could do any of the following. How you get the file there is up to you. At this point, you will be expected to have the skills to do that, but if you need some suggestions, here you go:

  1. If you cloned this repo to another directory (not inside of your lab05 repo directory, but as a sibling, for example), then you can use the Unix cp or mv file to copy the file into it’s proper spot.
  2. Another approch is to use the “raw” tab on the github site to expose a version of the file that doesn’t have any extra formatting (line numbers, etc.). Then any of the following techniques:
    • Use “save as” in your web browser to save a copy into the correct directory
    • Copy the URL, then cd into the target directory, and use wget url to get the file.
    • Open the target filename in an editor as an empty file, and use copy/paste to paste in the contents.

Step 2: Start writing code to make tests pass

In the previous lab, lab03, you implemented several methods of a class called MenuItem that represents item on a restaurant Menu. Now, we will implement the Menu class. The details about the Menu class appear below.

In lecture, we recently discussed java.lang.Comparable, java.util.Comparator, and Java lambda expressions. You’ll need that information. If you missed it, read this article, which outlines what we covered:

Just like in the previous lab (lab03), note that the starter code:

I suggest that you work in this order:

Additional Hints:

  1. You may find it helpful to declare private instance variables for these Comparator<MenuItem> objects:

    private Comparator<MenuItem> byPrice = ...;
    private Comparator<MenuItem> byName = ...;
    private Comparator<MenuItem> byCategory ...;
    

Note that the ... needs to be replaced by a lambda expression that sorts by that part of the class.

  1. You may find it helpful to fill in the csv() function with code that loops through the list of menu items, and produces one line of the comma separated output. You can see the expected format in the test cases. Note that you have to return this output; it does you no good to print it.

  2. Now each of the methods is essentially a call to sort, using your comparators, followed by a call to the csv() method.

  3. A reminder that comparators can be composed as follows, assuming that byThis and byThat are both references to Comparator<T> and mylist implements List<T>:

    java.util.Collections.sort(mylist, byThis.thenComparing(byThat));
    

    You can also reverse a comparator by putting .reversed() after it:

    java.util.Collections.sort(mylist, byThis.reversed());
    

    And this can be continued as deep as you need it to be:

    java.util.Collections.sort(mylist, byThis.thenComparing(byThat.reversed()).thenComparing(bySomethingElse));
    

Details about methods of Menu

The methods for Menu are as follows:

Modifier and Type Method Description
void | add(MenuItem mi) add a menu item to the menu (to the wrapped ArrayList<MenuItem>)  
String csv() Produce a listing of each item in csv format, with newlines between each item. Order is whatever order the items are currently in the ArrayList
String csvSortedByName() same as csv(), but the items should be sorted in lexicographic order by name.
String csvSortedByCategoryThenName() same as csv(), but the items should be sorted by category. With the same category, the items should be sorted by name.
String csvSortedByCategoryThenPriceDescendingThenByName() same as csv(), but the items should be sorted by category. With the same category, the items should be sorted by name.
String csvSortedByPriceThenName() same as csv(), but the items should be sorted by price, from lowest to highest. When more than one items has the same price, the items of the same price should be sorted by name.

Step 3: Checking Test Case Coverage

As in previous labs, be sure that you’ve:

Then, check your test coverage:

Some of the points in the manual inspection may be awarded on the basis of having good test coverage.
While 100% test coverage is not always the goal, in this particular exercise, it should be possible.

So if you see that you don’t have 100% test coverage, go back and write some additional unit tests.

For a review of how to read the test coverage reports provided by Jacoco, see: https://ucsb-cs56.github.io/topics/testing_jacoco_reports/

End of description for lab05