|
|
| (46 intermediate revisions by the same user not shown) |
| Line 1: |
Line 1: |
| | See [[CSC 142 Checklist]] |
| | |
| =Chapter 1: Intro to Java= | | =Chapter 1: Intro to Java= |
|
| |
|
| Sections:
| | [[CSC 142/Chapter 1]] |
| | |
| 1.1 Basic computing concepts
| |
| | |
| 1.2 And now, Java
| |
| | |
| 1.3 Program errors
| |
| | |
| 1.4 Procedural decomposition
| |
| | |
| 1.5 Case study: DrawFigs
| |
| | |
| Note: the first chapter is definitions-heavy.
| |
| | |
| ==Section 1.1: Intro to Java==
| |
| | |
| ===Definitions===
| |
| | |
| * Algorithm
| |
| * Program
| |
| * Hardware ("Hardware: the part of the computer that you can kick")
| |
| * Software
| |
| * Digital
| |
| * Binary
| |
| * Program execution
| |
| * Compiler
| |
| * Java Virtual Machine (JVM)
| |
| * Java Runtime Environment (JRE)
| |
| | |
| ===Material===
| |
| | |
| Things to cover:
| |
| * Java class libraries (standard library)
| |
| * Java programming environment
| |
| * Java Hello World program
| |
| * Console window/command line
| |
| | |
| 1995: Oracle: "Java: A simple, object-oriented, network-savvy, interpreted, robust, secure, architecture-netural, portable, high-performance, multithreaded, dynamic language."
| |
| | |
| ===Goodies===
| |
| | |
| Crypto puzzle challenge
| |
| | |
| ===Life Skills===
| |
| | |
| Life skills track:
| |
| * Pay attention and FOCUS
| |
| * RTFM
| |
| * Follow directions and read carefully
| |
| * Whether it's your compiler, or your instructor, or your fellow students - pay attention to what's being said
| |
| * Skill will pay off when you start to compile your Java programs
| |
| | |
| ==Section 1.2: Java==
| |
| | |
| ===Materials===
| |
| | |
| Class
| |
| * Every program, all Java code, lives in a class
| |
| * Class header
| |
| * Class methods
| |
| * Statements
| |
| * String literals
| |
| * System.out.println
| |
| * Escape sequences
| |
| * print vs println
| |
| | |
| Complex example: draw figures
| |
| * ASCII diamond, X, rocket
| |
| | |
| Code comments, white space, readability:
| |
| * Comments
| |
| | |
| ===Profiles===
| |
| | |
| * James Gosling - Oracle, tech companies, open source vs. enterprise, hackers vs suits
| |
| | |
| ==Section 1.3: Syntax Errors==
| |
| | |
| ===Material===
| |
| | |
| Different types of errors:
| |
| * Where the errors happen - the normal process - (code) --> (compiler) --> (bytecode)
| |
| * Errors at compiler level - syntax errors - book lists common syntax errors
| |
| * Errors at code level - bugs - errors in the logic of the program (wrong idea, or right idea but implemented wrong)
| |
| | |
| ===Pofiles===
| |
| | |
| * Grace Hopper
| |
| | |
| ==Section 1.4: Procedural Decomposition==
| |
| | |
| ===Materials===
| |
| | |
| Decomposing complexity:
| |
| * Decomposition concept: split into functions, tasks, subtasks
| |
| | |
| C is very verb-oriented, action-oriented
| |
| | |
| Java is very noun-oriented, object-oriented
| |
| | |
| Procedural programming:
| |
| * Function-based, action-based programming
| |
| * How to decompose task of baking a cake
| |
| * Static methods (help serve function of.......... functions)
| |
| | |
| Object oriented programming version:
| |
| * c = new Cake(); c.make()
| |
| * Encapsulating complexity of the object
| |
| * Example in book: drawBox, drawTopX, drawBottX
| |
| | |
| Flow control
| |
| * How control changes with function calls
| |
| * Objects: with OOP it becomes more complicated to follow the flow of the program
| |
| * Procedural programs and interpreted languages: you just start at the top and go from there
| |
| * Objects: "when is this code actually used?" (have to dive in to see)
| |
| * Learning to follow program flow control
| |
| * Flow control allows you to abstract away detail
| |
| * Example runtime error
| |
| | |
| ===Definitions===
| |
| | |
| * Decomposition
| |
| * Iterative enhancement
| |
| * Static method
| |
| * Method call
| |
| * Flow control
| |
| | |
| ===Life Skills===
| |
| | |
| Life skills track:
| |
| * Cover non-word representations of programs
| |
| | |
| (Give them a crypto puzzle, but we haven't introduced any crypto or codes just yet)
| |
| | |
| ==Section 1.5 : Case Study with DrawFig==
| |
| | |
| ===Material===
| |
| | |
| Modularization of drawing program
| |
| * Breaking into pieces - not just the box, or the x, or the rocket
| |
| * but breaking down into common components, common to all parts of the program
| |
| * Take hello world program...
| |
| * Modularize it, make it reusable...
| |
| | |
| ==Chapter 1 Summary==
| |
| | |
| Worksheet: definitions from book on one side (quiz material)
| |
| | |
| Source code for a procedural program (fizz baz foo bar buzz bam) on the other
| |
| | |
| There are N bugs, find the N bugs (self-work)
| |
| | |
| Discuss the program flow with a partner (group work)
| |
| | |
| How do we represent this program in a clear and concise way?
| |
| | |
| Transform foo bar into get ready for school - proper names can help clarify understanding
| |
| | |
| Lecture: broken up into 1.5 parts
| |
| * Broad brush-stroke over chapter 1
| |
| * Memorize definitions
| |
| * Know XYZ for quiz
| |
| * Spend a majority of time on in-class exercises
| |
| * FOCUS: KNOW HOW TO RUN/SET UP HELLO WORLD
| |
| * FOCUS: KNOW DEFINITIONS
| |
| * FOCUS: KNOW FUNCTIONS AND PROGRAM FLOW
| |
| * FOCUS: KNOW ERRORS/EXCEPTIONS
| |
| | |
| Quiz/exam/assessment material:
| |
| * Hello world, basics, public static void main, syntax
| |
| * Definitions matchup
| |
| * Functions questions
| |
| * Program flow questions
| |
| * Exceptions: spot the bug... spot logical errors... spot syntax errors...
| |
| | |
| Need to make beginnings of class difficult, to send the message that they can't let it slide
| |
| | |
| ===Deliverables===
| |
| | |
| Intro to Java
| |
| * Know how to set up and run hello world
| |
| * How does it work, role of compliler vs editor
| |
| * JRE vs JVM vs JDK
| |
| * Public static void main
| |
| * EEverything is a class
| |
| * Filename = class name
| |
| * Correct syntax, protected keywords
| |
| | |
| Definitions
| |
| * algorithm, program, hardware/software, digital, binary, program execution, compiler, JVM, JDK, JRE, class header, class methods, statements, string literals, system.out.println, escape chars, print vs println, exception, decomposition, flow control, iterative enhancement, static method
| |
| | |
| Functions and program flow
| |
| * How to follow the flow of a program through multiple (nested) function calls
| |
| * When to use a static method or static class
| |
| * How to follow a nested program
| |
| * How to break up a task into less complex parts, with reusability
| |
| * Translate between procedure and function
| |
| | |
| Errors and logical problems
| |
| * Spotting the error
| |
| * Red herring: error, plus (unclear) compiler output
| |
| | |
| ==Code==
| |
| | |
| ===Lecture Code===
| |
| | |
| Exceptions code
| |
| * Tie in with the Java API, let them know it exists and where it is, we'll talk more about it and how to use it in the future
| |
| | |
| ASCII rocket code
| |
| * From Reges and Stepp (no patterns/functions til next section)
| |
| | |
| Real rocket code - Vertical code (solve a basic equation)
| |
| * Good to keep math at the forefront. Just throwing it out there.
| |
| | |
| ===Worksheet Code===
| |
| | |
| Density code, DensityConversion code
| |
| * Description: write a program that prints a physical property of your choice for ten different chemical compounds (in metric units). Write another program that prints the physical property of your choice (in English units).
| |
| * Example: Look up densitiy, (g/cm3). Program to print 10 densities, in g/cm3. Then, program to convert to lb/ft3, and print 10 densities, in lb/ft3.
| |
| * Specifications: Your program must print a banner or header that states the physical property. Your program must print the (correct) units of the physical property.
| |
| * Submitting: Deliverables, what to turn in, what NOT to turn in, what format, how, when, with what
| |
| | |
| ===Homework Code===
| |
| | |
| (What homework problems?)
| |
|
| |
|
| =Chapter 2: Primitive Data and Definite Loops= | | =Chapter 2: Primitive Data and Definite Loops= |
|
| |
|
| Sections:
| | [[CSC 142/Chapter 2]] |
| | |
| 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==
| |
| | |
| ===Definitions===
| |
| | |
| Definitions:
| |
| * Data type
| |
| * Expression
| |
| * Evaluation
| |
| * Operator
| |
| * Precedence
| |
| * Casting
| |
| | |
| ===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)( ......... )
| |
| | |
| ===Profiles===
| |
| | |
| Charles Babbage - digital representation
| |
| | |
| ==Section 2.2: Variables==
| |
| | |
| ===Definitions===
| |
| | |
| Definitions:
| |
| * Variable
| |
| * Declaration
| |
| * String concatenation
| |
| * Increment/decrement
| |
| | |
| ===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: <code>2 + 3 + " hello " + 7 + 2*3</code>
| |
| * 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==
| |
| | |
| ===Definitions===
| |
| | |
| Definitions:
| |
| * Control structure
| |
| | |
| ===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==
| |
| | |
| ===Definitions===
| |
| | |
| Definitions:
| |
| * Scope
| |
| * Localizing variables
| |
| * Infinite loop
| |
| * Pseudocode
| |
| * Class constant
| |
| | |
| ===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
| |
| | |
| ===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
| |
| | |
| ==Section 2.5: Case Study: Hourglass Figure==
| |
| | |
| ===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
| |
| | |
| ==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 3: Parameters and Objects= | | =Chapter 3: Parameters and Objects= |
|
| |
|
| Sections:
| | [[CSC 142/Chapter 3]] |
| | |
| 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)
| |
| | |
| 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 <code>{}</code>. 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:
| |
| | |
| <math>
| |
| \sum_{i=1}^{n} i = \dfrac{ n(n+1) }{2}
| |
| </math>
| |
| | |
| To program this formula:
| |
| | |
| <pre>
| |
| public static int sum(int n) {
| |
| return (n * (n+1)) / 2;
| |
| }
| |
| </pre>
| |
| | |
| 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
| |
| | |
| <pre>
| |
| public static double hypotenuse( double a, double b ) {
| |
| double c = Math.sqrt( Math.pow(a, 2) + Math.pow(b, 2) );
| |
| return c;
| |
| }
| |
| </pre>
| |
| | |
| 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 4: Conditional Execution= | | =Chapter 4: Conditional Execution= |
|
| |
|
| Sections:
| | [[CSC 142/Chapter 4]] |
|
| |
|
| 4.1 If/else statements
| | =Chapter 5: Program Logic and Indefinite Loops= |
|
| |
|
| 4.2 Cmulative algorithms
| | [[CSC 142/Chapter 5]] |
|
| |
|
| 4.3 Text processing
| | =Chapter 6: File Processing= |
|
| |
|
| 4.4 Methods with conditional execution
| | [[CSC 142/Chapter 6]] |
|
| |
|
| 4.5 Case study: BMI
| | =Chapter 7: Arrays= |
|
| |
|
| Last chapter: solving complex programming problems with repeated tasks using for loops, flexibility via class constants, and user input.
| | [[CSC 142/Chapter 7]] |
|
| |
|
| This chapter: conditional execution. Expand our understanding of common programming situations. With new topics, revisit old material.
| | =Chapter 8: Classes= |
|
| |
|
| ==Section 4.1: If Else==
| | [[CSC 142/Chapter 8]] |
|
| |
|
| Conditional logic
| | =Outline= |
| * Sometimes, but not always, we want to execute particular lines of code.
| |
| * Can use if/else structure to branch execution
| |
| * Relational operators: different kind of operations
| |
| ** Up until now, have been doing operations on numbers to obtain new numbers
| |
| ** Example, 12*2, assign to new variable int a = 12*2
| |
| ** No true/false nature, it is always true because we're creating a definition, we're defining a new rule
| |
| * Now, we'll be using equals in a different sense
| |
|
| |
|
| "Does a equal 24?" vs. "a is equal to 24"
| | ==Midterm A== |
|
| |
|
| <code>if( a == 24 )</code> vs. <code>int a = 24;</code>
| | Midterm A Outline |
| | * Chapter 1: Intro to Java |
| | ** Java basics |
| | ** Program errors |
| | ** Procedural decomposition |
| | ** Case Study: DrawFigs |
| | * Chapter 2: Primitive Data and Definite Loops |
| | ** Basic data |
| | ** Variables |
| | ** For loop |
| | ** Managing complexity |
| | ** Case Study: Hourglass figures |
| | * Chapter 3: Parameters and Objects |
| | ** Parameters |
| | ** Methods returning values |
| | ** Using objects |
| | ** Case Study: Projectile trajectory |
| | * Chapter 4: Conditional Execution |
| | ** If/else statements |
| | ** Logic statements |
| | ** Object equality and operators |
| | ** Refactoring to use if/else logic |
| | ** Cumulative algorithms |
| | ** Equality with floats and roundoff error |
| | ** Text processing |
| | ** Printf |
| | ** Methods and conditional execution |
| | ** Case Study: BMI |
|
| |
|
| Can introduce other comparison operators:
| | ==Midterm B== |
| * Equals
| |
| * Not equals
| |
| * Less than/greater than
| |
| * Less than or equal to/greater than or equal to
| |
|
| |
|
| Revisit operator precedence:
| | Midterm B Outline |
| * Unary operators | | * Chapter 5: Program logic, conditional execution |
| * Multiplication operators | | ** While loops |
| * Additive operators | | ** Random numbers |
| * Relational operators | | ** Fencepost algorithms |
| * Equality operators | | ** Boolean type |
| * Assignment operators | | ** User input and dealing with errors |
| | ** Assertion and program logic |
| | ** Case Study: NumberGuess |
| | * Chapter 6: File processing |
| | ** File reading basics |
| | ** Token based processing |
| | ** Reading numbers from a file |
| | ** Paths and directories |
| | ** Line based processing |
| | ** Line/token combinations |
| | ** Advanced file processing |
| | ** Removing whitespace |
| | ** Case Study: Zip code lookup |
| | * Chapter 7: Arrays |
| | ** Array basics and initialization |
| | ** Comparing arrays |
| | ** Array traversal |
| | ** Reference semantics and memory, and null |
| | ** Advanced array techniques |
| | ** Shuffling, shifting, reversing |
| | ** Multidimensional arrays |
| | ** Rectangular multidimensional arrays |
| | ** Jagged multidimensional arrays |
| | ** Case Study: Benford's Law |
| | * Chapter 8: Object oriented programming |
| | ** Object state and behavior |
| | ** Accessor methods |
| | ** Object initialization and constructors |
| | ** Data encapsulation |
| | ** Private fields |
| | ** Mutable/immutable |
| | ** Case Study: Stock class |
|
| |
|
| Nested if/else:
| | =Flags= |
| * Patterns for designing logic structures
| |
| * If red/if blue/if green: can combine into structure, because only one can be true
| |
| * if/else
| |
|
| |
|
| Nested, only one is true:
| | {{CSC142Flag}} |
|
| |
|
| <pre>
| | The short list of topics: |
| if( condition1 ) {
| |
| ...
| |
| } else if( condition2 ) {
| |
| ...
| |
| } else if( condition3 ) {
| |
| ...
| |
| }
| |
| </pre>
| |
|
| |
|
| Multiple conditions can be true:
| | Intro to Java |
| | |
| <pre>
| |
| if( condition1 ) {
| |
| ...
| |
| }
| |
| if( condition2 ) {
| |
| ...
| |
| }
| |
| if( condition3 ) {
| |
| ...
| |
| }
| |
| </pre>
| |
| | |
| Corresponding diagram representations: (see book)
| |
| | |
| Finding the pattern:
| |
| * Analyzing the problem helps determine what kind of control structure you need
| |
| * Combination of branches doesn't matter: if statements
| |
| * Only one, or none, of branches should be taken: if/else if/else if
| |
| * Only one, and exactly one, branch should be taken: if/else if/else if/else
| |
| | |
| Weird equality stuff:
| |
| * For strings and other objects, == doesn't work great
| |
| * This is shorthand for a GENERAL concept, which is, EQUALS
| |
| | |
| Metaphor:
| |
| * Two numbers: 5 and 5. I know how to check if they're equal. Equality is defined rigorously, based on the way the real numbers are defined.
| |
| * Two lamps: A and B. Are these two lamps equal?
| |
| ** Equal in light? Equal in how much power they draw?
| |
| ** Equal in appearance? Independent of function? (Wax lamp)
| |
| ** Equal in the cosmic sense? Composed of the exact same atoms? What about a perfect atom-by-atom reproduction?
| |
| * Equality is something we have to define
| |
| | |
| Objects and equality:
| |
| * Always use obj1.equals(obj2), not == (equals() is the more general)
| |
| | |
| Refactoring/modularizing if/else code:
| |
| * if A: do a bunch of stuff; if B: do a bunch of stuff; if C: do a bunch of stuff
| |
| * rewrite, define an action X, so then it becomes def X do a bunch of stuff; if A do X; if B do X; if C do X
| |
| | |
| Multiple conditions for if/else:
| |
| * AND operator (if a number is between A and B, that can be expressed as the combination of 2 conditions: number > A, number < B)
| |
| * OR operator (A, or B, or both)
| |
| | |
| ==Section 4.2: Cumulative Algorithms==
| |
| | |
| ===Definitions===
| |
| | |
| Definitions:
| |
| * Cumulative algorithm
| |
| * Roundoff error
| |
| | |
| ===Material===
| |
| | |
| Connect loops with algorithms:
| |
| * Finding average, finding sum, finding min/max
| |
| | |
| Min and max:
| |
| * Exploring a "simple" idea like taking the maximum can be tricky to implement
| |
| * Let's say we're looking at surface temperature of a planet
| |
| * All negative numbers; if you initialize your minimum to 0, you'll get a minimum of 0 - incorrect
| |
| * To set a maximum: should start with data
| |
| | |
| Example: hailstone sequence
| |
| * pick a starting number
| |
| * If odd, e.g., do 3x+1, and if even, e.g., do (x/2)
| |
| * Start with an initial minimum/maximum guess
| |
| * Go through sequence, N steps, calculate a minimum and maximum
| |
| | |
| Example: cumulative sum with if
| |
| * if statement to check for valid input values
| |
| * if statement to count number of negative numbers
| |
| * user input (N steps), user input (numbers)
| |
| | |
| Roundoff error:
| |
| * Checking equality is tricky due to roundoff error
| |
| * Comes up around cumulative algorithms
| |
| * Example: 2.77500000000000005
| |
| * Float roundoff errors
| |
| * Truncate a number at a certain decimal place
| |
| * Replicating digits lopped off
| |
| * Why, if 2.1 and 3.8, no repeating decimals, would Java turn them into repeating decimals?
| |
| * Due to internal Java representation: representing decimal numbers as powers of 2, not powers of 10
| |
| * Simple example: add 0.1 (can't be represented exactly because base 2)
| |
| | |
| Checking equality:
| |
| * Use tolerance
| |
| | |
| <pre>
| |
| abs(A-B) < 0.01
| |
| </pre>
| |
|
| |
|
| ==Section 4.3: Text Processing==
| | Primitive Data and Definite Loops |
|
| |
|
| ===Definitions===
| | Parameters and Objects |
|
| |
|
| Definitions:
| | Conditional Execution |
| * Text processing
| |
| * ASCII
| |
|
| |
|
| ===Material===
| | Program Logic and Conditional Execution |
|
| |
|
| Char type:
| | File Processing |
| * comes from C language
| |
| * strings composed of chars
| |
| * charAt() method
| |
|
| |
|
| Chars and ints: primitive types
| | Arrays |
| * have already seen (with Caesar cipher) that we can treat chars like numbers, operate on them, increment them
| |
| | |
| Cumulative text algorithm
| |
| * Count # characters occurring in string
| |
| * Cumulative concatenation
| |
| | |
| Printf:
| |
| * More control over print format
| |
| * <code>%d</code> digit, f for float, etc.
| |
| * Table of common print formats
| |
| | |
| <pre>
| |
| %d
| |
| %8d
| |
| %-6d
| |
| | |
| %f
| |
| %.2f
| |
| %16.2f
| |
| | |
| %s
| |
| %8s
| |
| %-9s
| |
| </pre>
| |
| | |
| ===Handout: Caesar Cipher===
| |
| | |
| Cracking the Caesar cipher: letter frequencies, and also brute-forcing
| |
| | |
| ==Section 4.4: Conditional Execution with Methods==
| |
| | |
| ===Definitions===
| |
| | |
| Definitions:
| |
| * Precondition
| |
| * Postcondition
| |
| | |
| Basically, this section addresses: how do we ''ensure'' certain conditions are met before/after we run a method?
| |
| | |
| Exceptions:
| |
| * Occur at runtime, not compile time
| |
| * Previously, saw exceptions with scanner (nextInt(), not an int)
| |
| | |
| Assessment questions:
| |
| | |
| When would you see an exception?
| |
| * Run time
| |
| * Compile time
| |
| | |
| When would you see a logic error?
| |
| * Run time
| |
| * Compile time
| |
| | |
| When would you see a syntax error?
| |
| * Run time
| |
| * Compile time
| |
| | |
| Functions (analogy):
| |
| * Factorial function: can compute 0!, 1!, 2!, etc
| |
| * Cannot ocmpute negative factorial, so return undefined
| |
| * Mathematical approach: exceptions are definitions, rules that they can define
| |
| | |
| Documentation:
| |
| * Tell the user how to use it
| |
| | |
| Creating exceptions:
| |
| * exceptions are objects
| |
| * Pieces of grouped data/instructions
| |
| * Need to create new exceptions before we can throw them
| |
| * When we throw an exception, it stops the code
| |
| * We also want to add comments! and a useful exception message!
| |
| | |
| Similar concept (language): assertions
| |
| * C and C++, Python, assert(True)
| |
| | |
| Revisiting return values: example
| |
| * New application of conditional logic to comparison operators:
| |
| * Example of overloading (int and float)
| |
| * If statement for ints, etc.
| |
| * more templating: again, comes down to understanding what kind of control structure you need
| |
| | |
| <pre>
| |
| def method() {
| |
| if( x > y) {
| |
| return x
| |
| }
| |
| return y
| |
| }
| |
| </pre>
| |
| | |
| Revisiting return values: another example
| |
| * String: find particular index of particular character
| |
| * built-in String method, indeOf
| |
| * r = s.indexOf('r')
| |
| * make our own static method: myIndexOf
| |
| * called like, r = myIndexOf('r',s)
| |
| * How to locate a character in a string?
| |
| ** Can loop through each char of string (i in str length)
| |
| ** Then, can use charAt to see if this char is our char
| |
| * Note on why not static method:
| |
| ** static method doesn't remember our place
| |
| ** if we wanted to say, "find the ''next'' r," we'd have to pick up where we left off
| |
| ** that means, saving a piece of data, which means, we can't use static method
| |
| * Implement return method
| |
| ** return our char index
| |
| ** else, what to do if char not found?
| |
| ** convention: return -1 (or, 999,999)
| |
| * Scope:
| |
| ** allows us to say, when we get to return, we can drop everything and leave, everything will be cleaned up
| |
| | |
| String index and bounds:
| |
| * index of string of N characters: starts at 0, runs to N-1
| |
| * If we access a string at index N, Java will raise an exception
| |
| * Exception won't happen until runtime - that's when Java will know string length, and will know string is not long enough
| |
| | |
| Following logical branches:
| |
| * Example of program with nested if/else if/else if
| |
| * if you have a return type defined in the method header (e.g., string), that means you have to have a return statement, no matter what, for all cases (or, throw an exception)
| |
| * Can eliminate some code if we assume we know the SAT score is in the range 600-2400
| |
| * Assuming is BAD!!!
| |
| | |
| <pre>
| |
| if(totalSAT<600 || totalSAT > 2400 ) {
| |
| raise IllegalArgumentException
| |
| } else {
| |
| ...
| |
| }
| |
| </pre>
| |
| | |
| ==Section 4.5: BMI Study==
| |
| | |
| BMI:
| |
| * Calculation based on input values (weight, height)
| |
| * Iterative enhancement: one thing at a time
| |
| ** Compute results for one person (no structure)
| |
| ** Write program for 2 people (no structure)
| |
| ** Finally, assemble well-structured program
| |
| | |
| One person unstructured:
| |
| * height and wieght, read in
| |
| * Calulate BMI from that
| |
| * One line at a time, get x, print x, get y, print y, calc z, print z
| |
| * use printf
| |
| * use conditional to classify weight status
| |
| * again, one line after another
| |
| | |
| Two person unstructured:
| |
| * to handle a second person, eneed to prompt for both people's data, then do calcs, then print results all at onc
| |
| * New structure:
| |
| ** chunk of code for intro
| |
| ** chunk of code for person 1
| |
| ** chunk of code for person 2
| |
| ** print person 1
| |
| ** print person 2
| |
| | |
| Nowmove on to a more structured solution:
| |
| * giveIntro
| |
| * bm1 = getbmi()
| |
| * bm2 =getbmi()
| |
| * reportresults(bm1,bm2)
| |
| | |
| Heuristics:
| |
| 1. Each method should have a coherent set of responsibilities
| |
| 2. No one method should do too large a share of the overall task
| |
| 3. Coupling and dependencies between methods should be minimized
| |
| 4. The main method should be a concise summary of the overall program
| |
| 5. Data should be owned at the lowest possible level (abstract away detail and complexity)
| |
| | |
| ===Quotes===
| |
| | |
| Murphy's Law:
| |
| * If the user can do something wrong, they will do something wrong
| |
| | |
| Hanscombe's Law:
| |
| * Never attribute to malice what is equally attributable to stupidity
| |
| | |
| =Flags=
| |
|
| |
|
| [[Category:Teaching]]
| | Classes |