Write a static 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 greaterthan
character (">") separating consecutive values. The second half should be
printed with the lessthan character ("<") separating consecutive values. All
output should be on the current line of output.
printRange(1, 9);
System.out.println();
printRange(8, 20);
System.out.println();
printRange(8, 8);
System.out.println();
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
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();
printRange(13, 14);
System.out.println();
should produce the following output:
1 > 2 > 3 > 4 > 5  6 < 7 < 8 < 9 < 10
13  14
Here is what I have so far:
My code only works if x and y are equal, and if x is 1. I tried using a relationship between yx to track the place a number is in, but I couldn't keep the value constant. I can't use any loops. The problem must be solved with recursion. I keep trying to get it to work without x starting from 1, but I'm having difficulty. Any advice or guidance is appreciated. Thank you.
build the "<" half from the top number down to half the difference,
and the ">" part from the lower number up. Then concatenate the
two adding the "" if necessary. Does this meet the requirements?
Jim...
BEE MBA PMP SCJP6
That is an interesting idea Jim. Unfortunately, it doesn't meet the requirements. Thanks though
Brandon Peeples wrote:I increase x when I make the recursive call. I was thinking that it wouldn't seem right because if I did subtract x from y, it would always give me a different value each time since x increases. I am definitely doing something wrong here. Thank you for your comments Henry. I know your trying to get me to think about the logic more.
That is an interesting idea Jim. Unfortunately, it doesn't meet the requirements. Thanks though
I think its also not right because the (xy)/2 is not midway between x and y, its just the distance between the two numbers..
y/2 is actually only the "midpoint" when x = 0, in fact.. but (I believe.. if I am reading this right) your code is making it look like its correct for x = 1 because you add 1 to it
before you print it out..
If x + y is even this will return you the exact center. If it doesn't it returns you 1/2 left of the center. To get the other side (1/2 right of it) simply add 1/2: (x + y + 1) /2. Now if x + y is even, then (x + y) / 2 and (x + y + 1) / 2 will both return the same value. Use that to your advantage.
SCJP 1.4  SCJP 6  SCWCD 5  OCEEJBD 6  OCEJPAD 6
How To Ask Questions How To Answer Questions
I think its also not right because the (xy)/2 is not midway between x and y, its just the distance between the two numbers..
y/2 is actually only the "midpoint" when x = 0, in fact.. but (I believe.. if I am reading this right) your code is making it look like its correct for x = 1 because you add 1 to it
before you print it out..
I see what your saying. Thanks.
Rob Prime wrote:You get the center using the universal averaging code: (x_1 + x_2 + ... + x_n) / n. In this case, it's (x + y) / 2.
If x + y is even this will return you the exact center. If it doesn't it returns you 1/2 left of the center. To get the other side (1/2 right of it) simply add 1/2: (x + y + 1) /2. Now if x + y is even, then (x + y) / 2 and (x + y + 1) / 2 will both return the same value. Use that to your advantage.
Wow Rob... You are absolutely right. Thanks!
Brandon Peeples wrote:I increase x when I make the recursive call. I was thinking that it wouldn't seem right because if I did subtract x from y, it would always give me a different value each time since x increases. I am definitely doing something wrong here. Thank you for your comments Henry. I know your trying to get me to think about the logic more.
That is an interesting idea Jim. Unfortunately, it doesn't meet the requirements. Thanks though
I think another issue here might be to rethink your strategy a bit.. I don't think x is really "increasing" when you make the call with parameters x+1 and y, if I were writing a recursive method like that I would try to make sure it gives the right output for all values of the parameters regardless of the "starting point" where I first called the method  because otherwise its impossible to tell if the method is "correct" or not.. (at least as far as I can tell)  since there are too many recursive possibilities for different function calls (i.e. again, it should work the same way each time its called with the same parameters, whether called recursively or not..)
hope that makes sense?
Interesting note. For a recursive solution, you actually do not need to calculate the midpoint between x and y. You just need to handle the end conditions or otherwise setup for recursion.
The end conditions are... (1) if x > y (error), (2) if x = y, and (3) if x + 1 = y. And the recursive call is to increase x by 1 and decrease y by 1.
Henry
Henry Wong wrote:
Interesting note. For a recursive solution, you actually do not need to calculate the midpoint between x and y. You just need to handle the end conditions or otherwise setup for recursion.
The end conditions are... (1) if x > y (error), (2) if x = y, and (3) if x + 1 = y. And the recursive call is to increase x by 1 and decrease y by 1.
Henry
Thanks for the input guys. I'll give that a shot.
SCJP 1.4  SCJP 6  SCWCD 5  OCEEJBD 6  OCEJPAD 6
How To Ask Questions How To Answer Questions
First, those end condition checks should return from method  and not drop to the next check. After all, they are end conditions. It does seem okay, but just to be safe, in case you change something.
Anyway, you forget to print the y part of the recursive call  which should be done after the recursive call returns.
Henry
SCJP 1.4  SCJP 6  SCWCD 5  OCEEJBD 6  OCEJPAD 6
How To Ask Questions How To Answer Questions
Brandon Peeples wrote:
That makes sense. The problem I'm having is how can I make it stop without being infinite. If x+1 == y... how do I set the limit.
Well, if you make sure that end conditions actually end, instead of falling into the next condition, and causing another recursive call, it won't be infinite. As suggested in the last hint...
Henry
We should throw him a surprise party. It will cheer him up. We can use this tiny ad:
The WEB SERVICES and JAXRS Course
https://coderanch.com/t/690789/WEBSERVICESJAXRS
