Lab 5: Comparables, Comparators
Lectures needed for this homework: Lecture 11 (Comparators, Comparables), or Chapter 10.2.
Here are the slides presented in lab for your reference.
Setup
Follow the Assignment Workflow Guide to get started with this assignment. This starter code is in the lab05 folder. NOTE as with previous labs you do not need to submit this assignment.
Task 1: Comparable Stars
The file Star.java reads in a list of all stars within 20 light years of earth. Try running it and you should see the first five stars in the file. For each star, the name, distance, mass, and apparent magnitude are given.
Star Comparable
Stars should be comparable based on their mass. The mass of each star is given in units of solar mass, e.g. if massMsun is 2.5, then the mass of the star is 2.5 times that of the sun.
Modify the
Starclass so that it isComparablewith other star objects.Run
TestStarto verify yourcompareTomethod. You’ll need to uncomment the tests.
Main
Modify the main method so that only the star with the greatest mass is printed out.
Use Collections.max. For an example, see this chat.
Your main method should print out the entire string for the star. For example, the string for the sun is Star{name='Sun', distanceLy=0.000016, massMsun=1.000, mV=-26.74}.
This will only take one line of code. Don’t forget to comment out the code that prints the first five stars in the file.
Task 2: Word Comparators
In this task, you’ll edit WordComparators.java.
xComparator
Fill in public static Comparator<String> getxComparator().
An xComparator should compare strings based on the number of lowercase x’s that appear in the string. For example, “xelha” should be considered less than “xoxocotla”, because “xelha” has one x and “xoxocotla” has two.
Now that you have finished implementing your first
Comparatorplease complete the checkpoint as a group with your TA for attendance.To get attendence credit please explain each of these to your TA:
- What did you change in the
Starclass? Why did this work?- What is the
xComparatordoing here? How is this different from what we did previously?- More broadly, how are
ComparatorsandComparablesdifferent?
charComparator
Fill in public static Comparator<String> getCharComparator(char c).
A charComparator should compare strings based on the number of instances of c that occur, where c is the character specified when the Comparator is created. For example, the Comparator returned by getCharComparator('a') should consider “xelha” to be equal to “xoxocotla”, because they both have one ‘a’.
charListComparator
Fill in public static Comparator<String> getCharListComparator(List<Character> chars).
A charListComparator should compare strings based on the number of times any of the characters in the list appear in the string. For example, getCharListComparator(List.of('a', 'e', 'i', 'o', 'u')) should return a comparator that compares based on the number of lowercase vowels in the string.
Implement the three comparators in the
WordComparatorsclass.Run
TestWordComparators.javato test your implementation.
Note: Each comparator is likely to share logic with the others. You can use private helper methods as appropriate to avoid re-writing code.
Task 3: Word Finder
findMax
In WordFinder.java, findMax should return the String in the array with the maximum value, as defined by the comparator c.
If there are multiple strings that are the maximum value, return the first one you find.
Implement findMax, and run TestWordFinder.java to test your implementation.
Use a loop. Don’t use Arrays.sort or similar.
This is a straightforward task. We just wanted you to have a chance to actually use a Comparator.
Moby Dick
The text of the first chapter of Moby Dick is available in the file mobydick.txt.
Fill in the main method so that only the word with the most lowercase vowels is printed out.
You’ll want to use your findMax method.
Optional (ungraded)
Optionally, uncomment the code in main and use your tool to find the word with the most vowels in the wikipedia article on Zebra. Feel free to explore other articles.
files that you need to modify:
Star.javaWordComparators.javaWordFinder.java