logo Practice-It logo

printRange

Language/Type: Java recursion recursive programming
Author: Stuart Reges (on 2014/02/13)

Write a method printRange that takes integer parameters x and y and that prints the sequential integers between x and y inclusive. The first half of the list 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. All output should be on the current line of output.

For example, the following calls:

    printRange(1, 9);
    System.out.println(); // to complete the line of output
    printRange(8, 20);
    System.out.println(); // to complete the line of output
    printRange(-8, -8);
    System.out.println(); // to complete the line of output

should produce the following output:

    1 > 2 > 3 > 4 > 5 < 6 < 7 < 8 < 9
    8 > 9 > 10 > 11 > 12 > 13 > 14 < 15 < 16 < 17 < 18 < 19 < 20
    -8

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, 14 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 only one number.

When there are two values in the middle of the range, those two values should be separated by a dash. For example:

    printRange(1, 10);
    System.out.println(); // to complete the line of output
    printRange(13, 14);
    System.out.println(); // to complete the line of output

should produce the following output:

    1 > 2 > 3 > 4 > 5 - 6 < 7 < 8 < 9 < 10
    13 - 14

You must exactly reproduce the format of these examples. Your method should throw an IllegalArgumentException if the first parameter is greater than the second. You may NOT use a while loop, for loop or do/while loop to solve this problem; you must use recursion.

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.