Howdy. 

1. Name, username, date, class, room. 

2. What do you remember from Lecture 01? 

From now on bring to class a piece paper with a half summary entitled:

    Previously on C212

And write down the most important highlights. 

Today let's explore expressions in DrJava Interactions panel. 

Welcome to DrJava.  Working directory is C:\Users\dgerman
> 4
4
> 2 + 3
5
> 4 * 3
12
> Math.pow(2, 3)
8.0
> Math.pow(2, 0.5)
1.4142135623730951
> Math.sqrt(2)
1.4142135623730951


Let's try some more examples: 

Welcome to DrJava.  Working directory is C:\Users\dgerman
> ArrayList<Integer> a = new ArrayList<Integer>();
Static Error: Undefined class 'ArrayList'
> import java.util.ArrayList; // auto-import
ArrayList<Integer> a = new ArrayList<Integer>();
> a
[]
> a.add(4)
true
> a
[4]
> a.add(5)
true
> a.add(-2)
true
> a
[4, 5, -2]
> String b = new String("whatever");
> b
"whatever"
> b.length()
8
> b.substring(0, 4)
"what"


What is String? It is a data type? 

What is a type? What are some Java types? 

boolean is a type. 

A type is a set of values (plus operators defined on that set). 

boolean = { true, false }

Integers are another data type. 

Operators for integers: +, -, *, / 

Welcome to DrJava.  Working directory is C:\Users\dgerman
> 2 \ 3
Lexical error at line 1, column 3.  Encountered: "\\" (92), after : ""
> 2 / 3 // ha ha 2 \ 3 but 2 / 3 is: 1 or 0 or 0.666 
0
> 5 / 3
1
> 1 / 2
0
> 1.0 / 2
0.5


Numbers with fractional part: just another data type. 

Welcome to DrJava.  Working directory is C:\Users\dgerman
> 2 + 3
5
> 0.1 + 0.1
0.2
> "what" + "ever"
"whatever"
> "1" + 2 // maybe error maybe 12 maybe "12"
"12"
> 1 + 2 + "3" // is this "6" or "33" or "123"
"33"
> "1" + 2 + 3 // maybe 15 or maybe "123" or maybe something else 
"123"
> "1" + (2 + 3)
"15"


Order of operations, some operators hagve higher precedence. 

Welcome to DrJava.  Working directory is C:\Users\dgerman
> 1 + 2 * 3
7
> (1 + 2) * 3
9
> 1 / 2 * 6.0
0.0
> 1 / 2.0 * 6
3.0
> 1 / 2.0
0.5


Some more examples and questions: 

Welcome to DrJava.  Working directory is C:\Users\dgerman
> "1" + 2.3
"12.3"
> "1" + 2.3 + 5
"12.35"
> "1" + 2
"12"
> Integer.parseInt( "23" )
23
> Integer.parseInt( "1" ) + 2
3


We can also make some minor conversions by casting: 

Welcome to DrJava.  Working directory is C:\Users\dgerman
> 1 / (double) 2 * 3
1.5
> (double) 3
3.0
> (int) 1.73
1


In Java there are four (eight) prmitive types:

  integers (int, long, byte, short)

23 is an int

23l is a long 

  numbers with fractional part (double, float) 

1.2 is a double 

1.2f is a float 

  booleans (boolean) 

true false 

  characters (char) 

'a' 

Welcome to DrJava.  Working directory is C:\Users\dgerman
> "a" + "b"
"ab"
> 'a'
'a'
> 'b'
'b'
> 'a' + 'b' // maybe "ab" maybe error maybe 2 maybe 'c' or maybe ['a', 'b']
195
> 'a' + 0
97
> 'b' + 0
98
> 'a' + 1
98
> (char) ('a' + 1)
'b'


Other than the four primitive types everything else is a class. 

As a matter of fact even for the primitive types we have wrapper classes. 

This way Java is an entirely object-oriented language. 

Everything else is of 

  reference type 

  user-defined type 

  basically an object. 

Question: assume you have two integers a and b.

I want you to use +, -, /, 2, Math.abs(...) to calculate the bigger of the two. 

Welcome to DrJava.  Working directory is C:\Users\dgerman
> Math.abs(-2)
2
> Math.abs(12)
12


Let me set you up: 

Welcome to DrJava.  Working directory is C:\Users\dgerman
> int a = 3
> a
3
> int b = 5
> b
5
> a + b
8
> a < b
true
> (a + b) / 2 + Math.abs(a - b) / 2
5
> a = 12
12
> a = 13
13
> (a + b) / 2 + Math.abs(a - b) / 2
13
> a = -8
-8
> b
5
> (a + b) / 2 + Math.abs(a - b) / 2
5
> a = 12
12
> (a + b) / 2 + Math.abs(a - b) / 2
11
> ((a + b) + Math.abs(a - b)) / 2
12


One more program: 



--