Date: Fri, 29 Mar 2024 02:36:06 -0500 (CDT) Message-ID: <1311877359.1131.1711697766747@wiki-n2.rice.edu> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_1130_1506971464.1711697766745" ------=_Part_1130_1506971464.1711697766745 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
Submit via Owl-Space
This homework on manipulating Java lists must be done using the Function=
al language level of DrJava.We are providing skeleton classes for each prob=
lem with unimplemented methods for you to write. Each such class includes a=
method with a name like listString()
that converts lists to m=
ore readable String notation similar to that used in Scheme. You can put al=
l of your classes (except test classes) in the same file. Write a test clas=
s for each composite data type (Word
, WordList
, <=
code>ComparableList, ObjectList
, and ArithExpr).
arrangements
(permutations) function from HTDP p=
roblem 12.4.2 in HW2 as a Java method for a Word
class provide=
d in the file Word.dj1
. This file includes definitions of the =
composite pattern classes Word
and WordList
. Deco=
mpose the problem in exactly the same form as this solution to problem 12.4.2. W=
e are providing skeletons for the classes Word
and WordL=
ist
in the file WordList.dj1; use them.mergesort
function from the last problem in HW4 =
(using exactly the same top-down approach described in HW4) as a Java metho=
d in the composite pattern class ComparableList
provided in th=
e file ComparableList.dj1.Predicate
, which is the type of Java func=
tion arguments passed to the filter
method, and abstract metho=
d stubs for filter
, eliminateExp
, recall, and selection
. Exercise 21.2.3 provides Scheme code for=
the filter
function which you should directly translate to th=
e corresponding Java method code in ObjectList
. Note that your=
filter
method should work for arbitrary ObjectLists
eliminateExp
, recall
, =
and selection
, use the Java type Number
(compared=
using method doubleValue
in place of the Scheme number<=
/code> type and the Java type Object
in place of the Scheme symbol
type. The method doubleValue()
in Numb=
er
returns the value of this converted to a double
. You=
will need to cast the Object
input of the test
m=
ethod to type Number
when filtering lists of numbers.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.
In class, the following shortcut was shown but with the clear caveat tha= t this style of programming is only being done for brevity's sake at this p= oint and that it will NOT be tolerated in the future:
(THE FOLLOWING CODE USES GENERIC NAMES FOR EVERYTHING. DO NO=
T ROTE COPY AND USE THESE NAMES!)
// In a= method of a ConsList if(rest =3D=3D EmptyList.ONLY) { // we are at the last element, so process accordingly } else { // we are not at the last element, so process accordingly }
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 d=
own for it in the future.
The better solution is to delegate to rest
and let it conti=
nue the processing in a manner consistent with what it is.  =
; Remember that the last element of a list is defined by the fact that=
its rest
is empty
, or conversely, the paren=
t of an empty
list is the last element. This thus requir=
es a helper method:
class C= onsList implements List { ... // In a method of the ConsList... return rest.helper(this); // delegate to rest and let it decide what= to do. } /** * Helper method. If this method is called, we know that the parent is= NOT the last element! * @param parent A reference to the parent list, i.e. the caller. */ Object helper(ConsList parent) { // Parent is NOT the last element, process the *parent* accordingly. } } class EmptyList implements List { ... /** * Helper method. If this method is called, we know that the parent is= IS the last element! * @param parent A reference to the parent list, i.e. the caller. */ Object helper(ConsList parent) { // Parent IS the last element, process the *parent* accordingly. } }
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 enab= le you to differentiate between the follow four scenarios involving two lis= ts?