Date: Thu, 28 Mar 2024 21:38:04 -0500 (CDT) Message-ID: <1433768640.1089.1711679884767@wiki-n2.rice.edu> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_1088_1533748054.1711679884766" ------=_Part_1088_1533748054.1711679884766 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
Submit via SVN
This homework should be done in Full Java (using DrJava, IntelliJ, Eclip= se, or a text editor and command line compilation and execution). The= Functional Java language in DrJava regrettably no longer works for more co= mplex OO code such as the visitor pattern. In this assignment, you wi= ll re-implement each of the functions on IntLists assigned in Homework 7 us= ing the visitor pattern.
As before, your program must support the object-oriented formulation of = lists of integers defined the composite class hierarchy where
IntList
is an abstract list of int
.EmptyIntList
is an IntList
ConsIntList(first, rest)
, where first
is an <=
code>int and rest
is an IntList
, is an The Homework Support files IntList.java,
IntList=
Visitor.java
, LengthVisitor
, ScalarProductVisitor=
, and IntListTest.java
provide a starting point fo=
r your code. Feel free to edit these files and omit files that =
are not needed in this homework assignment.
Apply the visitor design pattern to define visitor classes implementing =
the IntListVistor
interface IntList
and=
its subclasses given above to formulate all of the following methods as&nb=
sp; visitorsJUnit test class, IntListTest
to te=
st all of your new methods in the IntList
class.&nbs=
p; Use the LengthVisitor example as a guide for defining your new visitor c=
lasses. Augment the test clas IntListTest.java
 =
;to include test methods for each of your visitor classes. Confine yo=
ur documentation to writing contracts (purpose statements in HTDP terminolo=
gy) for each visitor using javadoc
notation (a comment precedi=
ng the corresponding definition) beginning with /**
and =
closing with */
for each visitor class. Use the do=
cumentation of LengthVisitor
in the Homework Support files as =
an example.
boolean contains(int key)
returns key
is in the list, false
other=
wise. Name you visitor class ContainsVisitor
.)int reverse()
constructs a list that i=
s the reversal of this. Name your visitor class ReverseVisitor
=
. Hint: this computation is faster and simpler if yo=
u introduce a help "method" that takes an argument (also a visitor).int sum()
computes the sum of the ele=
ments in the list. Name your visitor class SumVisitor.double average()
computes the average =
of the elements in the list; returns 0
if the list is empty.&n=
bsp; Name your visitor class AverageVisitor
. int
&n=
bsp;to double
by using the prefix casting operator&n=
bsp;(double)
. IntList notGreaterThan(int bound)
retu=
rns a list of elements in this list that are less than or equal to NotGreaterThanVi=
sitor
.IntList remove(int key)
returns a list=
of all elements in this list that are not equal to key
.&=
nbsp; Name your visitor class RemoveVisitorIntList subst(int oldN, int newN)
ret=
urns a list of all elements in this list with oldN
r=
eplaced by newN
. Name your visitor class SubstVisit=
orIntList merge(IntList other)
merges th=
is list with the input list other, assuming that this list and other are so=
rted in ascending order. Note that the lists need not have the same length.=
Name your visitor class MergeVisitor
. Hint=
: add a "method" mergeHelp(ConsIntList other)
&nb=
sp;that does all of the work if one list is non-empty (a ConsInt=
List
). Only mergeHelp
is recursive. Use dynam=
ic dispatch on the list that may be empty. Recall that a.merge(b=
)
is equivalent to b.merge(a)
. You can =
formulate help methods as visitors.IntList mergeSort()
. Leveraging the =
merge
"method" you just wrote (as a visitor), write merge=
Sort()
that sorts an IntList.
Recall that you nee=
d to write a help function that splits a list approximately in two.In Racket, the equal?
function performs structural equality=
. Java does not include such a built-in operation. For the equals method (trivially defined in class
Object
) by an equa=
ls method that implements structural equality but it is slightly more compl=
ex than you might expect. Recall that the argument passed to eq=
ual
has type Object
. Hence, we have to worry abou=
t the class of the argument; the simple (and IMO best) definition of struct=
ural equality is to mandate that objects cannot be equal unless they are in=
stances of the same class. Study the definition of the equals=
code> method in class
statement where explicit ConsIntList
. Unfortunately, we can=
write the body of this method the return
of a boolean-valued =
expression, because Java does not support a notion of local
or=
let
at the level of expressions. So the body is an return
statements in the =
consequent statement and alternative statement. Notice that we still =
programmed in a functional style without any mutation.
To test the computations that yield results of composite type, we can ei=
ther define structural equality over the composite type (as we did for toString()
representations of the composite type. But be=
ware that toString()
equality may not imply structural equalit=
y and vice versa. You should always endeavor to make them agree.