Assignment 8: Abstraction
Goals: Practice applying abstracting to similar code.
You should submit one .java file containing the solution to this problem.
Be sure to properly test your code and write purpose statements for your methods. A lack of tests and documentation will result in a lower grade! Remember that testing requires you to make some examples of data in an examples class.
The "Earliest" String
Design the classes and interface necessary to represent a non-empty list of strings.
- Design a functional interface that contains one method which will indicate whether or not the first of two given strings "comes before" the second. Note that this interface is effectively defining an ordering of the strings, and different specific function objects will define different orderings.
Does this interface allow us to reason about strings that "tie"? Why or why not?
Use the above interface to design a method which returns the "earliest" element in a non-empty list of strings. Note that "earliest" here means the string that "comes before" all the others according to the given function object. Avoid duplicate computation!
- Use the above method to define methods which return:
The shortest element in the list of strings.
- The string that appears earliest in the alphabet.
A string s1 comes before another string s2 in the alphabet if s1.compareTo(s2) is negative.
The string at the start of the list.
The longest element in the list of strings.
The string that appears last in the alphabet.
The string at the end of the list.
Note: While six computations are described here, explicit programming of six different string comparison classes shouldn’t be needed. The latter half of this list is the exact opposite of the first half, and you should be able to use this to your advantage, all while using the same "earliest" method. Look to the lecture code for inspiration!