Stupid Proofing
Last Monday my Intro. to Computer Science teacher assigned a program for us to write, in Java. The program was supposed to ask the user for a positive integer. If the entered integer was even, the program was to count from the given number down to one. If it was odd, the program would count from one up to the number. Overall it was a pretty basic project, except for the additional requirement that he tacked on to the end. We were to "stupid proof" the program to the full extent of our ability.
Stupid proofing, he said, making sure the program still worked, even if the wrong type of data is given. For example, if the user entered o.25, for example, into the program described above, the program would yield unexpected results, or might throw a runtime error.
The basic program looks like this:
Scanner keyboard = new Scanner(System.in); //initialize the keyboardThis program works perfectly fine, so long as the user does as the program asks, and only enters a positive integer, but what happens if the user enters 3.14 or -56? This is where stupid proofing comes in.
System.out.print("Please enter a positive integer. ");//request input data
int number = keyboard.nextInt();
if (number % 2 == 0) { //if number is even...
int counter1 = number;
while (counter1 > 1) {
System.out.println(counter1);
counter1--;
}
} else { //if number is not even...
int counter2 = 1;
while (counter2 <= number) {
System.out.println(counter2);
counter2++;
}
}
Entering a decimal number, such as 3.14, would produce a run time error, and halt the program. Entering a negative number would cause the loops never to reach their termination conditions.
Fixing the run time error is pretty simple. All you have to do is change the data type of the umber variable form integer to double. This; however, causes problems with the program logic, so we can add an if statement to check to see if the entered number is an integer.
boolean isAnInt = false;This segment of code just checks to see if the given number is divisible by 1 with no remainder. If it is, the number is an integer and the rest of the code is bypassed, if it's not, then the program queries the user for a new number and. The while statement merely continues querying the user for numbers until an integer is entered.
while (isAnInt == false){
if (number % 1 != 0) {
System.out.print("The number you entered is not an integer!\nPlease enter a positive integer. ");
number = keyboard.nextDouble();
} else {
isAnInt = true;
}
}
The method to check for positive numbers is much the same as in the previous example: an if statment checks to see if the number is greater than zero, if it is not, it askes for a new number; and a while loop repeates the process untill the user gets it right.
boolean isPos= false;
while (isPos == false){
if (number < number =" keyboard.nextDouble();" ispos =" true;">These two peices of code can be easily combined into a single loop and a single if statment, as follows:
boolean isGood = false;
while (isGood == false){
if ((number % 1 != 0) || (number < number =" keyboard.nextDouble();" isgood =" true;">
With all of this in place, the user can enter any number into the program without halting it. Here's the original program, with all of the corrections inserted:
Scanner keyboard = new Scanner(System.in); //initialize the keyboardI don't know a good way to protect against the user entering symbols or strings, so this is as far as I can go. If by chance you want to use this program, be forewarned: I just typed it, without debugging it, or seeing if it compiled, so it may have some small errors.
System.out.print("Please enter a positive integer. ");//request input data
double number = keyboard.nextDouble();
boolean isGood = false;
while (isGood == false){
if ((number % 1 != 0) || (number < number =" keyboard.nextDouble();" isgood =" true;">
if (number % 2 == 0) { //if number is even...
int counter1 = number;
while (counter1 > 1) {
System.out.println(counter1);
counter1--;
}
} else { //if number is not even...
int counter2 = 1;
while (counter2 <= number) { System.out.println(counter2); counter2++; } }
I'll end with a piece of advice from my teacher:
Don't underestimate the stupidity of the user.
Labels: code, Computer Science, Java, program, stupid proofing
0 Comments:
Post a Comment
<< Home