Abstraction Marie desJardins

advertisement
Abstraction
IS 101Y/CMSC 101
Computational Thinking and Design
Tuesday, September 17, 2013
Marie desJardins
University of Maryland, Baltimore County
Getting Help
 CSEE Help Center
 ITE 201E – hours TBA but usually M-F 10-5, plus evening
hours on some days
 http://www.csee.umbc.edu/resources/computer-sciencehelp-center/
 Tutors may not know Processing, but all know Java
(which is similar) and are very helpful!
 IS Java Lab (?)
Abstraction
 Abstraction is the process of generalizing away from
the details of a problem to simplify and focus on its key
aspects
 We use abstraction for problem solving, design,
organization, and simplification
 Examples from everyday life:





Smart thermostats
Steering wheels
Email folders and labels
Schedules and calendars
Language!!
Abstraction in Computing
 Abstraction is ubiquitous in all computing disciplines
 “It’s All Just Bits”
 The interpretation (meaning) of data depends on how it is used
 The same bit sequence can represent an integer, a decimal number, a
sequence of characters, or anything else you might decide it means
 Layers of computing hardware, including gates, chips, and components
 Programming languages are abstractions of machine language
 Functional decomposition == layers of abstraction
 Models and simulations: abstractions of real-world phenomena
 We can study, analyze, and experiment with models more easily than
experimenting on the real world
Programming Enables Problem
Solving, Expression, and
Knowledge Creation
 Programs are written to execute algorithms




Requires an understanding of how instructions are processed
Programs are executed to automate processes
A single program can be run multiple times, on many machines
Executable programs increase the scale of solvable problems
 Programming is facilitated by appropriate abstractions




Functions are re-usable programming abstractions
Parameterization can be used to generalize a specific solution
Data abstraction can separate behavior from implementation
APIs and libraries simplify complex programming tasks
 Programs are developed and used by people
 Developing programs is an iterative process
 Finding and eliminating errors is part of the process
 Documentation is needed for maintainable programs
Big Idea:
Design
 Problem solving often produces multiple possible solutions
 Or multiple ways to implement the solution
 Design is the process of making those decisions and
choices
 Design at a high level
 Is it cost-effective to automate kidney exchange?
 Should we use a relational or object-oriented database?
 Design at a low level
 How do I structure this function?
 What do I call this variable?
Design Process
for Programs
 First, understand the problem clearly
 Second, write the solution in English
 Test its correctness by manually applying it to some simple –
and then more complex -- examples
 Optionally, “translate” the solution into pseudocode
 Advanced programmers will use this step as an abstraction to
avoid the syntactic details of a particular programming
language
 Next, translate the solution into a programming language
 Finally, implement (type) and test (carefully!) your solution
Example: Counting
 Print the numbers from one to N
 N is a variable that can change each time the program is
called [an abstraction!!]
 For example, “printNumbers (7)” should print:
1
2
3
4
5
6
7
Counting in English:
Attempt #1
 First attempt at English:
Count from one to N
Print out each number
 Not really an algorithm – just a restatement (what is the
primitive action “count”??)
Counting in English:
Attempt #2
 Next attempt:
Set the variable “current” to 1
Print the value of “current”
Add one to “current”
If “current” is greater than N, stop
Otherwise, go back to “print” step
 Turns out that “go to” statements are bad design (for
reasons we’ll talk about later)
 Let’s try it again, using something that looks more like a
loop with a condition
Counting in English:
Attempt #3 (almost right!)
Set the variable “current” to 1
While “current” is less than N:
Print the value of “current”
Add one to “current”
 Test by hand: what if N = 3?
 Boundary cases: what if N = 0? N = -4?
 Possible error cases: what if N = 8.73?
what if N = “this isn’t a number”?
Counting in English:
Processing Version
void printNumber (int N) {
int current = 1;
while (current <= N) {
println (current);
current = current + 1;
}
}
Exercise #1: Multiplication
 Work in your team – OK to split into smaller groups of 2 or 3
when you’re at the implementation step
 How would you multiply two numbers, using only the
addition operator?
 Understand the problem – state some examples
 Write the solution in English
 Test the English solution!
 Write the solution in Processing
 Test the program!
 Concepts: iterations, efficiency
Exercise #2: Guessing
 With your team, write Processing code to guess a
number between 1 and 100
 Remember the algorithmic design process:
 Understand the problem – work through some
examples by hand!
 Write a solution in English and test it – then get a TF
or instructor to OK your solution before moving on!
 Translate your solution into Processing
 Implement, test, and show a TF or instructor!
 Be prepared to explain why your approach is a good
way to solve the problem
Download