DrJava 

silo (Unix machine)

Readings might include Big Java 

In DrJava Interactions panel we can examine.... let's start with expressions. 

Welcome to DrJava.  Working directory is C:\Users\dgerman
> 3
3
> 2.0
2.0
> "abc"
"abc"
> 3 + 2
5
> 3 > 2
true
> true
true
> false
false


In starting with expressions we looked (above) at literals.

I typed literals of the following types: integers, numbers with 
fractional part, booleans, strings and ... there's one more type. 

What is a type? A type is a set of values. 

Boolean is such a (small) set.

String, int, double are bigger sets (still finite due to internal
representation in the computer). 

Besides the set we also need to specify rules of combination (operators). 

Welcome to DrJava.  Working directory is C:\Users\dgerman
> true && true
true
> true && false
false
> false && true
false
> false && false
false


So this is one of the three boolean operators in action. 

Welcome to DrJava.  Working directory is C:\Users\dgerman
> -5
-5
> 6 + 2
8
> 3 / 2
1
> 89 / 10
8
> 89 % 10
9
> 5 % 7 // is this 0, or is it 5?
5
> 5 / 7 
0
> 15 / 7
2
> 15 % 7
1
> -15 % 7 // maybe -1 or maybe 6
-1
>

Let's work a bit with numbers that have a fractional part:

Welcome to DrJava.  Working directory is C:\Users\dgerman
> 3.0 / 2
1.5
> 3 / 2.0
1.5
> 2 / 3 * 1.0 // 1 or 1.0 or 0 or 0.0 or 0.6
0.0


More examples:

Welcome to DrJava.  Working directory is C:\Users\dgerman
> 2.53
2.53
> 2.53 * 100
252.99999999999997
> 2.35 * 100
235.0


More examples:

Welcome to DrJava.  Working directory is C:\Users\dgerman
> 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.4
0.8
> 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
> 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1
0.8999999999999999
> 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1
0.9999999999999999
> 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1
1.0999999999999999
> 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1
1.2


Overloaded operators:

Welcome to DrJava.  Working directory is C:\Users\dgerman
> 2 + 3
5
> "what" + "ever"
"whatever"
> 1 + "2" // does it work, and if so how?  
"12"


Here are some other types:

http://docs.oracle.com/javase/8/docs/api/java/awt/Image.html

http://docs.oracle.com/javase/8/docs/api/javax/swing/JFrame.html

So an Image is a type, a JFrame is a type... 

Welcome to DrJava.  Working directory is C:\Users\dgerman
> "abc"
"abc"
> "a" + "b" + "c"
"abc"
> "automaton"
"automaton"
> "automaton".substring(2, 8)
"tomato"


What about the individual characters in Strings? 

Welcome to DrJava.  Working directory is C:\Users\dgerman
> "automaton".charAt(0)
'a'
> "automaton".charAt(5)
'a'
> 'a' + 1 // error or some number maybe 'b' and who knows what else really?
98
> (char) ('a' + 1) 
'b'


Conversions

Welcome to DrJava.  Working directory is C:\Users\dgerman
> 'a' + 0
97
> '2' + '3' // some number just not 5 
101
> (int) '2'
50
> '2' + ""
"2"
> ('2' + "") + ('3' + "") 
"23"
> "2" + "3"
"23"
> Integer.parseInt( "2" )
2
> Integer.parseInt( "2" ) + Integer.parseInt( "3" )
5


What if I want to convert '2' to 2 directly? 

Welcome to DrJava.  Working directory is C:\Users\dgerman
> Integer.parseInt('2' + "")
2
> '2' + 0
50
> '1' + 0
49
> '0' + 0
48
> 'A' + 0
65
> 'a' + 0
97
> ' ' + 0
32
> '0' + 0
48
> '6' - '0'
6
> ('2' - '0') + ('3' - '0')
5


In Java there are four (eight) primitive types: 

(a) integers  (int, byte, short, long) 

34 is an int

34l is a long

(b) numbers with fractional value (double, float) 

2.3 is a double value

2.3f is the same value as a float 

(c) booleans

true
false 

(d) characters 

'a'
'0' 
' '
'{'

Everything else is a class. 

They are known as: reference types, user-defined types, objects. 

Java programs are made out of classes. 

How many classes? Any number. 

For the most part in the beginning we will have one class. 

The class will have one method, a main. 

import java.util.Scanner; 

class Wednesday {
  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);  
    System.out.print("What's your name? "); 
    String name = s.nextLine(); 
    System.out.print("How old are you, " + name); 
    int age = s.nextInt(); 
    System.out.println(name + " you will be " + (age + 1) + " next year!");
  }
}

It runs like this:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Wednesday
What's your name? Dave
How old are you, Dave? 3 
Dave you will be 4 next year!


On your paper before turning it in

  write code for the simplest program that says "Hello and welcome to C212!"


http://i.cdn.turner.com/si/2010/writers/jack_mccallum/08/15/hof.wrapup/bird-pippen-col-getty.jpg

CatDog, Whatever. 

--