AP Computer Science

Download Report

Transcript AP Computer Science

AP Computer Science - AB
Ms. Knudtzon
September 7, 2005
Some of my Background
•
•
•
•
•
BS in Computer Science, Harvey Mudd College
Software Engineer, The Aerospace Corporation
MS in Computer Science, University of Maryland
Taught a Data Structures class at Cal-State Los
Angeles
Last year, student average of 4.5 on APCS-A
Administrative
• Syllabus for the course (Handout)
 Note: Final Project and Exam
• Survey - please fill out
• Course Web Site (Will be ready sometime this weekend)
• Books:
 Java Software Solutions (Lewis & Crofton)
 For Scheme: How to Design Programs (Felleisen, Findler, Flatt &
Krishnamurthi) - online version
 Other books (I will be using)
• Head First Java
• Objects First with Java: A Practical Introduction Using BlueJ
What I expect of you
• Keep your notes organized (take notes in class)
• Do any assigned reading
• Do all the assignments
 Including journals and abstracts
• Have fun; do hard work
• Let me know if the assignments are taking more
•
•
•
than one hour a night
Come to me with questions, concerns, and ideas
Follow the lab rules
No cheating
 But what’s cheating in CS?
What you can expect of me
• I will do my best to make class interesting
• I will create interesting projects for the course
 Please provide me feedback throughout the course
• I will be a master learner
 Teachers should be a model for learning in the
classroom environment and facilitate the learning of
others
• I will be available for help
 After school or by appointment
 I will answer questions via email (Allow at least 8
hours for a response)
Working on St. Albans computers
• Storing your files (in your student folders on the
•
server)
Organizing assignments and labs
 Keep a folder for each homework or project
• Software you will be using
 Dr. Scheme
 BlueJ (will start with this program)
 Eclipse (mostly 2nd quarter)
• Policies for homework submission
 Submit assignments via email
HW: Journal Assignment
• Please write 1/2 -- 1 page journal about your
goals for this semester in this class. Please
include/consider:




Your background
Your interests
Your worries
What you are most excited about
• This will be due Friday before class (by email)
 You will meet with me to discuss these goals early
next week
Introduction to Programming
Say you want to instruct someone to mark the
corners of a rectangle (5x10 foot) fence or field
 How would you explain exactly what to do?
 How else can we explain how to do that?
• What if we wanted to:
 Make pb&j sandwiches for the entire school?
 Average all the grades in a class?
Why is this important
• For a person to accomplish any task, they have to carry
out a set of explicit steps
 Computers are no different. You have to tell them step-by-step
how to do what you want them to do
• Computer Science, particularly programming, builds off of
this simple model of providing short programs (series of
steps) to complete tasks
 As the complexity of the task increases, so does the complexity
of the program required to complete it
 However, even the most complex programs can be broken into a
series of simpler programs
Programming as Science
• Programming is not easy and requires creativity
 Can be hard for people to grasp at first exposure
 Takes repetition and perseverance
• Requires a unique way of thinking
 Requires strong capability to think abstractly
 You must be able to think both about how to
• Break problems into small pieces, AND
• Build a large program from smaller pieces
• But… being able to think like this will serve you well in
almost any future career field
APCS-AB
Lecture 2
Thursday, September 8
Learning Programming
• 1st quarter we will introduce the Scheme and
•
Java programming languages and some basic
(and not so basic) programming techniques
2nd quarter we will revisit some topics and build
on them to make more substantial programs
• So, don’t worry if things aren’t completely clear
at first – it will all make sense more as we go
along. By second semester, you won’t remember
why you thought this first stuff was difficult!
Definition of Computer
Science
• CS involves the synthesis and analysis of:





Algorithms
Information representations
Information communication processes
Automated resource allocation methods
Languages for all of the above
Robert Keller
Harvey Mudd College
Computer Science in general
• What is computer science?




Is it programming?
Is that it?
What’s the role of hardware? Software?
What’s computer science research?
• What are the social implications of computing?
 What about ethics?
Interdisciplinary Nature of CS
“Ought computer science be a branch of
mathematics? Of engineering? Of
architecture? Of art? Of biology? Of
sociology? Of philosophy? Alternatively,
does it borrow techniques and approaches
from all of these disciplines? I think so”
Bjarne Stroustrup, The Design and Evolution of C++, 1994
Interdisciplinary Nature of CS
•
•
•
•
•
•
•
•
Mathematics – discrete analysis, logic
Engineering – system design
Art – program construction
Psychology – interface construction
Philosophy – language design
Biology – genetic programming paradigms, bioinformatics
Architecture – large (and expensive) systems
Sociology – information sharing and security
Scheme
We learn to compute at a young age. At first we just add and subtract numbers.
One plus one equals two.
Five minus two is three.
As we grow older we learn about additional mathematical operations, like exponentiation
and sine, but we also learn to describe rules of computation.
Given a circle of radius r, its circumference is r times two times pi. A minimum-wage laborer who works for
N hours earns N times 5.35 dollars.
The truth is, our teachers turn us into computers and program us to execute simple
computer programs.
So, the secret is out. Computer programs are just very fast students. They can perform
millions of additions while we might still be struggling with the first one. But computer
programs can do more than just manipulate numbers. They can guide an airplane.
They can play games. They can look up a person's phone number. They can print the
payroll checks for huge corporations. In short, computers process all kinds of
information.
Chapter 1 Intro, HTDP
Scheme
• Online Book is at: www.htdp.com
• Preface (Handout)
 Why are we learning Scheme?
 What the book claims
 What I hope
