This is a program I made to bounce the letter a across the screen. I made a few others, but this one I made with a recursive bounce()
method. I know main()
is a bit empty, but I intend to make an input menu that main refrences, so it seemed better to put the code in another method.
Is my code clean (probably far from it)? Easy to read? Am I overdoing the comments, or not specifing the code well enough?
Is it a bad idea using a recursive method for this as it has to keep memory for 80 recursions? It seemed like a good idea, because I was condensing so much more code into a smaller space.
/**
* This code is designed for use with CMD with an 80
* ASCII character width space.
* It adds and subtracts a space to a string and out prints
* the line.
* It uses recursion to create one full Right-Left cycle,
* then uses an infinite while loop to repeat.
*/
class Wave8 {
static String spaces = ""; // Holds the spaces
static boolean direction = true; // True is right, false is left
// Bouncing loop
public static void bounce() {
if (spaces.length() == (80 - 1)) { // Width minus the string after lead
System.out.print(spaces + "a");
direction = false; // Change direction
}
else
System.out.println(spaces + "a");
if (direction) { // If going right
spaces += " ";
bounce(); // Recursion
}
if (!direction) { // If going left
spaces = spaces.substring(1);
if (spaces.isEmpty()) {
direction = true;
return;
}
else System.out.println(spaces + "a"); // Normal routine
}
}
public static void main(String args[]) {
while (true) {
bounce();
}
}
}
Also, is there a way to test for efficiency? That would help greatly.