go to previous page   go to home page   go to next page hear noise

Fill the blank so that weight is tested in two ways:

Answer:

    // check that the weight is within range
    if ( weight >= 136 && weight <= 147 )
      System.out.println("In range!" );
    else
      System.out.println("Out of range." );

Another correct answer is:

    // check that the weight is within range
    if ( 136 <= weight && weight <= 147 )
      System.out.println("In range!" );
    else
      System.out.println("Out of range." );

Here is another correct (but unclear) answer:

    // check that the weight is within range
    if ( weight < 148 &&  135 < weight )
      System.out.println("In range!" );
    else
      System.out.println("Out of range." );

There are more than a dozen correct answers. But the best answers are both correct and clear.


Many Correct Answers

The boxer must weigh enough (weight >= 136), and must also not weigh too much (weight <= 147). The results of the two tests are combined with the and-operator, &&.

A common mistake is to use the wrong operands with the relational operators. The following does not work:

if ( 136 <= weight <= 147 )  // wrong

The above is incorrect because the first <= will produce a boolean, and now the second <= would try to compare a boolean to an integer.

Here is a JavaScript version of the program:




if ( weight >= 136 && weight <= 147 ) 
  System.out.println("In Range")
else
  System.out.println("Out of Range")


How heavy is the boxer? 


QUESTION 10:

Try the program with the weight 140.