Simple Scheme Programs
Computation
Scheme Program
3
3
value of pi
pi
4+5
(+ 4 5)
4+6*2
(+ 4 (* 6 2))
5*4-10
6*4 + 12*-2
Cost of 3 slices of pizza
(out of 8 slices) if a
whole pie costs $12
Mathematical Operations
• Scheme has a whole range of advanced
mathematical operators as well:
(sqrt A)
(expt A B)
(remainder A B)
(log A)
(sin A)
Variables and Programs
• We computed the cost of 3 slices of pizza (out of
•
8 slices) if a whole pie costs $12
(* 3 (/ 12 8))
What if we want the price of the pizza to be
variable?
 We need a function.
To write a function
; share-cost: number --> number
The contract
; Consumes price of a pizza and returns cost of 3 slices
(define (share-cost price)
(* 3(/ price 8)))
The purpose
Header and body
The actual code,
notice the various
sets of parenthesis
To use this function: (share-cost 12)
Another Function Example
;Contract: add-tip: number --> number
;Purpose: Consumes an amount and
; return amt plus 15%
(define (add-tip amt)
(+ amt (* .15 amt)))
Whole Class Exercise
• Write a function that calculates the area of
a disk
• Use that function to write a function that
calculates the area of a ring
More Simple Programs
Computation
Scheme Program
Is x zero?
(= x 0)
Is x positive?
(>= x 0)
Is x larger than 3 and
smaller than 6
(and (> x 3) (< x 6)
Test Cases
• Write test cases first (before writing the
body of the function)
• More likely to catch errors in code if you
understand what is supposed to happen
• Helps you know whether you understand
the problem or not
in-interval?
• Write a function that checks if a number is strictly between the other
two numbers
;Contract: num num num --> boolean
;Purpose: consumes 3 nums and determines
;whether first is strictly between the other two
(define (in-interval? x lo hi)
(…)
;Test Cases
(in-interval? 3 4 5) “should be” false
(in-interval? 4 3 5) “should be” true
; make sure you test boundaries
(in-interval? 4 4 4) “should be” false
Errors in Scheme
• Syntax errors - Not all parenthesized expressions are
Scheme expressions
 (10) is not a legal Scheme expression
 What errors messages do we get for the following expressions?
• (+ (10) 20)
• (10 + 20)
• (+ +)
(define (f 1) (+ x 10))
(define (g x) + x 10)
(define h(x) (+ x 10))
• Run-time errors - things like (/ 1 0)
• Logical errors - unfortunately, Scheme can’t help you
with these! You need to catch mistakes by designing
programs carefully and systematically.
Scheme Intro Cont’d
Ms. Knudtzon
September 9
Program Design
• Understand the program’s purpose
 Give the program a meaningful name and describe what it consumes
and produces (Contract)
 Write the header - it restates the program’s name and gives each input a
distinct name
 Write a Purpose Statement that describes what the program is to
compute (usually one or two lines)
• Program examples - to gain a better understanding of what the
program should compute
 Writing the examples first helps discover logical errors, because then
you don’t just trust the program - instead you think about what the
program is supposed to do
Program Design
• Testing - make sure the program computes
the expected results for your examples and
for all “boundary” conditions
• Finally, formulate the body - complete the
expression by understanding and
expressing how the inputs relate to what
you want to output
• This is our Design Recipe (1st pass)
 See Chapter 2, Figure 4
Specifications
• In many cases in the homework/exercises, it will say write
a function called “blah” - sometimes it doesn’t matter if
you name it exactly “blah”, but sometimes it will --> so if
something is specified, follow the specs
 Sometimes I will create automatic grading scripts that rely on
things following the given naming convention
 This is the same in the “real-world” - if a team writes interacting
code, then individual coders need to follow the spec exactly to
make everything work properly
Image & Fabric Teachpacks
• Image.ss (part of htdp)
 Help-desk: Composing Images
 Shapes --> example: > (circle 5 'solid 'red)
 Manipulation:
• overlay: image image --> image
• Lines up pinholes of images (usually center of image)
• Example > (overlay (rectangle 22 22 'solid 'blue) (circle 5 'solid 'red)
)
 And much more
• Fabric teachpack
 Functions for manipulating “fabric” images
 HW handout explains available functions
Shortcut
• If you want to save yourself some typing,
you can define constants in your program
• (define r-circ (circle 5 'solid 'red))
• (define red-tshirt (add-print tshirt (createsolid-fabric 'red tshirt) )
Lab
Create an AP folder on your H drive and create a Scheme
folder in it to save your files in
Finish Exercises from Scheme Handout (alone or with a
partner)
• Follow the Design Recipe:




Contract, Purpose & Header
Examples
Body
Test Cases
Homework
• Install Dr. Scheme at home
 Choose Version 209 (stable)
• Download fabric teachpack from my website
• Do fabric-exercises (as many as you can, if you have
problems with 4-6, that’s okay)
 Follow the Design Recipe
(Teachpack and exercises courtesy of TeachScheme workshop)