Computer Science 111:

Introduction to Computer Science I

Chapter Two

Gregory M. Kapfhammer


creative commons licensed ( BY-NC-SA ) flickr photo shared by Ed Yourdon

Character Strings

print and println

What are the similarities and differences?

When is the plus sign not for addition?

String concatenation!

Let's try this out!

Input, compile, and run the program in Listing 2.3

Can you explain this output?

Escape Sequences

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

Variables

Types

Declaration

Assignment

Constants

Primitive Types

Integers and floating point values

Bit-depth trade-offs

Resource-constrained devices

Character strings and booleans

What does this look like in memory?


creative commons licensed ( BY-SA ) flickr photo shared by mcclanahoochie

Let's Try It!

Write one Java program that declares variables of the following types:

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!

Expressions

Arithmetic operators: +, -, *, /, %

Wait, what does % mean?

Operator precedence

What about parentheses?

Can you draw an expression tree?


creative commons licensed ( BY-NC-SA ) flickr photo shared by jannemei

Operators

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?

Data Conversion

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

Conversion Techniques

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

User Input

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

Let's Try It!

Run the git pull command in your "share" repository

Then, change into the class/source/ directory

Compile and run the GasMileage.java program

What is the output of this program?

How does this program use the Scanner class?

Computer Graphics

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?


creative commons licensed ( BY-NC-ND ) flickr photo shared by Russ Allison Loar

Shapes and Colors

Run the git pull command in your "share" repository

Then, change into the class/source/ 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!

Complex Graphics

Run the git pull command in your "share" repository

Then, change into the class/source/ 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?