JMU JMU - Department of Computer Science
Help Tools
Lab: Gaining Experience with Conditions and Decisions


Instructions: Answer the following questions one at a time. After answering each question, check your answer (by clicking on the check-mark icon if it is available) before proceeding to the next question.

Getting Ready: Before going any further, you should:

  1. Download the following files:
    to an appropriate directory/folder. (In most browsers/OSs, the easiest way to do this is by right-clicking/control-clicking on each of the links above.)

1. Basic Syntax: This portion of the lab will help you determine whether you understand the syntax of if statements. (Note: Some variable declarations may be omitted for simplicity. The necessary declarations should be obvious.)
  1. Consider the following fragment:
    BasicExamples.java (Fragment: 1)
                   a = 0;
           if (a > 0) JMUConsole.println("Positive");
           else       JMUConsole.println("Non-Positive");
            
  2. What would be printed by the fragment above if it were compiled and executed properly? (Note: Don't yet create a program that includes this fragment, trace the code yourself.)


    Non-Positive
    
    Expand
  3. Why?


    The boolean expression a > 0 (involving the binary relational operator >) evaluates to false because a contains the value 0 (which is not greater than 0).
    Expand
  4. Re-write the fragment above putting each call to JMUConsole.println() on its own line (without using a { or }).


    BasicExamples-Solution.java (Fragment: 1a)
                   a = 0;
           if (a > 0) 
              JMUConsole.println("Positive");
           else       
              JMUConsole.println("Non-Positive");
            
    Expand
  5. Re-write the fragment above putting each call to JMUConsole.println() in its own block (i.e., inside of a { and }).


    BasicExamples-Solution.java (Fragment: 1b)
                   a = 0;
           if (a > 0) 
           {
              JMUConsole.println("Positive");
           }
           else       
           {
              JMUConsole.println("Non-Positive");
           }       
            
    Expand
  6. Consider the following fragment:
    BasicExamples.java (Fragment: 2A)
                   a = 0;
           if      (a >  0) JMUConsole.println("Positive");
           else if (a == 0) JMUConsole.println("Zero");       
           else             JMUConsole.println("Negative");
            
  7. What would be printed by the fragment above if it were compiled and executed properly? (Note: Don't yet create a program that includes this fragment, trace the code yourself.)


    Zero
    
    Expand
  8. The body of the first else clause contains a single statement. What is it? (Note: Be careful and write it on a single line so that there is no ambiguity.)


    if (a == 0) JMUConsole.println("Zero"); else JMUConsole.println("Negative");
    
    Expand
  9. Why does this fragment generate the output it does??


    The boolean expression a > 0 evalutes to false so control enters the body of the else clause, which is another if statement. The boolean expression in this ifstatement is a == 0 which evaluates to true. So, control enters the body of the if clause and not the body of the else clause.
    Expand
  10. Re-write the fragment above putting each call to JMUConsole.println() on its own line (without using a { or }).


    BasicExamples-Solution.java (Fragment: 2a)
                   a = 0;
           if (a > 0) 
              JMUConsole.println("Positive");
           else if (a == 0)
              JMUConsole.println("Zero");
           else       
              JMUConsole.println("Negative");
            
    Expand
  11. Re-write the fragment above putting each call to JMUConsole.println() in its own block (i.e., inside of a { and }).


    BasicExamples-Solution.java (Fragment: 2b)
                   a = 0;
           if (a > 0) 
           {
              JMUConsole.println("Positive");
           }
           else if (a == 0)
           {
              JMUConsole.println("Zero");
           }
           else       
           {
              JMUConsole.println("Negative");
           }
            
    Expand
  12. Consider the following fragment:
    BasicExamples.java (Fragment: 2B)
                   a = 0;
           if
           {
               (a >  0) JMUConsole.println("Positive");
           }       
           else
           {
               if (a == 0) JMUConsole.println("Zero");       
               JMUConsole.println("Negative");
           }
            
  13. What would be printed by the fragment above if it were compiled and executed properly? (Note: Don't yet create a program that includes this fragment, trace the code yourself.)


    Zero
    Negative
    
    Expand
  14. Why?


    Since a contains the value 0, the boolean expression a > 0 evaluates to false and control enters the body of the else clausew. There are two statements in the else clause. In the if statement, the boolean expression a == 0 evaluates to true so the body of the if clause is entered and Zero is printed. Then the next statement is executed and Negative is printed.
    Expand
  15. Consider the following fragment:
    BasicExamples.java (Fragment: 3A)
                   a = 1;
           if (a > 4) ;
               JMUConsole.println("A");
           JMUConsole.println("B");
            
  16. What would be printed by the fragment above if it were compiled and executed properly? (Note: Don't yet create a program that includes this fragment, trace the code yourself.)


    A
    B
    
    Expand
  17. Create a small application in Java that contains the fragment above. Note: Copy and paste the fragment so that you don't make any mistakes.
  18. Compile and execute the application.
  19. What was printed?


    A
    B
    
    Expand
  20. Why?


    The statement to be executed when the condition evaluates to true (i.e., the body of the if clause) is empty. The other two statements are not part of the if statement (despite the fact that the first statement is indented and appears to be the body of the if clause).
    Expand
  21. Consider the following fragment:
    BasicExamples.java (Fragment: 3B)
                   a = 1;
           if (a > 4) ;
           {
               JMUConsole.println("A");
           }
           JMUConsole.println("B");
            
  22. What would be printed by the fragment above if it were compiled and executed properly? (Note: Don't yet create a program that includes this fragment, trace the code yourself.)


    A
    B
    
    Expand
  23. Why?


    For the same reason - the brackets don't change anything. The body of the if clause is still empty.
    Expand
  24. Consider the following fragment:
    BasicExamples.java (Fragment: 4)
                   a = 5;
           if (a > 4)
               JMUConsole.println("C");
           else ;
               JMUConsole.println("D");
            
  25. What would be printed by the fragment above if it were compiled and executed properly?


    C
    D
    
    Expand
  26. Create a small application in Java that contains the fragment above. (Copy and past the fragment so that you don't make any mistakes.)
  27. Compile and execute the application.
  28. What was printed?


    C
    D
    
    Expand
  29. Why?


    This time, the statement to be executed when the condition evaluates to false (i.e., the body of the else clause) is empty. Since the condition evaluate to true a "C" is printed. Since the other statement is not part of the if or else, it is executed and a "D" is printed.
    Expand
2. Basic Logic and Rules of Inference: This portion of the lab will help you understand some basic logical operations and some rules of inference. (Note: Some variable declarations may be omitted for simplicity. The necessary declarations should be obvious.)
  1. Consider the following fragments:
    LogicExamples.java (Fragment: 1)
            
           // Fragment a
           if (price > 100) {
              tax = 5;          
           } else {
              tax = 0;          
           }
    
    
           // Fragment b
           if (price <= 100) {
              tax = 0;          
           } else {
              tax = 5;          
           }
    
            
  2. Are these two fragments equivalent (i.e., will they always produce the same results)? If not, explain.


    Yes
    Expand
  3. Consider the following fragments:
    LogicExamples.java (Fragment: 2)
            
           // Fragment c
           tax = 0;       
           if (price >= 200) {
              tax = 10;          
           }
    
    
           // Fragment d
           tax = 10;       
           if ( !(price >= 200) ) {
              tax = 0;          
           }
    
    
           // Fragment e
           if (price >= 200) {
              tax = 10;          
           } else {
              tax = 0;          
           }
    
            
  4. Are these three fragments equivalent (i.e., will they always produce the same results)? If not, explain.


    Yes
    Expand
  5. How many operators are in the boolean expression in the ifstatement of Fragment d? What are they, what kinds of operations are they, and what are their operands?


    There are two. >= is a relational operator that has two numeric operands, price and 200. ! is a unary logical operator, its operand is (price >= 200).
    Expand
  6. Re-write the boolean experssion in Fragment d in such a way that it does not use the ! operator. You may use a different relational operator, but you must not change the operands or their order.


    price < 200
    Expand
  7. Re-write Fragment d so that it uses this boolean expression and results in tax containing the same value after it is executed.


           tax = 10;       
           if (price < 200)
           {
              tax = 0;          
           }
    
    Expand
  8. Consider the following fragments:
    LogicExamples.java (Fragment: 3)
            
           // Fragment f
           tax = 0;       
           if (price >= 500) {
              tax = 50;          
           }
    
    
           // Fragment g
           if (price < 500) {
              tax = 0;          
           }
           tax = 50;       
    
            
  9. Are these two fragments equivalent (i.e., will they always produce the same results)? If not, explain.


    No.

    Fragment f will result in tax containing the value 50 when price is greate than or equal to 500 and 0 otherwise.

    Fragment g will always result in tax containing the value 50.

    Expand
  10. Consider the following fragment:
    LogicExamples.java (Fragment: xor)
                   boolean    movie1, movie2;       
    
           if (movie1 ^ movie2) {
              JMUConsole.println("OK");
           }
            
  11. Re-write the fragment above in Java using only the logical operators &&, ||, and !.


    LogicExamples-Solution.java (Fragment: xor)
                   boolean    movie1, movie2;       
    
           //   movie1 or movie2    and   not both movie1 and movie2
           if ((movie1 || movie2)    &&   (!(movie1 && movie2))) {
              JMUConsole.println("OK");
           }
            
    Expand
3. Nesting: This portion of the lab will help you understand nested if statements. (Note: Some variable declarations may be omitted for simplicity. The necessary declarations should be obvious.)
  1. Consider the following fragment:
    NestingExamples.java (Fragment: dink)
                   numberOfIncomes = 2;
           numberOfKids    = 0;
           
           if (numberOfIncomes == 2) {
              if (numberOfKids == 0) {
                 JMUConsole.println("DINK");
              } else {
                 JMUConsole.println("Not a DINK");
              }
           }
            
  2. What would be printed by the fragment above if it were compiled and executed properly?


    DINK
    
    Expand
  3. Re-write the fragment above without using nested if statements.


    NestingExamples-Solution.java (Fragment: dink)
                   numberOfIncomes = 0;
           numberOfKids    = 0;
           
           if ((numberOfIncomes == 2) && (numberOfKids == 0)) {
              JMUConsole.println("DINK");
           } else {
              JMUConsole.println("Not a DINK");
           }
            
    Expand
  4. Consider the following fragment:
    NestingExamples.java (Fragment: category)
                   value = 5;
           group = 1;    
           
           if (value < 100) {
              if (group < 0) {
                 category = 1;
              } else {
                 category = 2;
              }
           } else {
              category = 3;
           }
        
           JMUConsole.println(category);    
            
  5. What would be printed by the fragment above if it were compiled and executed properly?


    2
    
    Expand
  6. Re-write the fragment above without using nested if statements.


    NestingExamples-Solution.java (Fragment: category)
                   value = 5;
           group = 1;    
           
           category = 3;
           
           if ((value < 100) && (group < 0)) {
                 category = 1;
           }
    
           if ((value < 100) && (group >= 0)) {
              category = 2;
           }
        
           JMUConsole.println(category);    
            
    Expand

Copyright 2019