Computer Science 111:

Introduction to Computer Science I

Gregory M. Kapfhammer


flickr photo by GregKapfhammer shared under a Creative Commons (BY-NC-ND) 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

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

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!

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

Steps to Remember

Typing javac runs the Java compiler

This command is like typing gradle build

Typing java runs the Java program

This command is like typing gradle run

Using gradle is more comprehensive

What were your experiences?

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!

Clone the course repository from GitHub

https://github.com/Allegheny-Computer-Science-111-F2017/cs111-F2017-inclass

Find the directory for this module of the course

Compile and run the ComputeGasMileage.java program

What is the output of this program?

How does this program use the Scanner class?

Explain how ComputeGasMileage.java works

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 "inclass" repository

Then, change into the chapter01to03/ directory

Compile and run the DisplayGraphicalScene program

Be careful! Run it with: java DisplayGraphicalScene

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/ directory

Compile and run the LogisticMap.java program

Be careful! Run it with: java LogisticMap 0

Now try it with: java LogisticMap 0.80350

What is the output of this program?

How does this program create graphical output?

Any questions about computer graphics?