Lab Two solutions and grading remarks:

1. We know b is false and x is 0. 

 (a) false 

Note that the expression is 

   b && x == 0 

and not 

   b && (x == 0) 

which would be redundant given the precedence of operators. 

Notice also that 

   (b && x) == 0 does not compile, and for a good reason, which means clearly that == binds tighter than && (as we know). 

The rest are easy (immediate) based on truth tables of the operators involved.

 (b)  b || x == 0    // true 
 (c) !b && x == 0    // true 
 (d) !b || x == 0    // true 
 (e)  b && x != 0    // false 
 (f)  b || x != 0    // false 
 (g) !b && x != 0    // false 
 (h) !b || x != 0    // true 

Grading rubric: make sure each question is answered correctly, otherwise take points off, in proportion. 

If they have a program that confirms the answers, commend them: 

class One {
  public static void main(String[] args) {
    boolean b = false; 
    int x = 0; 

    System.out.println(  b || x == 0 ); 
    System.out.println( !b && x == 0 );
    System.out.println( !b || x == 0 );
    System.out.println(  b && x != 0 );
    System.out.println(  b || x != 0 );
    System.out.println( !b && x != 0 );
    System.out.println( !b || x != 0 );

  }
}

2. Simplify the following expressions:

 (a)    b == true       is the same as      b 
 (b)    b == false      is the same as     !b
 (c)    b != true       is the same as     !b 
 (d)    b != false      is the same as      b

Proof can be by truth table: 

   b         b != true       !b 
--------------------------------------
  true       false           false 
  false      true            true 

Note the last two columns, they're identical. 

Having a program like this doesn't hurt either: 

public class Something {
  public static void main(String[] args) {

    { boolean b = true;
      System.out.println( (b == true) + " same as " + b );
      b = false; 
      System.out.println( (b == true) + " same as " + b );
    }

    { boolean b = true;
      System.out.println( (b == false) + " same as " + !b );
      b = false; 
      System.out.println( (b == false) + " same as " + !b );
    }

    { boolean b = true;
      System.out.println( (b != true) + " same as " + !b );
      b = false; 
      System.out.println( (b != true) + " same as " + !b );
    }

    { boolean b = true;
      System.out.println( (b != false) + " same as " + b );
      b = false; 
      System.out.println( (b != false) + " same as " + b );
    }
  }
}

3. Here, b is a variable of type boolean and n is a variable of type int.

 (a) if (n == 0) { b = true; } else { b = false; }

Simplification: 

     b = (n == 0); 

Hint: What is the value of n == 0 ?

Answer: a boolean. Specifically the boolean value we want in b. 

 (b) if (n == 0) { b = false; } else { b = true; }

Simplification: 

     b = (n != 0); 

 (c) b = false; if (n > 1) { if (n > 2) { b = true; } }

Simplifcation: 

     b = (n > 2); 

 (d) if (n < 1) { b = true; } else { b = n > 2; }  

Simplification:

    b = (n < 1) || (n > 2); // call this (d')

Proof of this last one: 

  n     |            1             2            
--------+-----------------------------------------
n < 1   |  T T T T T F F F F F F F F F F F F F F 
--------+-----------------------------------------
n > 2   |  F F F F F F F F F F F F T T T T T T T 
--------+-----------------------------------------
 (d)    |  T T T T T F F F F F F F T T T T T T T  // think about it 
