Homework 7: Comparables, Comparators

Lectures needed for this homework: Lecture 10 (Comparators, Comparables), or Chapter 10.2.

Reminder that you must follow the style guide, and that you can check style yourself in IntelliJ as many times as you want.

Setup

Follow the Assignment Workflow Guide to get started with this assignment. This starter code is in the hw07 folder.

Before starting this assignment, you should update your library-fa25 folder. To do this:

  1. Open a terminal.
  2. Navigate to your library-fa25 folder. For example:
     cd ~/Desktop/cs61b/library-fa25
    

    (Change the command if your folder is elsewhere.)

  3. From the library-fa25 folder, run:
     git pull
    
  4. You should see a new file called jsoup-1.21.2.jar appear in your library-fa25 folder.

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 Star class so that it is Comparable with other star objects.

Run TestStar to verify your compareTo method. 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.

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 WordComparators class.

Run TestWordComparators.java to 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.

Submission

Graded files that you need to modify:

  • Star.java
  • WordComparators.java
  • WordFinder.java

You must follow the style guide for all three of these files. You can check style yourself in IntelliJ as many times as you want.

Follow the Assignment Workflow Guide to submit. You can submit as many times as you want. The score you see on Gradescope is your final score for this homework.