jpa06 : ArrayList, Sorting, Comparators, Lambdas
num | ready? | description | assigned MW lect | due MW lect | assigned TR lect | due TR lect |
---|---|---|---|---|---|---|
jpa06 | true | ArrayList, Sorting, Comparators, Lambdas | Mon 11/02 05:00PM | Mon 11/09 11:00PM |
This assignment is jpa06, i.e “Java Programming Assignment 04”.
If you find typos or problems with the lab instructions, please report them on the #help-jpa06 channel on Slack
In this lab:
- using
ArrayList
- sorting with Comparators
- more practice with writing JUnit tests
Preparation
Before starting this lab, you should understand how to use the built-in methods for sorting in Java, which rely on an understanding of:
- interfaces
java.lang.Comparable
java.util.Comparator
These concepts are covered in these online exercises and videos:
- Lecture Video from W21: Lecture 02-03, Wed
- Article that goes with Lecture Video: https://ucsb-cs156.github.io/topics/java_sorting/
- Code that goes with Lecture Video: https://github.com/UCSB-CS56-pconrad/java-sorting-and-comparators
- Exercises (Updated with links to code)
- Lecture Videos from F20 (may not be accessible yet, but I’m working on it)
- Lecture Video from Tue 10/27 on Gauchocast that goes with ex14 and ex15.
- Lecture Video from Thu 10/29 on Gauchocast that goes with ex16 and ex17.
Working in a pair?
You have the option to work in a pair on this assignment.
To set up a pair repo, see these instructions: Pair Repo Setup.
In addition:
- Read about Strong Style Pairing and consider trying it.
- If you are working in aSwitch navigator/driver frequently and tradeoff who commits
If you are in your repo directory, and type git log
at the command
line, you’ll see a list of the commits for your repo.
Record that you are pairing on each commit message by putting the initials of the pair partners at the start of the commit message.
E.g. If Selena Gomez is driving, and Justin Timberlake is
navigating, and you fixed a bug in your getDanceMoves()
method, your
commit message should be SG/JT fixed bug in getDanceMoves()
We should see frequent switches between SG/JT and JT/SG.
When remote pairing, this means switching who is sharing their screen:
- The person driving pushes the repo.
- You switch who is sharing the screen
- Then, the other person pulls from the repo.
Step-by-Step
Step 0: Set up your repo
When cloning your repo, be sure to use only the ssh links.
- Using the
https
link may result in an error when you try to push the to the repo, indicating that “a GitHub Action cannot be accessed from an OAuth login” or something like that. - Using ssh instead of https gets around this problem.
You may work individually or as a pair on this lab. However, if you work as a pair, please:
- Pair with someone from your same team, or at least, from your same discussion section.
- Remember to name the repo correctly, and also to add your pair on Gradescope each time you submit
The starter code is in https://github.com/ucsb-cs156-w21/STARTER-jpa06. Visit that page for the approrpiate URL to add the starter
remote.
To add the starter as a remote, cd into the repo you cloned, then do:
git remote add starter https://github.com/ucsb-cs156-w21/STARTER-jpa06
Then do:
git checkout -b main
git pull starter main
git push origin main
That should get you set up with the starter code.
Step 1: Copy code from your jpa01
submission.
This lab builds on your jpa01
submission. In jpa01
, there were two classes in the starter code, both inside the package edu.ucsb.cs56.pconrad.menuitems
:
Class Name | File Name |
---|---|
MenuItem |
src/main/java/edu/ucsb/cs56/pconrad/menuitems/MenuItem.java |
MenuItemTest |
src/test/java/edu/ucsb/cs56/pconrad/menuitems/MenuItemTest.java |
Note that in the next project, these files go in a package called edu.ucsb.cs156.pconrad.menuitems
(changing cs56
to cs156
). So, as you copy these files over, please change the package
line at the top of the file accordingly.
Your starter code for jpa06 contains these same two files, but the “original” versions
from the jpa01
starter (with the package name changed from cs56
to cs156
).
You should copy your code for both MenuItem.java
and MenuItemTest.java
from your jpa01
directory into the corresponding file in your new jpa06 directory.
Note that there are also new classes Menu.java
and MenuTest.java
. Leave these alone for now;
you’ll be modfiying those as part of this programming assignment.
Commit the change to the files MenuItem.java
and MenuItemTest.java
. As you commit the change, consider the information below about good commit messages.
Write a good commit message
As you commit the change, start now with the habit writing meaningful commit messages. Starting from this assignment, you may be occasionally graded on a random sample of your commit messages, with the initial rubric consisting of these parts:
- Is the commit message accurate?
- Is the commit message understandable?
- Does it convey what changed and why?
A good commit message in this case would be something like this, where xy
are your initials:
git commit -m "xy - replace MenuItem and MenuItemTest classes with solutions from jpa01"
Reminder of Maven Commands
Here is a reminder about the commands you’ll need as you work with the code. Try them out now.
To do this | Type this command | Notes |
---|---|---|
compile the code | mvn compile |
|
reset everything | mvn clean |
|
run the tests | mvn test |
|
generate a report of test coverage | mvn test jacoco:report |
Open the file target/site/jacoco/index.html in a browser to read the report |
generate a jar file | mvn package |
|
generate a mutation test report | mvn test org.pitest:pitest-maven:mutationCoverage |
Open the file target/pit-reports/yyyymmddhhmm/index.html to see the report; note that yyyymmddhhmm will be replaced by a date/timestamp. |
Step 3: Look at the code for the Menu.java
and MenuTest.java
There are two new classes in the jpa06 code; they live in the same package as MenuItem.java
and MenuItemTest.java
:
Menu.java
: this is a class that encapsulates a collection ofMenuItem
objects that would represent the menu in a restaurant. The suggestion is to implement this as anArrayList<MenuItem>
.MenuTest.java
: this is a class to test theMenu
class’s methods.
Your job in this lab is to:
- Understand the tests in
MenuTest.java
- Understand the stubs in
Menu.java
- Add additional test cases to
MenuTest.java
for methods that don’t have test cases - Replace the stubs in
Menu.java
with correct implementations.
In addition, there are a few additional instructor test cases for the MenuItem.java
class
that might catch bugs that the test suite for jpa01
missed. So if your code has those bugs,
you may need to do some additional work in the MenuItem.java
class as well.
Methods you need to implement in Menu
:
-
Menu
constructor: You’ll see that theMenu
class has a constructor. That constructor currently does nothing, but it should do is set themenuitems
instance variable to refer to an emptyArrayList<MenuItem>
object. -
add
method: You’ll see that there is anadd
method. This method should take in aMenuItem
object and add it to theArrayList
calledmenuitems
. -
lookup
method: You’ll see that there is alookup
method. This method should search theArrayList
for aMenuItem
object with a name that matches the parameter passed in. If one is found, return a reference to thatMenuItem
; otherwise, returnnull
. -
csv
method: You’ll see that there is acsv
method. This method returns aString
consisting of the entire contents of the menu, one line at a time, where each line represents one item in the menu. The lines are separated by newline characters. Since the newline character can be different on different systems, instead of using'\n'
, we use the value ofSystem.lineSeparator();
which we store in a variable callednl
. (You can get a better idea of whatcsv()
is supposed to do by looking at the test cases for it inMenuTest.java
). -
sortByName
method: ThesortByName
method sorts theArrayList
that is encapsulated inside the class so that the result of thecsv
method will be sorted byname
. You should not implement your own sort (i.e. DO NOT write your own insertion sort, bubble sort, etc.). You should use the techniques for sorting outlined during the lectures and exercises linked to at the start of this assignment. You may choose to make this sort be the so-called natural ordering forMenuItem
, or you may choose to create an instance ofjava.lang.Comparator
, using any of the methods covered in the exercises; the implementation details are up to you. -
sortByCategoryThenName
method: ThesortByCategoryThenName
sorts first by the category of the item, then within each category, the items are sorted by name. One way to approach this is to build simple comparators for category and name, and then construct a composite comparator using thethenComparing
method, as shown in ex17. -
sortByCategoryThenPriceDescendingThenByName
method: This method is described in the comment in the source file:/** * Put items in categories first, where the categories are in * lexicographic order. Then sort in order of descending price, * with most expensive items first, and least expensive items last. * Break ties of items within a category that have the same price * by putting them in lexicographic order. */
This is certainly a case where creating simple comparators for category, name and price first, then using the
thenComparing
andreversed
methods is likely the easiest approach.
Checking the tests
To check the code against the tests that you’ve written, use:
mvn test
To check your test coverage, use:
mvn clean test org.pitest:pitest-maven:mutationCoverage
When you see that you are passing all of your own tests and killing off all the mutants, you can try submitting to Gradescope. The grade breakdown is:
- 32 points for tests for
MenuItem.java
- 48 points for tests for
Menu.java
- 20 points for mutation testing (i.e. your test suite kills all the mutants)
Do I need to adjust the CODECOV_TOKEN
?
For this lab, we are not checking that you’ve fixed the CODECOV_TOKEN
.
You are encouraged to set that up, as it’s good practice for future assignments, but it is not a requirement in terms of your grade.