![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
MP6Homework 6. CollectionsSmalltalk neophytes only iterate over collections using do:. Veterans make full use of the Collection enumeration protocol, including
Part 1. Using the Collection protocol on stringsExercise 1.1. Create a class category CS598rej-HW6. Add one class called StringManipulator. Using the richer set for enumarating over collections (String is a collection), implement the following methods as instance methods in StringManipulator:We provided for you some test cases for this part (found in class StringManipulatorTestCase at the bottom of this page). Part 2. Extending class SetClass Set is one of the Collection classes, and it implements typical Collection protocol like do:, add:, remove:, and collect:. It also implements typical operations on sets like intersection and union. Study those methods in the image.For this part, you will create a class MySet, subclass of Set that implements method setComplement. This method takes no arguments and returns the complement of the set. Note that a complement makes sense only if there exists a Universal set. Define a universal set for the class MySet that is common to all instances of that class. (This is an ambiguous statement inviting you to explore different ways of storing the universal set.) Implement this method in a Test-Driven-Development style. Before writing the code of setComplement, write the test cases for it. Add a test case class called SetComplementTestCase. Add the following test case methods to it:
Here are some operations on sets that you might want to use:
Part 3. Binary treeExercise 3.1. Build a BinaryTree class. A binary search tree is a collection that is made up of a set of nodes. Each node has two children and a value. The particular BinaryTree class that you will implement should be ordered so that the value stored in a node is greater than or equal to the values of its right-most child or any of its children. Likewise, the value stored in a node is less than or equal to the values of its left-most child or any of its children (notice that our BinaryTree is the mirror-image of the regular BinaryTree you might have implemented in other programming classes). The trick is to note that each node is itself a binary tree, so you only need to make one class, BinaryTree, which is also the class of your nodes. Make your binary tree class a subclass of Collection . Implement the following messages:
add: aValue - add any object to the tree, as long as they are the same class and understand the '<' and' >' messages. elementsInOrder - return a collection of the elements of the tree in ascending order. do: - Note that do: should evaluate the block only for the values of the nodes, not the nodes themselves.
Exercise 3.2.
Having do:
implemented
in the binary tree, it should be able to understand
size, addAll:
, and inject: into:
. Test it to make sure. It might be able to understand
collect: and select:
. If not, make sure you can explain why. DeliverablesTry to use small methods described by an intention-revealing selector. When you fileOut the classes, please just paste the text into the e-mail. Do not send as an attachment. Part 1. fileOut the "StringManipulator" class that you created and I will look at that. Use the provided test case to verify your answers. Part 2. fileOut the "MySet" class and also your test cases. For each test case (testSetComplemet, testBadSetComplement, testDeMorgansLaws) make sure that you test several different inputs (null set, etc). Part 3. fileOut the BinaryTree class and also your test cases. As with part2, make sure that your tests cover different cases. For part 3.2, you can create some more test cases just to show me that size, addAll, works. FilesStringManipulatorTestCase.st Link to this Page
|