Computer Science 100

Computational Expression

Gregory M. Kapfhammer


flickr photo by wocintechchat.com shared under a CC (BY) license

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?

Equality and Relational

==

!=

<

<=

>

>=

Logical Operators

!

&&

||

Any questions about these operators?

Design Exercise

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

Let's look at some examples!

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?

Now, let's code our design! 🎉

Retrieve new code in our inclass repository

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

Try Two More Programming Examples

Input these programs from the book

Two Examples

View the source code of two new programs

You should see Wages and CoinFlip

How do these programs actually work?

Can you compile and run these programs?

What output do these programs produce?

Any questions about conditional logic?

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

View the source code of a new program

Build and run the ComputeAverage.java program

What output does this program produce?

How does this program work?

Add comments to the source code

Add the required output statement

Find the conditional logic

Find the iteration construction

Any questions about average computation?

Infinite While Loops

See the example on page 219

Any questions about iteration constructs?

Iterating through a container

Iterators and Loops

Process a collection of items in a loop

Use method calls to "iterate" through objects

See 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

Store the objects in an ArrayList "container"

Any questions about ArrayLists?

Refer to the textbook for more details

Make sure to complete the inclass programming!