Skip to content

Latest commit

 

History

History
96 lines (76 loc) · 3.36 KB

8a61.md

File metadata and controls

96 lines (76 loc) · 3.36 KB

Back to questions

8a61: Int set

It is worth trying question 1486 first, though this is only a weak dependency.

Step 1

Write an interface, IntSet, to represent a set of integers. Your interface should support the following methods:

// Adds the integer x to the set
public void add(int x);

// If the integer x belongs to the set, it is removed and 'true'
// is returned.  Otherwise 'false' is returned
public boolean remove(int x);

// Returns true iff the set is empty
public boolean isEmpty();

// Returns true iff the set contains the integer x
public boolean contains(int x);

Step 2

Now provide two classes that implement IntSet: MemoryEfficientIntSet and SpeedEfficientIntSet. The idea is that MemoryEfficientIntSet would be suitable for representing large sets, using minimal memory but at the expense of slower element lookup operations, while SpeedEfficientSet would be optimised for fast element lookup, at the expense of requiring more memory.

However, in this question you do not need to implement these classes to actually be memory/space efficient -- the distinction between these sets is for purposes of illustration only. Implement the two classes in any way which conforms to the IntSet interface: if you wish you can use the same implementation for both, and it is fine to realise these sets by delegating to classes in the Java Collections framework.

Hint: A simple solution is for MemoryEfficientIntSet to have a member of type Set<Integer>. You can then implement add(), remove(), isEmpty() and contains() in a very straightforward manner by delegating these calls to the Set<Integer> member. You can implement SpeedEfficientIntSet similarly. This may seem pointless: why not just use a Set<Integer> directly? Of course, it is pointless. However, going through this process provides a solid educational example of how classes and interfaces work.

Step 3

Write a Demo class with a static method:

public static IntSet readIntegers(int n) throws IOException;

The readIntegers method should read n integers from standard input. If n > 10 the method should return a memory efficient set containing these integers, otherwise it should return a space efficient set. (You can assume that the integers entered are all distinct.)

Step 4

Write a main method which gets an IntSet 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.

Assuming that 3 is given as a command-line argument, an interactive session with your program might look like the following:

Please enter an int:
10
Please enter an int:
20
Please enter an int:
30
Set is: class tutorialquestions.question8a61.SpeedEfficientIntSet
Enter an int, to be tested for membership:
10
Set contains 10: true
Enter an int, to be tested for membership:
11
Set contains 11: false
Enter an int, to be tested for membership:
12
Set contains 12: false
Enter an int, to be tested for membership:
20
Set contains 20: true
Enter an int, to be tested for membership:
^D
Goodbye!