Here are the answers to Lab 03 exercises: 

1. (a) "Hello, Bill!" is printed (without double quotes)

   (b) greeting = greeting.toLowerCase(); // second line in main 

The reason for (a) is: String objects are immutable. 

2. It calculates 3 * (1 + 2) with BigDecimals. 

   Assume BigDecimal d = new BigDecimal("4"); 

   1 + 2                   is    a.add(b)

   2 * 3                   is    b.multiply(c)

   (1 + (2 + (3 + 4)))     is    a.add(b.add(c.add(d)))

   (1 - (2 - (3 - 4)))     is    a.subtract(b.subtract(c.subtract(d)))

   1 + 2 + 3 + 4           is    ((a.add(b)).add(c)).add(d)

   1 - 2 - 3 - 4           is    ((a.subtract(b)).subtract(c)).subtract(d)

   2 * 3 + 4 * 5           is    (b.multiply(c)).add(d.multiply(new BigDecimal("5"))))

Proof:

import java.math.*;

public class Two {
  public static void main(String[] args) {
    BigDecimal a = new BigDecimal("1");
    BigDecimal b = new BigDecimal("2");
    BigDecimal c = new BigDecimal("3");

    System.out.println( c.multiply(a.add(b)) ); //                                                    9
    
    BigDecimal d = new BigDecimal("4"); 
    
    System.out.println( a.add(b) ); // 1 + 2                                                          3
    System.out.println( b.multiply(c) ); // 2 * 3                                                     6
    System.out.println( a.add(b.add(c.add(d)))); // (1 + (2 + (3 + 4)))                              10
    System.out.println( a.subtract(b.subtract(c.subtract(d))) ); // (1 - (2 - (3 - 4)))              -2
    System.out.println( ((a.add(b)).add(c)).add(d) ); // 1 + 2 + 3 + 4                               10
    System.out.println( ((a.subtract(b)).subtract(c)).subtract(d) ); // 1 - 2 - 3 - 4                -8
    System.out.println( (b.multiply(c)).add(d.multiply(new BigDecimal("5")))); // 2 * 3 + 4 * 5      26

  }
}

3. Here's the program that is testing both:

public class Three {
  public static void main(String[] args) {
    { boolean p = false, q = false; 
      System.out.println( (! (p || q)) == (!p && !q) ); // should print true
      p = true;  
      System.out.println( (! (p || q)) == (!p && !q) ); // should print true
      p = false; 
      q = true;
      System.out.println( (! (p || q)) == (!p && !q) ); // should print true
      p = true; 
      System.out.println( (! (p || q)) == (!p && !q) ); // should print true
    }
    // second part of exercise 3 in lab 03
    System.out.println ( Three.test( false, false ) ); // should print true 
    System.out.println ( Three.test( false, true  ) ); // should print true 
    System.out.println ( Three.test( true , false ) ); // should print true 
    System.out.println ( Three.test( true , true  ) ); // should print true 
  }
  public static boolean test(boolean p, boolean q) {
    // testing the second law of De Morgan 
    return (! (p && q)) == (!p || !q);
  }
}

4. The output is false because that's the value of true && false 

   Reason for that: ! binds tighter than &&

   If we add parens the result is true because ! false is true. 

5. a[0] is 1 so a[a[0]] is a[1] which is 2
   ----           ---        -
   a[a[0]] is 2 so a[a[a[0]]] is a[2] which is 3
   -------           -------  
   a[a[a[0]]] is 3 so a[a[a[a[0]]]] is a[3] which is 4
   ----------           ---------- 

Likewise 

   a[1+a[1+a[0]]] is 
   a[1+a[1+  1 ]] which is
   a[1+a[   2  ]] which is 
   a[1+    3    ] which is 
   a[    4      ] which is 5

Proof: 

public class Five {
  public static void main(String[] args) {
    int[] a = { 1, 2, 3, 4, 5};
    System.out.println( a[a[a[a[0]]]] ); // prints 4
    System.out.println( a[1 + a[1 + a[0]]] ); // prints 5
  }
}

--