--------+-----------------------------------------
 (d')   |  T T T T T F F F F F F F T T T T T T T  // just apply the or 
--------+-----------------------------------------

4. Has already been discussed and presented in class:

http://silo.cs.indiana.edu:8346/c212/fall2015/0904a.phps

Note the very last problem is the solution. 

The one above is the clue (or first step). 

5. Assuming that b is a boolean variable please simplify:

 (a)  b && !b      // false 
 (b)  b || !b      // true 
 (c)  b && true    // b 
 (d)  b || true    // true 
 (e)  b && false   // false 
 (f)  b || false   // b 


6. Assuming that x is an integer variable please simplify:

 (a)  (x >  3) && (x <   5)   // x == 4 // x is an int, remember? 
 (b)  (x >  3) || (x <   5)   // true 
 (c)  (x >  5) && (x <   3)   // false 
 (d)  (x <  3) && (x <  25)   // x < 3 (easy to prove too)
 (e)  (x <  3) || (x <  25)   // x < 25
 (f)  (x >= 3) && (x >= 25)   // x >= 25

7. Consider the following code fragment when embedded in a complete program.

Assume that x has a value of 6 at the beginning of fragment.

What value does the variable y hold after the fragment gets executed? Why?

  if (x > 3) { // yes, 6 > 3
    if (x <= 5) // no, 6 <= 5 is false 
      y = 1; 
    else if (x != 6) // so we come here and we ask: 6 != 6
      y = 2;
    // and because 6 != 6 is false we come here and there's nothing else for us to do 
  } else
    y = 3; // we get here only if we have an x <= 3 to start with which is not the case 
  // conclusion: y is unchanged. Whatever it was, it stays the same. 

Same question if you remove the curly braces. Explain your answer(s).

  if (x > 3) // so we remove the curly braces  
    if (x <= 5)
      y = 1;
    else if (x != 6)
      y = 2;
  else // do you see how this else now binds to the immediately preceding if? 
    y = 3; // that's the answer this time 

8. Assume int j = 3;

What happens when the following expression gets executed?

   j = ++j + j++;
//       4 since j just became 4 
//           4 and j becomes 5
// j becomes 8 (the 5 goes away)

9. What is the output of the following program? Explain.

class One {
  public static void main(String[] args) {
    int x = 3, y = 5;
    int b = x++ + y++ - ++y - ++x;
//          3 and x becomes 4
//                5 and y becomes 6 
//                        7 since y just became 7 
//                               5 since x just became 5 
    System.out.println( x + " " + y + " " + b );
//                      5         7        -4
  }
}


10. What's wrong with the following code? Explain your answer.

  if (2 < 1) ; { // that's an empty then branch right there 
    System.out.println( "Oops." );
  }


11. The following code illustrates anonymous blocks.

class One {
  public static void main(String[] args) {
    { // first block starts here 
      int i = 5, j = 2;
      System.out.println( i + j );
    } // and ends here 
    { // second block starts here 
      String i = "5";
      char j = '2';
      System.out.println( i + j );
    } // and ends here 
  }
}

What happens when you try to compile and run this code? Why?

Answer: each block creates a separate scope for their i and j. 

The program prints: 

    7
    52

12. Consider the following two program fragments. Assume that x is an int variable.

   // fragment 1                 // fragment 2

   if (x == 5)                   if (x == 5)
     x = x + 1;                    x = x + 1; 
   else                          if (x != 5)
     x = 8;                        x = 8; 

Are these two fragments equivalent? Why or why not?

Answer: it's easy to first try find a counterexample. 

For every number other than 5 the fragments act identically (x is set to 8). 

But if x is 5 then the two separate if statements on the right will eventually change x to 8 (in two steps). 

However on the left x is turned into 6. Enough for us to have proved that the two fragments are not equivalent. 

13. Consider the following code fragment, what does it print?

  String plum = "coconut";
  System.out.println(plum.substring(0, "plum".length());

Answer: "coco" since "plum".length() is 4. 

14. Consider the following code fragment, what does it print?

  String plum = "coconut";
  System.out.println((char)('Q' - plum.length())); 

Answer: lots of things going on there. 

First off plum.length() is 8 (it's plum not "plum" now, and therefore, it's "coconut"). 

Then we have (try it out in DrJava):

  > (char)('Q' - 8)
  'I'
  >  

Characters are deep inside numbers, but they can be cast back to their alphanumeric representations... 

15. What is the output of the following code when embedded in a complete program?

  int x = 3;
  if (x > 5) {
    if (x < 10)
      System.out.print(1); }
  else
    System.out.print(2);
  System.out.print(3); 

Answer: trace it to determine that 23 is printed. 

What if we remove those curly braces?

Answer: now the else binds to the second if and we print 3 only.

This is the second time we have seen a "dangling else" today. 

16. Consider the following fragment, what does it print? Why?

   int x = 3, y = 11;
   x = x + y;
   y = x - y;
   x = x - y;
   System.out.println("(" + x + ", " + y + ")"); 

Answer: the variables are being swapped. 

17. A year with 366 days is called a leap year. A year is a leap year 
    if it is divisible by 4 (for example, the year 1980 was a leap year), 
    except it is not a leap year if it is divisible by 100 (for example, 
    the year 1900 was not a leap year); however, it is a leap year if it 
    is divisible by 400 (for example, the year 2000 was leap year). There 
    were no exceptions before the introduction of the Gregorian calendar 
    on Oct 15, 1582 (as an example, the year 1500 was a leap year). Write 
    a program that asks the user for a leap year and determines if the year 
    entered by the user is leap or not. [Diagram].

class Leap {
  public static void main(String[] args) {
    int year = Integer.parseInt(args[0]);
    if (year % 4 == 0) { // year is divisible by four
      if (year < 1582) {
        System.out.println(year + " definitely a leap year.");
      } else { // year is new and divisible by four
        if (year % 100 == 0) { // danger, it may not be a leap year!
          // what do I do? Postpone for now.
          if (year % 400 == 0) {
            System.out.println("Leap year because div by 4 and 100 and also by 400.");
          } else {
            System.out.println("Not a leap year because div by 4 and 100 but not 400.");
          }
        } else { // new year but not a scary year
          // do I have the answer?
          System.out.println(year + " definitely a leap year.");
        }
      }
    } else { // year is not divisible by four
      System.out.println(year + " not a leap year.");
    }
  }
}

--

Perhaps I will add a few more remarks above specifically about grading tonight (--dag 06/25 @4:27pm).