Computer Science 100

Computational Expression

Gregory M. Kapfhammer

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

Program Variables

Variables

Types

Declaration

Assignment

Constants

How should we organize the declarations in our programs?

Primitive Types

Integers and floating point values

Bit-depth trade-offs

Resource-constrained devices

Byte values and booleans

What does this look like in memory?


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

Reference Types

ArrayLists and Colors and ...

Classes and objects

Containers to store objects

User-defined and system-provided

What does this look like in memory?


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

Any questions about variables?

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!

Using GitHub and Docker

Needs correctly configured laptop

Use a departmental desktop

Let's Try It!

Enhance 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 each variable

Add a single-line comment to the source code

See the README file for full details about the requirements

Make sure to fix the program so that it passes all checks!

What were your experiences?

Let's Try It!

Return to your in-class GitHub repository

What is the output of this program?

How does this program use the Scanner class?

Use comments to explain how ComputeGasMileage.java works

See the README file for full details about the requirements

Make sure to fix the program so that it passes all checks!

Any questions about these examples?

Finish the in-class programming in about a week

Work to achieve learning objectives

Schedule meeting during office hours

Hands-on practice throughout the semester!

Schedule office hours if you have questions!