Due: October 20th @ 10:00 pm
Purpose:
Practice designing function-classes and parametrized classes/methods. Practice using mutation.
Programming Problem: (Functions.java)
Here we get to work some with function classes. Be sure to read over the entire problem first. Note that if you feel comfortable, you can directly implement the IBuild as a parameterized class (see 5). The assignment is laid out to introduce the different components separately, but you will only hand in the parameterized implementation, not the int implementation.
- Create a class hierarchy to represent lists of integers. Make some examples. Two or three should do.
- Remember the Racket function build-list? It took a number and a function and produced a list of the given length with each element generated by the given function. Your job is to recreate that function in Java.
- Create an interface called IBuild (and so do you) to represent the 'function' passed to build-list. Call its method build. It should match a simple contract of [int -> int]
- Create an example of the identity function-class that implements IBuild. The identity function simply returns whatever it is given, in this case an integer.
- Design the method buildList in your Examples class (since it doesn't depend on any fields of any class). Your method should take an integer (length) and an IBuild (b) and produce a list of the given length with each element generated by using b on the numbers from 0 to length-1.
- Create IBuild subclasses that can be used to create: (1) a list of even numbers, (2) a list of square numbers, and (3) a list of numbers counting down from a given number (i.e., when the builder is created).
- Next we design the function map for your lists of numbers.
- Create an interface called IMap. For simplicity it will also have the simple contract of [int -> int]
- Create an example of the identity function-class that implements IMap.
- Design the method map in your list classes that takes an IMap (m), and produces a new list after applying m to each of the list's elements.
- Create IMap instances that can be used to: (1) add a given number to each element, and (2) multiply each element by a given number.
- Now we want to introduce a version of map which uses mutation.
- Now create a second version of the map method in your list classes called dmap which performs a destructive mapping, i.e. it modifies the elements in the list, changing them to be the new value. Think about the return type of the function.
- Create tests using your previous IMap instances. Don't forget to use a reset method to clean the state between tests.
- Finally we want to use parameterization to allow our IBuild to build more than just lists of numbers.
- Modify your IBuild class to take a parameter <T>, thus modifying your contract to [int -> T]. Modify the functions and examples to suit. Note that you will need a parameterized list; luckily we just created one in lab.
- Finally, take advantage of your new IBuild to create one more subclass which creates a list of numerical inverses (i.e. 1/1, 1/2, 1/3, 1/4, ...).