Howdy. 

It was hoped that you would grade your exams in lab and post a report
in OnCourse (which is still open at the moment). I wanted to know what
you thought about your written test and I wanted you to try to get back
some of the points that you may have missed. 

Example 12:

If b is a boolean please simplify  

   b == true 

Possible answer:

   true 

Is this a simplification? No, because although shorter this expression
is not equivalent. How can you prove that b == true and true are not two
equivalent boolean expressions? 

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

For two expressions to be equivalent they need to evaluate to the
same exact value for each one of their inputs. 

Another possible answer: 

  b

Simplify x + 0 when x is a number: answer is x

Simplify x * 0 when x is a number: 0 

Second question: simplify 

   b == false 

Answer:

   !b

Another answer:

   b != true         is this a simplification of b == false 

The answer is yes but it's not the best. 

Question #23.

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

To prove that they are equivalent you try to find counterexamples. 

If you can't you hope you can articulate a reason why. 

There is a counterexample: 5. 

x = 5 becomes 6 in the first example and 8 in the second. 

Question 16. If n is 5 what is the value of n++? 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> int n = 5
> n
5
> n++
5
> n
6


Any other way to prove this? 

class One {
  public static void main(String[] args) {
    int n = 5;
    System.out.println( n ); // 5
    System.out.println( n++ ); // prints 5 then makes n 6
    System.out.println( n ); // 6

  }
}

Compile and run and you get:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run One
5
5
6


Let's take this code fragment:

   int i = 0; 
   while (i < 10) ; {
     i = i + 1; 
   }

Does this program ever terminate? If so in how many iterations? 

Who could ever write such a program? 

A beginner student wrote this. 

Every test will be a learning experience. 

You are aware of Math.sqrt(...)

Please provide a definition for it. 

                    number
                      6 
----------------------------
0                     6
low                   high 

           3
         guess 

3 * 3 is 9 > 6 what do we do? 

          high 

  guess
   1.5

1.5 * 1.5 is 2.25 < 6 so what do I do? 


   low 

        2.25
       guess 

So I propose the following: 

class Math {
  static double fun(double low, double high, double number) {
    return 3; 
  }
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Math.fun(1, 2, 3)
3.0
> Math.fun(0, 0, 0)
3.0


class Math {
  static double fun(double low, double high, double number) {
    return Math.PI; 
  }
}

class Math {
  static double fun(double low, double high, double number) {
    return java.lang.Math.PI; 
  }
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Math.fun(0, 0, 0)
3.141592653589793


class Math {
  static double sqrt(double number) {
    return Math.fun(0, number, number);  
  }
  static double fun(double low, double high, double number) {
    double guess = (high + low) / 2; 
    if (  (high - low) < java.lang.Math.pow(10, -4) )  {
      return guess; 
    } else {
      if (guess * guess > number) {
        return Math.fun( low, guess, number );  
      } else if (guess * guess < number) {
        return Math.fun(guess, high, number);  
      } else {
        return guess; 
      }
    }
  }
}

class Math {
  static double sqrt(double number) {
    return Math.fun(0, number, number);  
  }
  static double fun(double low, double high, double number) {
    double guess = (high + low) / 2; 
    if (  (high - low) < java.lang.Math.pow(10, -12) )  {
      return guess; 
    } else {
      if (guess * guess > number) {
        return Math.fun( low, guess, number );  
      } else if (guess * guess < number) {
        return Math.fun(guess, high, number);  
      } else {
        return guess; 
      }
    }
  }
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Math.sqrt(2)
1.414213562372879
> java.lang.Math.sqrt(2)
1.4142135623730951
> java.lang.Math.sqrt(3)
1.7320508075688772
> Math.sqrt(3)
1.7320508075689531
>

class German {
  static double fun(double in) {
    return in - 2; 
  }
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> German.fun(5)
3.0


class German {
  static double fun(double in) {
    return in - 2; 
  }
  double fun(double a, double b) {
    return a * b;  
  }
}





Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> German.fun(3)
1.0
> German.fun(1, 0)
Static Error: No method in static German with name 'fun' matches this invocation
    Arguments: (int, int)
    Candidate signatures: double fun(double)




class German {
  static double fun(double in) {
    return in - 2; 
  }
  double fun(double a, double b) {
    return a * b;  
  }
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> German g = new German()
> g
German@6f14adc0
> g.fun(1, 0)
0.0
> g.fun(2, 3)
6.0
> g.fun(7)
5.0
>

See you on Wednesday. 

--