CSC 142/Chapter 2
From charlesreid1
Contents
- 1 Chapter 2: Primitive Data and Definite Loops
- 2 Flags
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
Confused Operator code
- shows same operator in two different contexts with two different behaviors
Density Calculations
- Re-implements density calculations, but with a class constant
For pattern
- Illustrates how to print a simple pattern using nested for loops
Print sequence
- Print numbers from the sequence 2^n
Worksheet Code
Square root
- Use Newton's Method to compute the value of square roots
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
Isaac Newton
- Brilliant mathematician and physicist, discovered the Law of Gravitation of spheres, Newton's Laws of Motion
- Physics: Newton's Three Laws (body in motion stays in motion until acted upon, F=ma, for every action equal opposite reaction)
- Physics: Newton's Law of Gravitation (two planets, multi-body problem)
- Mathematics: invented the calculus, and as if that weren't enough, created Newton's Method for finding roots of equations (still used on modern computers to calculate square roots), binomial expansions, infinite series
- Optics: discovered light was a particle, uncovered nature of color and its connection to wavelengths of photons, white being combination of all colors
- Lucasian Professor of Mathematics at Cambridge
- Retired from Cambridge, worked as the Warden of the Mint
- Mint: London was having a financial crisis because of counterfeit money, Newton applied his talents to making coins and bank notes that could not be counterfeited (hard problem! making something that's hard to make a copy of, on a really really big scale)
- Economics: Newton was the first to devise a comprehensive system for paper money in the British economy, starting in London; this helped to make Britain a financial power, and made London the world's financial center, which it still is to this day
Puzzle 2
Puzzles/Fall 2016/Puzzle 2 - Et tu, brute? (Caesar cipher cryptograms with spaces)
Flags
CSC 142 - Intro to Programming I Computer Science 142 - Intro to Programming I, South Seattle College.
Chapter 1: Intro to Java CSC 142/Chapter 1 Chapter 2: Primitive Data and Definite Loops CSC 142/Chapter 2 Chapter 3: Parameters and Objects CSC 142/Chapter 3 Chapter 4: Conditional Execution CSC 142/Chapter 4 Chapter 5: Program Logic and Indefinite Loops CSC 142/Chapter 5 Chapter 6: File Processing CSC 142/Chapter 6 Chapter 7: Arrays CSC 142/Chapter 7 Chapter 8: Classes CSC 142/Chapter 8
Puzzles: Puzzles
Category:Teaching · Category:CSC 142 · Category:CSC Related: CSC 143 Flags · Template:CSC142Flag · e |