Laboratory 5

Decisions, Decisions

Week of 28 February, 2005



Home | Resources | Homeworks | Slides | Labs | Contacts | Submit | Forums | Private

Objective

As your knowledge of the Java language grows, your capacity to write more useful and more complex programs also increases. The if statement is a powerful tool for decision making. However, it can lead to considerable confusion if you do not fully understand its usage. This laboratory familiarizes you with the if statement, Boolean logic, and introduces you to program decision making.

 

Key Concepts

 

Getting Started

Using the procedures in the previous laboratories, copy the files IsSorted.java, IsReverse.java, IsTriangle.java, Nand.java, Mystery.java, and Predict.java for your manipulation (for each of the files, right-click on the link and select "save as").  All the files will need to be submitted except for IsSorted.java.

 

Syntax Errors aren't the only problem

In addition to syntax errors, also known as compile-time errors, there are also logic errors. Code often compiles successfully (i.e., there are no syntax errors). Does this mean that the code will work? Well, it will do exactly what you tell it to do, but not necessarily what you want it to do.

Programmers sometimes write programs that they think look correct and that compile without producing any errors, but do not produce the intended results (i.e., there are logic errors). This situation results in frustrated users, and sometimes in users who do not know that the program is producing incorrect results.

One source of logic errors is misunderstandings about the uses of particular language features and their effects on the flow of execution in the program (what the program does and which paths it takes when you run it).

In the following exercises you will learn how to follow the logic of an if statement and to create tests cases to show that are there are no logic errors.

 

Checking Order

The if statement allows the programmer to make decisions. If some expression is true, one course of action is taken. If the expression is false, the course of action is skipped. The if statement can lead to problems. The following exercises will help you to understand the if statement so that you can avoid some of its troublesome pitfalls.

Open the program IsSorted.java. This program checks whether its user supplies three numbers in non-decreasing order. If they are not in non-decreasing order, a message is displayed and the program immediately terminates using the Java method System.exit(). Otherwise, the program does not produce any output. The program accomplishes its task through a single if statement.

Examine the program and record what you expect as the result of supplying as input, the values 5, 4, and 8 (in that order). Re-examine the program and record (as a comment in the file) what you expect as the result of supplying as input, the values 4, 5, and 8 (in that order).

Compile and run the program to check your understanding. Compare its results with what you had expected. If they differ, examine the code more carefully to see what is happening. If you cannot reconcile the differences, discuss them with an instructor.

Why do you think it is important to develop a set of test inputs so that every part of a program is executed at least once? This question is a great exam question. Knowing the answer is important.

Close IsSorted.java.  You do not have to submit it, as it was not changed.

Open the file IsReverse.java. The purpose of this program is to determine whether its inputs are in non-increasing order. Complete the program. As part of the program include comments that list test cases that together ensure all of the code is tested.

Submit IsReverse.java.

Close the file.

 

Checking triangleness

Open the program IsTriangle.java. This program begins in a similar manner as program IsSorted.java. However, it asks for three positive non-decreasing numbers. After extracting the inputs, the program checks as did IsSorted.java whether the three numbers are in non-decreasing order. If they are not in non-decreasing order, a message is displayed and the program immediately terminates using the Java method System.exit().

If the input values pass the ordering test, the program checks whether they are possible lengths of the sides of a triangle. For the values to represent the lengths of a triangle, the sum of the first two input values must be greater than the length of the third value. In other words, the values do not represent a triangle if the sum of the first two values is less than or equal to the third value.

Examine program IsTriangle.java and record what you expect as the result of supplying as input, the values 5, 4, and 8 (in that order). Re-examine the program and record what you expect as the result of supplying as input, the values 4, 5, and 8 (in that order). Record your answers as a comment in the file.

Compile and run the program to check your understanding. Compare its results with what you had expected. If they differ, examine the code more carefully to see what is happening. If you cannot reconcile the differences, discuss them with an instructor.

Our test cases do make sure that all lines of code within the program are executed. But is the program correct? Does it verify that the input is of the specified form? Consider the inputs -4, 0, and 1 (in that order). What output should the program display? What output does the program display? The program needs to be modified to accomplish its task in a truly correct manner – an error message should be displayed if the numbers are not of the requested form. To implement the modification, the program needs to perform an additional check regarding the input. This new check must determine whether the inputs are positive. If the other check has already been made, then testing whether the smallest value (value1) is greater than zero tells us whether the inputs are all positive.

There are several ways to add the check. One possibility is to expand the Boolean expression that tests the program to see whether this other condition is also met. The advantage to this approach is that the code is short. However, is the code still simple to read? Another possibility is a separate if statement that follows the first if statement. Other possibilities also exist.

Modify IsTriangle.java using one of the two listed possibilities mentioned above.

Retest the program using all three of our test cases. Whenever changes are made to a program. It should be completely retested. You need to be sure that a correction has not introduced a new error.

If your modified program is incorrect, examine it line-by-line to determine how the errant output was produced.

Once a correct program has been developed, submit it.

Close the file.

 

Being Contrary

You will now use the if statement to help compute the value of a standard Boolean operation.

Open the file Nand.java.

This program offers one solution to the following truth table for the Boolean nand operation, which is the negation of the and operation (not (p and q)).

p q p nand q
true true false
true false true
false true true
false false true

Using the four possible input combinations, run the program to verify its correctness.

Observe that only the first combination - p and q both true - causes the nand result to be false. Restructure your code so that it uses a single if and else to achieve the same functionality. (Test only whether the combination defined in the first row of the truth table is the one you have).

Compile and run the program. Once it is correct, submit it. If you cannot determine the proper test expression, ask for help.

Close the file.

 

A Common Mistake

Consider the following program Mystery.java. What is its output? Record your guess as a comment in the file.

Open the file Mystery.java and then compile and run it. Is the output the same as your guess?

Many people when reading the program do not notice that an = was used rather than an ==. A single = is the assignment operator (i.e., it’s not the equality operator). The value of an assignment operation is the value assigned to the variable. For this assignment, the value is false. Therefore, the test expression is false and the else action is executed.

Correct the program so that an equality test is made. Compile and run the program.

Once the program is correct submit it.

The purpose of this exercise is to encourage you to pay strict attention when developing test expressions. Make sure the expression is what you want!

Close the file.

 

A Simple Prediction

Examine the following program Predict.java. Determine what inputs are needed for the string “1” to be displayed, what inputs are needed for the string “2” to be displayed, and so for strings “3”, “4”, and “5”.

Record your answers as a comment in the file. Show two ways for “4” to be displayed. Are there others?  Put the answers in the Predict.java file as a comment, and then submit it.

Close the file.

 

Finishing up

Copy any files you wish to keep to your own drive.

Delete all of the files you copied or created on the laboratory machine.