Notes on Chapter 13: Single branch if

by _____________________________________

Fill in the information about these decision making programs and statements:

import java.util.Scanner ;
public class CookieDecision {
  public static void main (String[] args)    {
    Scanner scan = new Scanner( System.in );
    int hunger, look, smell ;
    System.out.print("How hungry are you?            (1-10): ");
    hunger = scan.nextInt();
    System.out.print("How nice do the cookies look?  (1-10): ");
    look   = scan.nextInt();
    System.out.print("How nice do the cookies smell? (1-10): ");
    smell  = scan.nextInt();

    if ( (hunger + look + smell ) > 15 )
      System.out.println("Buy cookies!"  );

    System.out.println("Continue down the Mall.");
  }
}
  1. How many variables does this program use? _______
  2. The if statment asks a question comparing two _______________.
  3. If the answer is true the true branch is executed.
    If the answer is false______________________________
  4. In both cases, execution continues with ________________________ the true branch.
  5. The user rates hunger, appearance, and aroma as 4, 6, and 9, respectively. Does the user buy cookies? ____________
  6. Arithmetic operators have _________________ precedence than relational operators, and so are done _____________.
  7. Can you rewrite
    if ( (hunger + look + smell ) > 15 )
    without parentheses and have it work the same way? _______

Evaluate each of the following Boolean expressions (to true or false):

12 + 4 > 16 5.2 - 1.2 <= 2.0+2 2*3-4 == 1+1
     

Is the following a correct if statement? _________
if ( booleanExpression ){
    one or more statements
}else{
    one or more statements }
import java.util.Scanner;
class SweaterPurchase {
  public static void main (String[] args)   { 
    final int price = 4495;    // price in cents
    Scanner scan = new Scanner( System.in ); 
    int cash;                       

    System.out.println("How much do you have, in pennies?");
    cash   = scan.nextInt();     
    
    if ( __________________________________________  )
      System.out.println("You can buy the sweater." );
    else
    {
      System.out.println("You can't buy the sweater." );
      System.out.println("You need " + (price-cash) + " more cents." );
    }
  }
}

The reserved word _________ says that the value held in price will not change during the run of the program.

Why pennies? Programs that deal with financial matters frequently do their calculations using ______________. As you have seen,_____________________ is not exact.

Boolean expressions are always true or false. Use the correct relational operator (==, >, <, >=, <=, != ) to ask a question that is true when you want the true branch to be executed. If the statements inside of the two branches are reversed, pick a ______________________________ so the program does the same thing as before.

What is the "opposite" of   <= ? ___________