Gregory M. Kapfhammer
Key Concept
Corresponding Diagram
In-Class Discussion
In-Class Activity
Details in the Textbook
What if we want to output a quotation mark?
This character has a special meaning in Java
So, we have to escape it!
Please refer to Figure 2.1 in your textbook
Escape sequences support formatted output
Types
Declaration
Assignment
Constants
How should we organize the declarations in our programs?
Integers and floating point values
Bit-depth trade-offs
Resource-constrained devices
Character strings and booleans
What does this look like in memory?
int
, double
, float
, boolean
, char
, String
Give each of the variables a suitable default value
Make at least one of the variables a constant
Try to assign a value to the constant two times!
If this does not work, make sure to fix your program!
Arithmetic operators: +, -, *, /, %
Wait, what does %
mean?
Operator precedence
What about parentheses?
Can you draw an expression tree?
Let's try to "get some work done"!
We often want to increase a value by one
You can write count = count + 1
or count++
Which one of these notations do you prefer? Why?
We often want to assign a value to a variable
You can write total = total + 5
or total += 5
Which approach do you like better? Why?
Can we ever transform one data type into another?
We can use widening and shortening conversions
Converting from a byte
to a short
is safe! Why?
See Figure 2.5 for a list of widening conversions
Narrowing conversions can lead to information loss. Why?
Refer to Figure 2.6 for the list of narrowing conversions
Assignment conversion: money = dollars
...
... when dollars is an int
and money is a float
Promotion conversion: result = sum / count
...
... when sum is a float
and count is an int
Casting conversion: dollars = (int) money
...
... when dollars is an int
and money is a float
Wait, what about when we want user-defined values?
We can perform input with the Scanner
Check this out ...
... Scanner scan = new Scanner(System.in)
What is the purpose of this line of code?
Review the Scanner
methods listed in Figure 2.7
Run the git pull
command in your "share" repository
Then, change into the class/
directory
Compile and run the GasMileage.java
program
What is the output of this program?
How does this program use the Scanner
class?
Displaying pixels on the screen
Using a coordinate system
Representing color with RGB values
Try to use the showrgb
command in the terminal!
Can you connect the RGB values to a specific color?
Run the git pull
command in your "share" repository
Then, change into the class/
directory
Compile and run the DrawingTest
program
Be careful! Run it with: java DrawingTest
What is the output of this program?
How does this program create graphical output?
Please enhance this program with a new graphic!
Run the git pull
command in your "share" repository
Then, change into the class/
directory
Compile and run the LogisticMap.java
program
Be careful! Run it with: java LogisticMap 0
What is the output of this program?
How does this program create graphical output?