Computer Science 111:

Introduction to Computer Science I

Gregory M. Kapfhammer


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

Color Scheme

Key Concept

Corresponding Diagram

In-Class Discussion

In-Class Activity

Details in the Textbook

Boolean Expressions

Ways to express conditions in Java programs!

Can be incorporated into if, while, and for constructs

Logical operators and truth tables

Any questions about these basic building blocks?

Programming Project

Read in two values from the user

Compare the two values with an if statement

If the values are the same, print this out

Otherwise, print out that they are different

If Statements

Allows us to implement conditional logic

See if(hours>STANDARD) on page 218

See if(myCoin.isHeads()) on page 219

Wait! We can also have an else component!

Any questions about the use of this construct?

Comparing Data

Floats, characters, and objects

Different methods for different data

Boolean and int are easy!

Comparing a floating point is hard! Why?

Remember, characters are just numbers!

For objects, we look at the address in memory

Two Examples

Run the git pull command!

View the source code of the two new programs.

You should see ifElseDemo and ifElseDemoCap

How do these programs actually work?

Can you compile and run these programs?

What output do these programs produce?

While Statements

Loop that evaluates a boolean condition

Check condition, execute body, check condition

Allows a program to repeatedly perform an action!

Sometimes, the condition tests a "sentinel value"

You can modify while behavior with break and continue

Using While Loops

Run the git pull command in the share repository

Compile and run the Average.java program

What output does this program produce?

How does this program work?

Write a summary sentence to describe this program and its output!

Infinite While Loops

Compile and run the Hooray.java program

What output does this program produce?

How can you stop this program?

Compile and run the WhileLoopExample.java program

This program has a bug in it. Can you fix it?

Iterators and Loops

Process a collection of items in a loop

Use method calls to "iterate" through objects

Let's try it with the URLDissector.java program!

How does this program work?

Write a summary sentence to explain this program!

ArrayLists

Often, we need to manage sets of objects!

What data structure will help us to do this?

The ArrayList is a great choice!

We can store objects of a specified type

Let's try this with the Shuffler program

What output does this program produce?

Write a summary sentence to explain this program!