From charlesreid1

Line 209: Line 209:
* Suppose you invest X in Y, at rate R
* Suppose you invest X in Y, at rate R
* Watch how quickly your money grows after Z years, printing row by row
* Watch how quickly your money grows after Z years, printing row by row
===Homework Code===
(Homework problems?)


==Chapter 2 Homework==
==Chapter 2 Homework==

Revision as of 19:20, 14 September 2016

Chapter 2: Primitive Data and Definite Loops

Sections:

2.1 Basic data concepts

2.2 Variables

2.3 The for loop

2.4 Managing complexity

2.5 Case study: Hourglass figure

Note: this chapter has two halves. The first half examines expressions, particularly for numerical data and variables. The second half examines control structures, used to perform repetitive actions.

The goal here is pattern-finding - what repeated actions will lead to the desired outcomes? This loop will cover definite loops (loops that repeat a predetermined number of times). Next chapter will cover indefinite loops.

Section 2.1: Basic data concepts

2.1 Definitions

Definitions:

  • Data type
  • Expression
  • Evaluation
  • Operator
  • Precedence
  • Casting

2.1 Material

Java primitive types:

  • int
  • double
  • char
  • boolean

Why important? because computers represent data and numbers in memory, and we need to understand how (rules)

How we input data and operations:

  • literals (literal values, 2.19)
  • expressions (assembling stuff, related by operators)

Operators:

  • Mix of computer science and math
  • Operators mean, performing a set of tasks (or, a task)
  • Some operators require 2 things, e.g., 2+3
  • Some operators require 1 thing, e.g., inverse(A) or d/dx( x^2 )

Literals

  • Different data types
  • Decimals vs integers (especially with operations)
  • Booleans: true/false (keywords)

Arithmetic

  • Division weirdness
  • Remainder operator
  • Goldfish analogy - small memory capacity - 2 digits - what happens after 99? start over

Precedence and order of operations

  • Grammar connection
  • Be explicit in what you're asking computer to do

Casting and file types

  • can deal with int/float differences by casting
  • Again, be explicit: (int)( ......... )

Section 2.2: Variables

2.2 Definitions

Definitions:

  • Variable
  • Declaration
  • String concatenation
  • Increment/decrement

2.2 Materials

You have to declare it available in order to use it

You have to declare what kind of variable it is

Can combine declaration and assignment

Can declare multiple variables on one line

Concat:

  • To combine strings, use plus operator
  • But be careful with number/string types
  • Example: 2 + 3 + " hello " + 7 + 2*3
  • Multiplication first
  • Then only addition is left, so evaluate left to right
  • 2+3 first becomes 5
  • Then the remaining plus operators turn into string concatenation operators
  • Better: be explicit about what types are being added to what, and in what order.

Increment/decrement:

  • Useful shorthand operators

Section 2.3: For Loop

2.3 Definitions

Definitions:

  • Control structure

2.3 Material

Purpose:

  • Replace redundant tasks

Syntax of for loops

  • Initialize a loop variable, create a condition
  • Body of for loop is executed if condition is true
  • Tracing loops
  • Curly braces are important

Some patterns:

  • If we want to execute a loop N times, can use two patterns: i=0, or i=1
  • Nested for loops
  • Print vs println with nested for loops

Section 2.4: Managing Complexity

2.4 Definitions

Definitions:

  • Scope
  • Localizing variables
  • Infinite loop
  • Pseudocode
  • Class constant

2.4 Material

Scope:

  • Helps to manage complexity of variable space
  • Curly braces represent scope
  • Variable defined inside braces is not defined outside those braces

Examine examples, errors, why it crashes, and how it crashes

Tracing a for loop:

  • Initialization
  • Test
  • Body
  • Update
  • End

Pseudocode:

  • Part of communicating about code
  • Simple examples to give them an idea
  • More complex pattern-finding, indices (2(i-1)), etc...
  • Constants (for drawing patterns, e.g., number of lines)
  • Public static final type name = (...) <-- permanently, same value, accessible by static methods
  • We are still thinking PROCEDURALLY

Section 2.5: Case Study: Hourglass Figure

2.5 Material

Detailed example of how to work out a pattern and translate it into modular code.

Single-line pattern, indexing and offset shifts

Entire code, refactoring into methods and modularization

Overall: tackling a complex problem by breaking it down into simpler parts, taking small steps toward end goal, managing complexity

Chapter 2 Summary

Deliverables:

  • Primitive type expressions and literals
  • Casting
  • Assigning/changing variable values
  • For loops, for loop patterns
  • Scope
  • Pseudocode
  • Constants

Assessment Material

Dealing with primitive types in expressions, and order of operations, etc.

  • How to interpret incrementing and other assignment operators
  • For loops:
    • Know how to follow control
    • Syntax
    • Curly braces
  • Scope
  • Pseudo code to describe how to draw a pattern
  • Constants
    • Syntax
    • Explaining the purpose (e.g., which of the following would be a good variable to program as public static final z)
  • 99 bottles of root beer

Chapter 2 Code

Lecture Code

Quadratic equation

  • Given a, b, c
  • Assume (or implement in driver) check that b2-4ac > 0

Worksheet Code

Worksheet: watch your money grow

  • Suppose you invest X in Y, at rate R
  • Watch how quickly your money grows after Z years, printing row by row

Chapter 2 Homework

HW Summary

(Recommended) Self-check problems: #2, #4, #8, #10, #14, #22, #25, #28, #34

(Required) Exercises: #3, #9, #12, #22

(Required) Projects: (none)

HW Detailed

Self-check:

  • 2, 4 - order of operations
  • 8, 10 - getting 1st, 2nd, 3rd digit of a number using Java
  • 14 - variables on both LHS and RHS
  • 22 - Tracing for loop
  • 25 - Blastoff!
  • 28 - three nested loops to print pattern
  • 34- turn figure into table of char counts for ! and / in figure

Exercises:

  • 3 - Fibonacci numbers
  • 9 - Nested for loops for 40 character lines, 4 different patterns
  • 12 - Nested for loops to create 000111222333...
  • 22 - Java program to produce dollar figure (V of $ on * bkg)

Chapter 2 Goodies

Profiles

Charles Babbage

  • Digital representation
  • Computer

Puzzle 2

Cryptograms (definitions/vocab?)

When a user does X it is called Y.

Between the X and the Y is Z.

For extra credit write the word apollo on your next quiz.

Flags