Class Range3
- java.lang.Object
-
- Range3
-
- All Implemented Interfaces:
Comparable<Range3>
public class Range3 extends Object implements Comparable<Range3>
A class that represents a range of values on the integer number line. A range has two properties: aminimumvalue and amaximumvalue. Range objects always ensure that the following class invariant is true:minimum <= maximum
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intcompareTo(Range3 other)Compares this range to another range using the widths of the two ranges.booleancontains(int value)Checks ifvalueis inside this range.Range3expandLeft()Expands the width of this range by decreasing the minimum value of this range by 1 if possible.Range3expandRight(int amount)Expands the width of this range by increasing the maximum value of this range by the specified amount if possible.StringfizzBuzz()A common interview question for software developers.intgetMaximum()Returns the maximum value of the rangeintgetMinimum()Returns the minimum value of the rangeStringtoString()Returns the string representation of a range.
-
-
-
Constructor Detail
-
Range3
public Range3()
Initialize this range with minimum value -1 and a maximum value 1.
-
Range3
public Range3(int min, int max)Initialize this range with the specified minimum and maximum values.- Parameters:
min- the minimum value of the rangemax- the maximum value of the range- Throws:
IllegalArgumentException- if min is greater than max
-
-
Method Detail
-
getMinimum
public int getMinimum()
Returns the minimum value of the range- Returns:
- the minimum value of the range
-
getMaximum
public int getMaximum()
Returns the maximum value of the range- Returns:
- the maximum value of the range
-
expandLeft
public Range3 expandLeft()
Expands the width of this range by decreasing the minimum value of this range by 1 if possible. If the range cannot be expanded then nothing happens to this range.- Returns:
- a reference to this range
-
expandRight
public Range3 expandRight(int amount)
Expands the width of this range by increasing the maximum value of this range by the specified amount if possible. If the range cannot be expanded then nothing happens to this range.- Returns:
- a reference to this range
- Throws:
IllegalArgumentException- if amount is less than zero
-
contains
public boolean contains(int value)
Checks ifvalueis inside this range. A value is considered inside this range if the value is greater than or equal to the minimum value of this range and value is less than or equal to the maximum value of this range.- Parameters:
value- the value to check- Returns:
- true if value is inside this range, false otherwise
-
compareTo
public int compareTo(Range3 other)
Compares this range to another range using the widths of the two ranges. The narrower range is considered to be "less than" the wider range.- Specified by:
compareToin interfaceComparable<Range3>- Parameters:
other- range to compare- Returns:
- a negative integer if this range is "less than" the other range, a positive integer if this range is "greater than" the other range, and 0 otherwise
-
fizzBuzz
public String fizzBuzz()
A common interview question for software developers.Returns a string representation of this range where the string contains the values of this range (in order from the minimum to maximum value) where the values are separated by a comma and a space. If a value is evenly divisible by 3 then the string
"fizz"appears instead of the value. If a value is evenly divisible by 5 then the string"buzz"appears instead of the value. If a value is evenly divisible by 3 and evenly divisible by 5 then the string"fizzbuzz"appears instead of the value. For example, the range[10, 15]will produce the string equal to:"buzz, 11, fizz, 13, 14, fizzbuzz"
-
toString
public String toString()
Returns the string representation of a range. The string contains the minimum and maximum values separated by a comma and space all inside of a pair of square brackets. For example, the string[2, 25]represents the range whose minimum value is
2and whose maximum value is25
-
-