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:

$ \sum_{i=1}^{n} i = \dfrac{ n(n+1) }{2} $

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

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

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"

Homework Code

(homework problems?)

Chapter 3 Goodies

Puzzle 3

Crypto resources: http://www.math.brown.edu/%7Ejhs/MathCrypto/MCOnlineExercises.html

Block Caesar cipher puzzles

  • Now you know what type of puzzle they are
  • Now you can brute-force them (by hand)
  • Think about how to do this programmatically
MVBY ZJVY LHUK ZLCL UFLH YZHN VVBY MVYL MHAO LYZZ LAMV 
YAOV UAOP ZSHU KHUL DUHA PVUJ VUJL PCLK PUSP ILYA FHUK 
KLKP JHAL KAVA OLWY VWVZ PAPV UAOH AHSS TLUH YLJY LHAL 
KLXB HS

Profiles

Carl Freidrich Gauss

  • Adding up the sum of all integers from 1-100
  • 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


Flags