From charlesreid1

Chapter 3: Parameters and Objects

Sections:

3.1 Parameters

3.2 Methods returning values

3.3 Using objects

3.4 Projectile trajectory

Section 3.1: Parameters

Definitions

Definitions:

  • Parameter/parametric/parameterize
  • Formal parameter
  • Actual parameter
  • Method signature
  • method overloading
  • Parameters = arguments (language)

Material

Parameterization:

  • Form of abstraction
  • What we are doing is generalizing a task
  • Not just solving x2 + 4x + 2 = 0, solving ax2 + bx + c = 0
  • Example code for parameterizing print statements
  • How do you use/declare parameters in a class method?
  • How not to declare parameters - not writeSpaces(int numLines)
  • Differentiating between TYPE DECLARATION/DEFINITION, and the ACTUAL METHOD CALL
  • Parameters: data --> method
  • Each method is defined to have its own scope, as denoted by {}. So how to pass data through the scope?

Scope:

  • Scope means, all variables from outside the function are unknown
  • Data is passed in as fresh
  • Parameters are variables that are predefined in that fresh variable space
  • Scope ties in with return values - once you do a calculation, how do you get the result out?
  • Return values/return variables.
  • Overloading methods: so that we can handle variations in the input data.

Section 3.2: Methods that Return Values

Definitions

Definitions:

  • Return
  • Object
  • class

Material

Example of when you need to return a value: square root function

  • Method syntax: how to declare a function's return type (void --> int/double/etc)

Real life example: Java Math (static class, static methods)

  • Math constants E and PI
  • Casting and math functions
  • How to explore the Java API
  • Just focus on Math, don't get overwhelmed

Math example:

  • Carl Gauss, sum of first 100 integers
    • Excellent example of how different ways of tackling problems (different algorithms) can lead to vast speedups
    • Formula:

To program this formula:

public static int sum(int n) {
    return (n * (n+1)) / 2;
}

How return statements work: value is returned where function call happens, so we need to assign the result of the function call to a variable.

Math example: Pythagorean theorem

  • find the hypotenuse, given sides a and b
public static double hypotenuse( double a, double b ) {
    double c = Math.sqrt( Math.pow(a, 2) + Math.pow(b, 2) );
    return c;
}

How to debug, if this isn't working? Split calculation into more parts: csquared, c, etc.

Section 3.3: Using Objects

Definitions

Definitions:

  • Object
  • Class
  • Index
  • Immutable object
  • Exception
  • Console input
  • Constructor
  • Token
  • Whitespace
  • Package
  • Declaration

Material

Primitive types and objects:

Outline:

  • String objects (and return values and mutability)
  • Interactive programs and user input (scanner0
  • Sample interactive program

This is where we focus on Strings

  • Tokenization, strings, etc - that gets at arrays, which is Chapter 7.

String tokenization section for ciphers.

Metaphor: Why experimentation is necessary in programming.

  • We talked about James Gosling.
  • Does your brain work like his?
  • Does your brain work like Java compiler?
  • Nope! So we can't just think our way through problems
  • Need to be improving our mental compiler, our mental model of what the code will do
  • Iterative refinement

In-Class Worksheet

In-class worksheet: Caesar Cipher

  • How Caesar cipher works
  • Specifications: give them a main method, and call the cipher. They define the cipher method.

Handout for Caesar Cipher:

  • Give them a hint on how to do the char shift
  • Encourage playing around/experimentation

Section 3.4: Projectile Trajectory

Material

Case study with complex example, covering:

  • Input parameters
  • methods that return values
  • mathematical calculations
  • scanner for user input

Basic projectile problem: given an initial velocity and angle, answer questions;

  • Highest point, and the time for that point
  • Time to ground
  • Distance (horizontal distance) traveled
  • Basically: x/y path lengths, and times
  • Print usage/intro
  • Print pretty table

Program:

  • User input
    • Get vel at t=0
    • Get angle at t=0
    • Get steps to display
  • Prep calc
  • For t in Nsteps
    • Increment t
    • Increment x
    • Increment y


Chapter 3 Summary

Assessment Material

Parameters:

  • Notation
  • Purpose
  • How they work

Return values:

  • Notation
  • Purpose
  • How they work

Objects

  • LOTS of definitions nad new concepts
  • Use of String objects to understand these concepts

Projectile trajectory:

  • Focusing on a complex application/program

Chapter 3 Code

Lecture Code

Quadratic equation solver

  • Takes arguments a, b, c
  • No return (two primitive values requires object)
  • Will cover later

Sum of integers

  • Sum of first n integers, ties in with Carl Freidrich Gauss, algorithmic thinking, pattern-finding

Pythagorean theorem

  • hypotenuse
  • let's recap what we have so far: we've solved a projectile gravity equation, solved a quadratic equation, summed up the first N integers, solved the Pythagorean theorem
  • no limit to the kinds of mathematics we can implement
  • very deep and profound connection between programming and mathematics

Scope

  • Illustrate a few simple scope examples

Worksheet Code

Caesar cipher

  • Char shift is the key here, getting them to think about how to implement modular arithmetic AND how to use that integer offset to shift chars
  • Get them to link chars with integers
  • Hint: try executing this Java code: "'a' + 5"

Chapter 3 Homework

HW Summary

(Recommended) Self-check problems: #3, #7, #9, #13, #14, #20, #26

(Required) Exercises: #1, #6, #16, #19

(Required) Projects: (none)

HW Details

Self-check:

  • 3 - spotting mistakes
  • 7, 9 - mystery program
  • 13 - math operations
  • 14 - mystery return
  • 20 - string method calls
  • 26 - get a phrase from user and repeat phrase

Exercises:

  • 1 - print formatting digits
  • 6 - function find larger of abs of 2 numbers
  • 16 - triangle area using Heron's formula
  • 19 - reverse string

Chapter 3 Goodies

Puzzle 3

Puzzles/Fall 2016/Puzzle 3: Et three, brute? (Caesar cipher cryptograms with no spaces (frequency or brute force)

Profiles

Carl Freidrich Gauss

  • Adding up the sum of all integers from 1-100
  • Add up 100 numbers: pair off, and get 101 each time. 100 and 1, 99 and 2, etc. Then 100*101/2 = 10100/2 = 5050
  • First approach: don't manage the complexity, just dive in and have at it
  • Gauss: found an alternative pattern
  • By adding last and first, then second last and second first, and so on, got same number for each
  • Utilizing this turned a sum problem into a multiplication problem
  • Lesson: by changing your pattern of thinking/problem solving, can open up new, better, faster approaches

Julius Caesar

  • History lesson: why important
  • Sometimes, connecting seemingly unconnected topics can lead to sudden insights
  • Need for security, military applications of cryptography
  • Back to the definition of programming: "turning a task that requires repeated computation into something that a machine can execute"
  • Computation is as old as civilization itself

Flags