home1/src/Balls.java

38 lines
896 B
Java
Raw Normal View History

2016-08-24 19:18:26 +03:00
public class Balls {
2016-08-29 11:16:29 +03:00
enum Color {green, red};
public static void main (String[] param) {
// for debugging
}
2016-08-24 19:18:26 +03:00
2016-08-29 11:16:29 +03:00
public static void reorder (Color[] balls) {
int redBallCount = 0;
2016-08-29 11:22:05 +03:00
// count how many red balls there is
2016-08-29 11:16:29 +03:00
for (int i = 0; i < balls.length; i++) {
if (balls[i] == Color.red) {
redBallCount++;
}
}
2016-08-29 11:22:05 +03:00
// If all balls are red or green then just return
2016-08-29 11:16:29 +03:00
if (redBallCount == balls.length) {
return;
2016-08-29 11:22:05 +03:00
} else if (redBallCount == 0) {
return;
2016-08-29 11:16:29 +03:00
}
2016-08-29 11:22:05 +03:00
// replace the contents of the array with new colors that are in correct order
2016-08-29 11:16:29 +03:00
for (int i = 0; i < redBallCount; i++) {
balls[i] = Color.red;
}
for (int i = redBallCount; i < balls.length; i++) {
balls[i] = Color.green;
}
}
2016-08-24 19:18:26 +03:00
}