Skip to content

Latest commit

 

History

History
executable file
·
39 lines (28 loc) · 2.19 KB

b401.md

File metadata and controls

executable file
·
39 lines (28 loc) · 2.19 KB

Back to questions

b401: Generic sets

Note: if you had trouble completing question 8a61, you should still be able to attempt this question by starting with the question 8a61 sample solution.

Replace the IntSet interface of question 8a61, with an interface called GenericSet that can hold elements of any given type, not necessarily integers.

Replace the implementing classes from question 8a61 -- MemoryEfficient``IntSet and SpeedEfficientIntSet -- with generic classes that mimic the original classes, but store elements of an arbitrary type rather than integers. Call these classes GenericMemoryEfficientSet and GenericSpeedEfficientSet.

Write a Demo class with a Main method that creates some sets, using various element types and both set representations (memory- and speed-efficient). Show that your generic sets implementation is working by adding some elements to the sets, removing some elements, adding some more, etc., and interspersing this set manipulation code with assertions checking that particular objects are or are not contained in the sets at given program points.

In addition, re-write the method:

public static IntSet readIntegers(int n) throws IOException;

to return instead a GenericSet with element type Integer. The method should behave the same as before: if n > 10 a memory-efficient set should be returned, otherwise a speed-efficient set.

As in question 8a61, add to your main method so that a GenericSet of integers is created by calling readIntegers, where the parameter n is given by a command-line argument. On returning from readIntegers, main should indicate which type of set has been returned. After this, main should repeatedly ask the user to enter an integer, in each case indicating whether the integer belongs to the set. When the user enters the "end of input" character, the program should terminate.

See question 8a61, for an example of what an interactive session with your program might look like.

When your program prints the type of set that has been returned, does it indicate what the generic parameter is? If not, why not?