Problem 2: Splitting Ranges
Loyola College >
Department of Computer Science >
Dr. James Glenn >
HS Programming Contest >
Problems >
Problem 2: Splitting Ranges
Introduction
An instance of the Range class models a range of integers from
some minimum value to some maximum value. In some applications of ranges,
it is useful to split the ranges up into subranges so that the subranges
cover all of the integers in the original range and have endpoints
that are multiples of some integer (or the endpoints of the original range).
For example, given the range (4, 19) and using multiples of 5, we would
create subranges (4, 5), (5, 10), (10, 15), and (15, 19).
Note that the endpoints of any given subrange differ by at least one and that
each integer in the original range is in exactly one of the subranges,
except that multiples of 5 may appear as the endpoints of two consecutive
ranges.
Problem
Complete the split method in the Range class, which
adds subranges to the range it was invoked on based on multiples of
the argument passed to it.
You should assume that the Range object has already been
instantiated and has an empty subrange list.
The Range class has methods getMinValue
and getMaxValue to get the endpoints. The subranges you compute
should be added to the subrange list
by using the void addRange(int min, int max) method.
They must be added in increasing order by minimum value.
Assumptions
- You may assume that the minimum value of the range to split is
strictly less than its maximum value.
- You may assume that the argument to split is at least 2.
- You may assume that no subranges have been added prior to
the invocation of your split method.