Lab 01 is due today. 

Homework 01 will close tomorrow. 

Lab 02, Lab 03, Homework 02, Homework 03 etc. 

Challenge: 

  assume n, m are integers 

  please calculate the biggest of the two 

You can use +, -, /, Math.abs(...), n, m, 2. 

No if statements allowed. 

     ( n + m ) / 2 + Math.abs( n - m ) / 2

The answer above is flawed. 

Kevin says:

     ( n + m + Math.abs( n - m )) / 2

He says because it's aesthetically more pleasing. 

Somehow he's right. 

Welcome to DrJava.  Working directory is C:\Users\dgerman
> int n = 5
> int m = 3
> ( n + m ) / 2 + Math.abs( n - m ) / 2
5
> n = 6
6
> m
3
> ( n + m ) / 2 + Math.abs( n - m ) / 2
5
> (( n + m ) + Math.abs( n - m ) ) / 2
6


Now let's look at casting:

Welcome to DrJava.  Working directory is C:\Users\dgerman
> 1 + 2
3
> 4.53 * 100
453.0
> Math.floor(3.141592)
3.0
> Math.floor(Math.PI)
3.0
> Math.PI
3.141592653589793
> int n = 3
> double a = 3.141592
> a
3.141592
> a = 2
2.0
> a
2.0
> n = 3.1
Static Error: Bad types in assignment: from double to int
> n = (int) Math.PI
3
> n
3


In your homework one you have Integer.parseInt(...)

What can we say about parseInt?

Clearly a static member of class Integer. 

The contract is: a String comes in and an int is returned. 

Examples: int n = Integer.parseInt("23"); 

Welcome to DrJava.  Working directory is C:\Users\dgerman
> "23" + 1
"231"
> Integer.parseInt("23")
23
> Integer.parseInt("23") + 1
24


Exercise 10 from Lab 02:

class Ten {
  public static void main(String[] args) {
    
    if (20 < 10) ; 
    
    
    {
      System.out.println( "Oops." );
    }
    
  }
}

This clearly disproves the theory we had and makes us aware of the ;

// fragment 1                 // fragment 2

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


Haoyang, Zach, Xing all say: no. 

They are right and the counterexample is 5 (Levi). 

For all other numbers the two fragments work the same. 

In Java we four kinds of variables:
 
  (a) static variables 

  (b) instance variables 

  (c) local variables

  (d) parameters 

(c) and (d) need to be initialized by the programmer. 

(a) and (b) have default values. 

(a) and (b) need to be called through their class/instance. 

(c) and (d) are just called by their names. 

class Example {
  public static void main(String[] args) { // args is a parameter 
    int n; // local variable
    n = 4; // without this you can't print
    System.out.println(n);     
    
  }
}

Now an example: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> 4.53 * 100
453.0
> 4.35 * 100
434.99999999999994
> 0.1
0.1
> 0.1 + 0.1
0.2
> 0.1 + 0.1 + 0.1
0.30000000000000004
> 0.1 + 0.1 + 0.1 + 0.1
0.4
> 0.1 + 0.1 + 0.1 + 0.1 + 0.1
0.5
> 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1
0.6
> 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1
0.7
> 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1
0.7999999999999999


Because of finite representation we are inherently inaccurate:

import java.math.*;

public class Two {
  public static void main(String[] args) {
    BigDecimal a = new BigDecimal("4.35");
    BigDecimal b = new BigDecimal("100");

    System.out.println( a.multiply(b) );
    
    System.out.println( 4.35 * 100 ); 
  }
}

However that can be fixed as in Scheme with BigDecimal class. 

--