Computer Science 112:

Introduction to Computer Science II

Gregory M. Kapfhammer


flickr photo shared by Billboard Art Project under a Creative Commons ( BY-NC-ND ) license

Color Scheme

Key Concept

Corresponding Diagram

In-Class Discussion

In-Class Activity

Details in the Textbook

Sit together in groups of three

Important Software

Compiler

Java virtual machine

What are the inputs and outputs of these programs?

Draw a picture showing how these are related!

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

Use typed variables to compute values!

Expressions

Arithmetic operators: +, -, *, /, %

Wait, what does % mean?

Operator precedence

What about parentheses?

Review Sections 1.4.1 and 1.4.2 for more details


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

Type 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?

Narrowing conversions can lead to information loss. Why?

Refer to Section 1.4.3 for details about casting

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

What does this look like in a program?

Java Programs


public class Lincoln
{
   // prints a presidential quotation
   public static void main (String[] args)
   {
      System.out.println ("A quote by Abraham Lincoln:");
      System.out.println ("Whatever you are, be a good one.");
   }
}
                    
          

Can you find? ... The definition of a class

Java Programs


public class Lincoln
{
   // prints a presidential quotation
   public static void main (String[] args)
   {
      System.out.println ("A quote by Abraham Lincoln:");
      System.out.println ("Whatever you are, be a good one.");
   }
}
                    
          

Can you find? ... The main method

Java Programs


public class Lincoln
{
   // prints a presidential quotation
   public static void main (String[] args)
   {
      System.out.println ("A quote by Abraham Lincoln:");
      System.out.println ("Whatever you are, be a good one.");
   }
}
                    
          

Can you find? ... Java keywords

Java Programs


public class Lincoln
{
   // prints a presidential quotation
   public static void main (String[] args)
   {
      System.out.println ("A quote by Abraham Lincoln:");
      System.out.println ("Whatever you are, be a good one.");
   }
}
                    
          

Can you find? ... A method invocation

Java Programs


public class Lincoln
{
   // prints a presidential quotation
   public static void main (String[] args)
   {
      System.out.println ("A quote by Abraham Lincoln:");
      System.out.println ("Whatever you are, be a good one.");
   }
}
                    
          

Can you find? ... A character string

Java Programs


public class Lincoln
{
   // prints a presidential quotation
   public static void main (String[] args)
   {
      System.out.println ("A quote by Abraham Lincoln:");
      System.out.println ("Whatever you are, be a good one.");
   }
}
                    
          

Can you find? ... A single line comment

Java Programs


public class Lincoln
{
   // prints a presidential quotation
   public static void main (String[] args)
   {
      System.out.println ("A quote by Abraham Lincoln:");
      System.out.println ("Whatever you are, be a good one.");
   }
}
                    
          

Can you find? ... A static variable

If Statements

Allows us to implement conditional logic

Consider if(hours>STANDARD)

Consider if(myCoin.isHeads())

Wait! We can also have an else component!

Any questions about the use of this construct?

Review Section 1.5 for additional details

Comparing Data

Floats, characters, and objects

Different methods for different data

Boolean and int are easy!

Remember, characters are just numbers!

Comparing a floating point is hard! Why?

For objects, we look at the address in memory

Iteration Constructs

Perform some action repeatedly

Looping constructs: while, do-while, while

What are the similarities and differences?

When would you decide to use a certain construct?

Loops can tax the limited resources of a computer

Let's Try It!

Go to the #class channel in our Slack team

Save, compile, and run the WhileLoopExample program

This file is also available in our "share" repository

What is the output of this program?

Does this program have a defect in it?

If it does, can you fix the program?

More Iteration!

Go to the #class channel in our Slack team

Save, compile, and run the LogisticMap program

This file is also available in our "share" repository

Be careful! Run it with: java LogisticMap 0

Try it again! Run it with: java LogisticMap 0.825250

What is the output of this program?

How does this program create graphical output?

Iteration Investigation

Try many different inputs: java LogisticMap 0.825250

Where are the split points in this graph?

Where does the function depart into chaos?

How does this program use iteration constructs?

This program combines conditional logic and iteration

Review Section 1.5.2 for more details!

Iteration and recursion are often viable alternatives

What is an intuitive definition of recursion?

Review basic concepts while exploring new frontiers

Greatest Common Divisor

Develop an example of this with your partner

How can you compute the GCD of two numbers?

What is the most efficient way to compute GCD?


flickr photo shared by Defence Images under a Creative Commons ( BY-NC-ND ) license

Iterative GCD

Download this example from the "share" repository

Run this program many times and study the output

How does this program work?

Comment out the debugging statements

Perform a timing experiment to evaluate performance

/usr/bin/time java IterativeGCD 10 2


flickr photo shared by Defence Images under a Creative Commons ( BY-NC-ND ) license

Recursive GCD

Add some debugging statements

Run this program many times and study the output

How does this program work?

Comment out the debugging statements

Perform a timing experiment to evaluate performance

/usr/bin/time java RecursiveGCD 10 2


flickr photo shared by Defence Images under a Creative Commons ( BY-NC-ND ) license

Which of these programs is faster? Why?

Calculation of elapsed time for a method

Factorial

Develop an example of this with your partner

How can you compute the factorial of a number?

What is the most efficient way to compute factorial?


flickr photo shared by National Eye Institute under a Creative Commons ( BY ) license

Iterative Factorial

Run this program several times and study the output

How does this program work?

What is the maximum value that can be computed?

Perform a timing experiment to evaluate performance

/usr/bin/time java IterativeFactorial 10


flickr photo shared by National Eye Institute under a Creative Commons ( BY ) license

Recursive Factorial

Add some debugging statements

Run this program several times and study the output

How does this program work?

Perform a timing experiment to evaluate performance

/usr/bin/time java RecursiveFactorial 10


flickr photo shared by National Eye Institute under a Creative Commons ( BY ) license

Which of these programs is faster? Why?

Compare and contrast results from experiments

Why are these results evident?

Will the results ever be different?

Object-Oriented Programming

A class defines behavior and data

An object is an instance of a class

Methods define behavior and variables store the data

Object-Oriented Programming

Visibility modifiers control access

Variables/methods can either be instance or static

Inheritance supports code reuse

Methods

Methods have a "signature"

Returns values and parameters

Parameter passing in the Java language

Any questions about using methods in Java?

Parameter passing in Java?

Write a Java program that includes a method that can swap two values.

String Manipulation

Real world examples of string processing?

Purpose of and ways to index a string?

See Section 1.3 for more details about Strings


flickr photo shared by James Jordan under a Creative Commons ( BY-NC-ND ) license

Arrays

Arrays allow you to store a fixed number of variables

All values in the array are of the same type

Indexing an array can lead to out of bounds exceptions

Compare and contrast arrays and ArrayLists

Any questions about using arrays in Java?

Packages and Imports

Let's keep our source code organized!

Place related Java classes into a package

Make sure that your directories are correctly organized

Create an example of a package and a directory structure

Any questions about using packages in Java?

Simple input and output

Programming Challenge

Write a Java program called SimpleInput.java that reads an integer value from the user with java.util.Scanner, generates a random integer value with java.util.Random, sums these two integers, and then outputs the result of the addition.

Any questions about the programming challenge?

Review the details of the CreditCard class

Any questions about the CreditCard example?

Review Chapter 1 for more details!