Problem
I made a Java program that asks for a word, then a screen width, then whether you want it to go fast or slow. Then it outputs that word and bounces it across the screen. Is this code efficient for what it does? Is there a better way to do it (without a pre-defined method/class)? And does it look professional and easy to read?
/*
This program was designed for use with command prompt on an ASCII
character set. Output is run through CMD.
This was designed for CMD with only an 80 ASCII character width.
str1 is the string containing the spaces.
str2 is the string containing the input word.
*/
import java.util.Scanner;
class Wave6 {
//Causes the output to lag for a set amount.
public static void xwait(long L) {
for(long c=0; c<L; c++);
}
//Asks for a string to "bounce" across the screen.
public static String input_string(char chr, String str2)
throws java.io.IOException {
do {
System.out.print("n"
+ "What word do you what to input? n"
+ "Must be less than 81 characters: ");
/*
Reads a list of characters and
inputs it into a string (to allow
for spaces and other non-letter chars).
*/
do {
chr = (char) System.in.read();
if(chr != 'n') str2 += chr;
} while(chr != 'n');
//Checks if the string is in the acceptable range.
} while((str2.length() < 1) | (str2.length() > 80));
return str2;
}
//Asks for a screen width (this is arbitrary).
public static int input_width(int width, String str2){
Scanner input = new Scanner(System.in);
do {
System.out.print("n"
+ "What is the width of the screen? n"
+ "Must be less than 81, and more than " + str2.length() + " characters: ");
width = input.nextInt();
} while((width <= str2.length() ) | (width >= 81));
return width;
}
//Asks if the user wants the program to run
//at full speed, or slower.
public static long input_speed(char ch, char ign, long L)
throws java.io.IOException {
do {
System.out.print("n
+ "Do you want it slow or fast? n"
+ "S or F: ");
ch = (char) System.in.read();
//Gets rid of unwanted chars in
//the char buffer.
do {
ign = (char) System.in.read();
} while(ign != 'n');
//checks if ch == F or S
} while( !(ch == ('F')) ^ (ch == ('S')) );
//Returns a large or small number
//to slow down the program, or not.
L = 0;
if(ch == 'S')
L = 25550000L;
else if(ch == 'F')
L = 0;
return L;
}
public static void main(String args[])
throws java.io.IOException {
String str1 = "", str2 = "";
int width = 0, i;
char ch = ' ', chr = ' ', ign = ' ';
long L = 0L;
str2 = input_string(chr, str2);
width = input_width(width, str2);
L = input_speed(ch, ign, L);
/*
Part of the program that actually makes the word
bounce around.
*/
for(;;) {
for(;;) { //Right bound
/*
If the two strings added length is equal to the
Input width +1, do not output a character return.
This is the end of the right bound, break out
of the infinite loop.
Based on the code, between the conversion from
right bound to left bound, str1 has to equal width.
Otherwise the word only goes to the width -1 space.
So str1 + str2 must equal width +1
*/
if( width + 1 == (str1.length() + str2.length()) ) {
System.out.print(str1 + str2);
xwait(L);
break;
}
else {
System.out.println(str1 + str2);
xwait(L);
}
//If the two strings are not equal to width +1
//add one space to str1
if(width + 1 != ( str1.length() + str2.length()) )
str1 += " ";
}
for(;;) { //Left bound
/*
Right bound handles the code when str1 and str2
are equal to width +1, so left bound doesn't have to.
Similarly Right bound handles the output for when
str1 equals 0, but left bound makes it equal to 0.
*/
if( width + 1 != (str1.length() + str2.length()) ) {
System.out.println(str1 + str2);
xwait(L);
}
//Take a space off of str1.
str1 = str1.substring(0, str1.lastIndexOf(" "));
if(str1.length() == 0)
break;
}
}
}
}
Also, I use n++, and I have my tab set to two ASCII character widths, so when I copied and pasted all of it was set to four spaces, is there an easy way to copy it without having to go back and delete all of the tabs and replace them with spaces? It takes forever. (this isn’t important to me, but it is a hassle when trying to ask questions about code on here)
This happened when I did this in Basic on my Ti 84, but what if I had made an output_leftBound()
and output_rightBound()
method, used main to call rightBound()
, then had rightBound()
and leftBound()
call each other? Would that be efficient or would it cause the memory to build up? On the calculator, if you goto
out of loops, the calculator builds up memory because it reserved it to look for an END command to signal the end of a loop, but it never finds one.
Someone mentioned that:
public static void main(String args[])
throws java.io.IOExeption {
was a code smell. How (in an inquisitive tone)? The book I am using uses it whenever it uses System.in.read()
. Though I did notice that when I didn’t include it my other methods that use System.in.read()
, the compiler reported an error stating:
Unreported exception IOExeption; must be caught or declared to be thrown
Is this a problem?
Solution
Major issues
This feels like C code more than Java. Part of the reason is the code structure — you’re using class methods everywhere instead of instance methods. The bigger problem is your parameter passing. For example, in input_string(char chr, String str2)
, you are using chr
and str2
as nothing more than local variables — you completely disregard their input values. Why not just take zero parameters and return a String?
Your input routines are unnecessarily complicated. You shouldn’t have to read one character at a time. Just read a line at a time using BufferedReader.readLine()
.
Your xwait()
function relies on busy-waiting, which you should never ever do. The first problem with busy-waiting is that it chews up CPU cycles which could be yielded to other processes running on the machine. Laptop users will hate you for wasting battery power warming up the CPU and spinning the fan. These days, you might even be running on a virtual machine on a multi-tenant host, which will cause other customers to complain! The other problem with busy-waiting is that the delay depends on the CPU’s speed, which will differ wildly on various machines. Assumptions about the CPU speed are what forced early PCs to have a “Turbo” button — to slow down the CPU so that poorly written games could run at their intended speed. If you want to introduce a delay, call Thread.sleep()
.
Your right-bound and left-bound loops are excessively complex. The way you use an infinite for(;;)
loop and break out of them is unconventional. Why not use the for-loops the way they are intended to be used? Then every programmer can tell at a glance what the structure of your loops are — what is being tested and what changes between iterations.
Nitpicks
Comments on your classes and functions might as well be JavaDoc comments. All you have to do is follow the JavaDoc formatting conventions.
Indentation should be at least four spaces. Two spaces is too narrow to read reliably, and it also encourages excessively deep nesting, which is a symptom of poorly organized code.
Method names in Java should lookLikeThis()
.
Your variables str1
, str2
, L
, ch
, chr
, ign
are all poorly named. None of them is self-descriptive. For the first three, I suggest lead
, word
, and delay
instead. Avoid declaring all your variables at the beginning of the function. For tightest scoping and least mental workload, you should declare variables as close as possible to where they are used. Also, don’t initialize all your variables to 0 or