Computer Science 112:

Introduction to Computer Science II

Gregory M. Kapfhammer


flickr photo by by physician1977 shared under a CC Public Domain Dedication (CC0)

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: for, while, do-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

Iteration and recursion are often viable alternatives

What is an intuitive definition of recursion?

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.

Solution One: Using int variables does not work!

Solution Two: Using Integer variables does not work!

Can you develop a new data type that will work?

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?

Review the details of the CreditCard class

CreditCard

Instance variables

Constructors

Accessor methods

Utility method

Main method

How would you test this Java class?

Any questions about the CreditCard example?

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?

See Section 1.6 for more details

Now, let's highlight software development tasks