...
- Write the
arrangements
(permutations) function from HTDP problem 12.4.2 in HW2 as a Java method for aWord
class provided in the fileWord.dj1
. This file includes definitions of the composite pattern classesWord
andWordList
. Decompose the problem in exactly the same form as this solution to problem 12.4.2. We are providing skeletons for the classesWord
andWordList
in the file WordList.dj1; use them. - Write the
mergesort
function from the last problem in HW4 (using exactly the same top-down approach described in HW4) as a Java method in the composite pattern classComparableList
provided in the file ComparableList.dj1. - Do Exercise 21.2.3 from HTDP using the Java composite pattern class
ObjectList
provided in the file ObjectList.dj1. This file includes the interfacePredicate
, which is the type of Java function arguments passed to thefilter
method, and abstract method stubs forfilter
,eliminateExp
,recall
, andselection
. Exercise 21.2.3 provides Scheme code for thefilter
function which you should directly translate to the corresponding Java method code inObjectList
. Note that yourfilter
method should work for arbitraryObjectLists
. In coding the methodseliminateExp
,recall
, andselection
, use the Java typeNumber
(compared using methoddoubleValue
in place of the Schemenumber
type and the Java typeObject
in place of the Schemesymbol
type. The methoddoubleValue()
inNumber
returns the value of this converted to adouble
. You will need to cast theObject
input of thetest
method to typeNumber
when filtering lists of numbers. - Do Problem 2 from Homework 5 in Java using the composite hierarchy of classes provided in the file ArithExpr.dj1.
Are You The Last Element In A List?
The A difficulty when processing lists is how to tell if one is at the last element of the list. This is useful in a number of situations, such as in the merge sorting process above.
...
The above code is frowned upon because it is an encapsulation violation of rest
, amounting to a checking of its type. You may use this style for this assignment, but it is not recommended and you can expect to be marked down for it in the future.
The better solution is to delegate to rest
and let it continue the process processing in a manner consistent with what it is. Remember that the last element of a list is . This defined by the fact that its rest
is empty
, or conversely, the parent of an empty
list is the last element. This thus requires a helper method:
...
This code is longer, but safer, more robust and more extensible.
But Wait, There's More!
How could you extend the delegation ideas in the above technique to enable you to differentiate between the follow four scenarios involving two lists?
- One list is empty and
- the other list is empty or
- the other list is non-empty
- One list is non-empty and
- the other list is empty or
- the other list is non-empty