Computer Science 100

Computational Expression

Gregory M. Kapfhammer

flickr photo by shared under a CC (BY) license

Color Scheme

Key Concept

Corresponding Diagram

In-Class Discussion

In-Class Activity

Details in the Textbook

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






How should we organize the declarations in our programs?

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

Use a println statement to display all of the values

Add a single-line comment to the source code

See the README file for full details about the requirements

If this does not work, make sure to fix your program!

What were your experiences?


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


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(

What is the purpose of this line of code?

Review the Scanner methods listed in Figure 2.7

Let's Try It!

Refresh your in-class GitHub repository

git remote add download

git pull download master

What is the output of this program?

How does this program use the Scanner class?

Use comments to explain how works