Discovering errors
Seoul National University of Science and Technology
Information Technology Management
Lecture slides index
April 13, 2025
There’s something else that also happens in the video that may go unnoticed at first.
This effect is known as perceptual blindness, and is explained by the fact that as we focus on a specific task, our brains tend to filter out information that is irrelevant to that task.
However, we don’t always know what information is, in fact, essential and what is not
Concentrating on a specific part of a study exercise can lead to relevant information being filtered out.
Fortunately, applying oneself to a given task lessens the occurrence of perceptual blindness. In other words, practice develops one’s ability to distinguish between relevant and irrelevant information.
Scanner scanner = new Scanner(System.in);
int values = 0;
int sum = 0;
while (true) {
System.out.println("Provide a value, a negative value ends the program");
int value = Integer.valueOf(scanner.nextLine());
if (value < 0) {
break;
}
values = values + 1;
sum = sum + value;
}
if (sum == 0) {
System.out.println("The average of the values could not be calculated.");
} else {
System.out.println("Average of values: " + (1.0 * sum / values));
}
A bug still happens to remain in the program in the previous slide. What kind of input can be used to find it? In the input options below, the character ‘’ corresponds to a new line that is achieved by pressing the enter key.
-1\n
6\n2\n-1\n
0\n-1\n
/*
Prints the numbers from ten to one.
Each number is printed on its own line.
*/
// We create an integer variable named value,
// assigning the value 10 to it.
int value = 10;
// The loop execution continues until
// the value of the variable named value is less than or equal to
// zero. The excution doesn't stop immediately when the value zero
// is assigned to the variable, but only when the condition of the
// loop is evaluated the following time.
// This always happens after the loop has executed
while (value > 0) {
// we print out the value in the variable and a new line
System.out.println(value);
// we decrement the value by one
value = value - 1;
}
public static void printValuesFromTenToOne() {
int value = 10;
while (value > 0) {
System.out.println(value);
value = value - 1;
}
}
Scanner scanner = new Scanner(System.in);
int values = 0;
int sum = 0;
while (true) {
System.out.println("Provide a value, a negative value ends the program");
int value = Integer.valueOf(scanner.nextLine());
if (value < 0) {
break;
}
values = values + 1;
sum = sum + value;
}
if (sum == 0) {
System.out.println("The average of the values could not be calculated.");
} else {
System.out.println("Average of values: " + (1.0 * sum / values));
}
Scanner scanner = new Scanner(System.in);
int values = 0;
int sum = 0;
while (true) {
System.out.println("-- values: " + values + ", sum: " + sum);
System.out.println("Provide a value, a negative value ends the program");
int value = Integer.valueOf(scanner.nextLine());
if (value < 0) {
System.out.println("-- value is negative, exiting loop");
break;
}
values = values + 1;
sum = sum + value;
}
System.out.println("-- loop exited");
System.out.println("-- values: " + values + ", sum: " + sum);
if (sum == 0) {
System.out.println("The average of the values could not be calculated.");
} else {
System.out.println("Average of values: " + (1.0 * sum / values));
}
Ctrl+F8
.F8
.F7
.Consider the following Java code snippet where a simple loop is used:
public class Test {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
multiplicationTable(3);
}
public static void multiplicationTable(int max) {
int number = 1;
while (number <= max) {
printMultiplicationTableRow(number, max);
number++;
}
}
public static void printMultiplicationTableRow(int number, int coefficient) {
int printable = number;
while (printable <= number * coefficient) {
System.out.print(" " + printable);
printable += number;
}
System.out.println("");
}
}
Computer Language
Comments