Next how would you do it manually? What if there were some playing cards laid in a row in slots on the table? What steps would you take to rotate them?
Norm Radder wrote:@Daniel Demesmaecker
Please read this: http://www.javaprogrammingforums.com/cafe/9544-problem-spoon-feeding.html
Please look at the link Norm posted; it is a good explanation of why posting complete solutions is a bad thing. I looked at it last night, but didn't finish it because it was very late and it is a long article.
But, I am going to retrieve your code (obviously moderators have access to deleted posts on every forum website) because I think it needs discussion. I am afraid I don't actually think it is correct code. You would have noticed that if you had tested it more extensively. I suggest you go through the original array and make ten copies of it, each with one number changed. For example, if the original array is
1 3 6 7 4 5 7 7 9 9
then your first change might be
2 3 6 7 4 5 7 7 9 9
and your fifth change might be
1 3 6 7 5 5 7 7 9 9
You might like to write a program to make those changes automatically.
You now have eleven different arrays: test your program with those eleven and see how many give the expected output. I am going to be bold and predict that you will get 9/11 right.
I am afraid you are going to have to improve your code style. I can't seem to find the old Sun style guide; I think it has been removed. If you find it pleasee post a link to it. We do however have old Javaranch style suggestions. Have a look at the sections about spacing out text. I also think the if statement would work better if you move it outside the loop.
Challenge: Find out whether I was right about 2/11. I haven't tried it yet. I was all confused last night about why your code seemed to give the expected answer, but when daylight come, I thought there is a special feature about the input which obscures the error. See if you can work out what I mean. And whether I am right or wrong.
Yes, that is what I meant, but the link won't open.
Daniel Demesmaecker wrote:. . . odl sun style guide, but anyways: 1997 Java code conventions
Campbell Ritchie wrote:Yes, that is what I meant, but the link won't open.
Daniel Demesmaecker wrote:. . . old sun style guide, but anyways: 1997 Java code conventions
Weird, I have no probs opening the link
There has been nothing from OP for 36 hours, so I think I can discuss your solution with a clear conscience.
Some languages permit this construct: x, y := y, x, but the languages whose syntax derives from C don't. And that includes Java®. Now, if you try to swap the values of two variables, you have to use an intermediate temporary variable. Otherwise you get this sort of thing:-I know there are party tricks like this:-...but their behaviour in C is not strictly defined (I think, not certain), and the left‑to‑right rule in Java® means that trick doesn't work at all in Java®. Try it for yourself, both in C and Java,, but remember it isn't suitable for production code. There are also tricks whereby you subtract the two values, but I can't remember how they go.
That means that swapping two elements in an array is best done in a utility method, so please remind yourself about the utility class pattern (I think it is a pattern, even though I don't remember seeing it in the pattern books).
Now we have seen that, consider that rotating an array takes the same concepts as swapping elements, only it is a bigger exercise. You have successfully avoided simply copying array into every element of the array, but go through the code you posted very carefully and work out how many times you have assigned anything to array. In 9 out of 11 cases, you were unlucky that the last two elements of the array were equal to each other, so leaving the penultimate element unchanged appeared to give the required result. The error only shows up when the last two elements are different from each other. There are two corrections I would suggest.
You can simplify the process greatly by using the System.arrayCopy() method. Like so:
1. Save array somewhere.
2. Use System.arrayCopy() to copy array[1..N] to array[0..N-1].
3. Put the saved value back into array[N].
That is the document, yes. Thank you. I can open it on Opera and not on FF, but I am too busy for that to be more than one of those little mysteries of life.
Daniel Demesmaecker wrote:. . . anyways: 1997 Java code conventions
[edit:addition] It is a different edition from what I was familiar with, but yes, that is the document. I have saved it.