logo Practice-It logo

printRange

Language/Type: Java recursion recursive programming
Author: Whitaker Brand

Write a method printRange that accepts integer parameters x and y and that prints the sequential integers between x and y inclusive. The first half should be printed with the greater-than character (">") separating consecutive values. The second half should be printed with the less-than character ("<") separating consecutive values. The following table shows several calls and their expected output:

Call Output
printRange(1, 9); 1 > 2 > 3 > 4 > 5 < 6 < 7 < 8 < 9
printRange(10, 20); 10 > 11 > 12 > 13 > 14 > 15 < 16 < 17 < 18 < 19 < 20
printRange(-8, -8); -8
printRange(1, 10); 1 > 2 > 3 > 4 > 5 - 6 < 7 < 8 < 9 < 10
printRange(13, 14); 13 - 14

Notice that in the first output, 5 is in the middle with the numbers before it separated by greater-than and the numbers after it separated by less-than. In the second output, 15 is in the middle with numbers before it separated by greater-than and numbers after it separated by less-than. The third output has no separators because that range includes one number. When there are two values in the middle of the range, those two values should be separated by a dash, as shown in the last two outputs.

The method should throw an IllegalArgumentException if x is greater than y.

Type your solution here:


This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.