Howdy. 

Get access, pay $42.20, upgrade to eText with access code. 

Chapter 1: Introduction. 

Chapter 2: Data and Operators. 

Chapter 3: Using Objects.

Chapter 4: Writing Classes (this is where it really starts). 

Homework One due Friday 01/23

Lab Two due Monday 01/26

http://www.cs.indiana.edu/classes/c211/gru.jpg

We read Homework One, Problem Two and the question is:

  How do you get true and false in Java?

Let's position ourselves in the Interactions Panel of DrJava:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> 1 + 1
2
> 1 < 1
false
> 1 == 1
true
> 1 <= 1
true
> 49 % 4 == 0
false
>

So we start by discussing relational operators. 

I will post a sample Early Exam this weekend. 

It will help us focus next week as we approach our own Early Exam.

Example exercise: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> "a"
"a"
> 'a'
'a'
> "abc"
"abc"
> 'abc'
Lexical error at line 1, column 3.  Encountered: "b" (98), after : "\'a"


Notice that 'a' is a character and "a" is a String of length one. 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> "12:34"
"12:34"
> "12" + ":" + "34"
"12:34"
> 12 + ":" + 34
"12:34"
> 1 + 2
3
> "1" + 2
"12"
> '1' + 2
51


ASCII table:

http://www.ascii-code.com/

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> 1
1
> '1'
'1'
> 1 - 0
1
> '1' - 0
49
> 'a' - 0
97
>

What's the difference between '1' and 1

The first one is a char(acter)

The second one is an int(eger)

Types in Java:

  (a) primitive types

         int, long, short, byte

         double, float 

         char

         boolean 

  (b) classes, user-defined types, reference types 

Practice expressions:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> ';'
';'
> ';' + 0
59
> 4 
4
> + 4
4
> - 4
-4
> 5
5
> + 'n'
110
> - 'n'
-110
> Math.abs( - ';' );
> Math.abs( - ';' )
59
> Math.abs( - 'n' )
110


Let's think about this:

You are getting a number. 

Whole number. 

If it's less than 21 and even it's a winner.

If it's above 21 and multiple of 3 it's also a winner. 

Otherwise it's not a winner. 

Write a program that reads a number and returns true/false accordingly. 

int number = Integer.parseInt( in.nextLine() ); 

boolean answer = (number < 21 && (number % 2 == 0)) || 
                 (number >= 21 && (number % 3 == 0));

So the program will look like this:

import java.util.Scanner;

class Winner {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in); 
    System.out.print("Enter number: "); 
    int number = Integer.parseInt( in.nextLine() ); 
    boolean caseOne = (number < 21 && (number % 2 == 0));
    boolean caseTwo = (number >= 21 && (number % 3 == 0)); 
    boolean answer = caseOne || caseTwo; 
    System.out.println( answer ); 
  } 
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> 1 < 2
true
> 1 > 2
false
> boolean a = true
> a
true
> boolean b = false
> a
true
> b
false
> a && b
false
> a || b
true
> ! a
false
> a
true
> !b 
true
> b
false