CS 101 Chapter 1: Background Spring 2005 Aaron Bloomfield

advertisement
CS 101
Chapter 1: Background
Spring 2005
Aaron Bloomfield
1
Let’s begin
Goal

Teach you how to program effectively
Skills and information to be acquired




Mental model of computer and network
behavior
Problem solving
Object-oriented design
Java
2
What is a computer?
Not a rhetorical question!
“A device that computes…
especially a programmable electronic
machine
that
performs
high-speed
mathematical or logical operations or that
assembles,
stores,
correlates,
or
otherwise processes information”

From American Heritage® Dictionary of the
English Language, 4th Edition
3
So what is a computation?
The act or process of computing

Duh!
Definition of computing:


To determine by mathematics, especially by
numerical methods: computed the tax due
To determine by the use of a computer
My revised definition for computing:

The act of taking a problem with specific
inputs and determining a specific answer
(output)
4
Axiom
By definition, a (properly functioning)
computer will always produce the same
output given the same input
5
The first computers
Scales – computed relative weight of two items

Computed if the first item’s weight was less than,
equal to, or greater than the second item’s weight
Abacus – performed mathematical computations


Primarily thought of as Chinese, but also Japanese,
Mayan, Russian, and Roman versions
Can do square roots and cube roots
6
Computer Size
ENIAC then…
ENIAC today…
7
Computer Organization
Memory
Output Devices
Input Devices
Central
Processing Unit
8
Computer Organization
Computer advertisement specification










Intel® Pentium 4 Processor
at 3.06GHz with 512K cache
512MB DDR SDRAM
200GB ATA-100 Hard Drive
(7200 RPM, 9.0 ms seek time)
17” LCD Monitor
64MB NVIDIA GeForce4
MX Graphics Card®
16x Max DVD-ROM Drive
48x/24x/48x CD-RW Drive
56K PCI Telephony Modem
Windows XP Home Edition®
10/100 Fast Ethernet Network Card
512 million bytes of
3.06
Reads
memory
17”
Computer
Can
Can
Microprocessor
Stores
billion
on
send
read
DVDs
the
200
that
or
and
operations
operating
diagonal.
16
billion
receive
can
write
times
for
be
faster
displaying
transferred
data
bytes
CDs.
up
Resolution
system
to
per
than
at
Can
of
56
two
second
data.
images
athousand
using
hold
at
basic
rates
up
double
You
650
to
awith
DVD
–
10
drive.
million
want
64
graphical
or
the
1,280
bits
million
100
high
normal
Can
per
bytes
by
million
hold
interface
RPM
second
bytes
1,024
of
rate
up
and
data
bytes
ofto
memory.
8 Reads
billion
low
per
seek
pixels
at
bytes
More
second
48
time.
memory
times
of data
supports
0.009
faster
A byteseconds
and
more
is 8writes
bits
colors
is
and
24 times
higher
average
faster
resolution
than
A bit
a basic
is a 0drive
or a 1
9
A bit of humor: Computer
Organization
Why I like adding humorous bits
into class…
10
Home network
11
Backbones
12
A bit of humor…
13
Network communication
Communication protocol

Set of rules that govern how data is sent and received
TCP/IP

Exchanging packets of information over the Internet
FTP

Exchanging files between computes
SMTP

Exchanging email over the Internet
POP

Exchanging email between mail reader and the ISP
HTTP

Exchanging files over the WWW
SSL

How information is to be encrypted
14
First Programming Languages
Ada Lovelace (1833)

Programmed of Babbage’s analytical engine
ENIAC (1945)

Programmed by plugging wires
Binary

100100010101001010101010
Assembly
FORTRAN (1954)
15
Java’s Direct Lineage
C (1972) by Dennis Ritchie
C++ (1985) by Bjarne Stroustrup
Java (1991) by James Gosling and others
at Sun Microsystems
16
Computing units of measure
A bit is either a 1 or a 0

On or off, true or false, etc.
A byte is 8 bits:



01001010
As there are 8 bits per byte, each byte can hold
28=256 values
01001010 = 74
All computing measurements are in terms of
bytes
18
Computing units of measure
Kilo (K) = 1,000 (thousand)
Mega (M) = 1,000,000 (million)
Giga (G) = 1,000,000,000 (billion)
Tera (T) = 1,000,000,000,000 (trillion)
Kilo = 210 = 1,024
= Kibi (Ki)
Mega = (1024)2 = 1,048,576
= Mebi (Mi)
Giga = (1024)3 = 1,073,741,824
= Gibi (Gi)
Tera = (1024)4 = 1,099,511,627,776
= Tebi (Ti)
19
Computing units of measure
An unformatted text document (such as a Java program)

3 pages per kilobyte (1,000 bytes)
A formatted document (such as a Word file)

About 5k per page with formatting
A digital camera picture

About 1 Mb each (1,000,000 bytes)
An MP3 music file

5 Mb for a 5 minute song
A music file on a CD

50 Mb for a 5 minute song

10 times the size of an MP3!
A movie clip

About 10 Mb per minute of (TV-sized) video
20
A marketing trick
This hard drive has
250,059,350,016
bytes


= 250.06 Gigabytes
= 232.89 Gibibytes
Guess which one
they
use
to
advertise the drive?
21
Software
Program

Sequence of instruction that tells a computer what to do
Execution

Performing the instruction sequence
Programming language

Language for writing instructions to a computer
Major flavors



Machine language or object code
Assembly language
High-level
Detailed
Program
knowledge
to which of
computer
the
machine
respond
is not
Java
Symbolic
is acan
high-level
language
For
program
to be
directly.
required.
Each machine
Uses
instruction
a
for
programming
coding
executed
itcode
must
be
is
a
vocabulary
binary
and
that
language
language
instructions
translated
structure
corresponds
closer to
to athe
22
problem
native being
instruction
solved
Translation
Translator

Accepts a program written in a source language and
translates it to a program in a target language
Compiler

Standard name for a translator whose source
language is a high-level language
Interpreter

A translator that both translates and executes a
source program
23
Java translation
Two-step process
First step

Translation from Java to bytecode
Bytecodes are architecturally neutral object code
Bytecodes are stored in a file with extension .class
Second step

An interpreter translates the bytecodes into machine
instructions and executes them
Interpreter is known a Java Virtual Machine or JVM
24
Programming Task
Display the forecast
I think there is a world market for maybe five
computers.
Thomas Watson, IBM, 1943.
25
DisplayForecast.java
// Authors: J. P. Cohoon and J. W. Davidson
// Purpose: display a quotation in a console window
public class DisplayForecast {
// method main(): application entry point
public static void main(String[] args) {
System.out.print("I think there is a world market for");
System.out.println(" maybe five computers.");
System.out.println("
Thomas Watson, IBM, 1943.");
}
}
Anmethod
Three
A
application
statements
is a named
program
make
piece
is
uprequired
the
of code
action
to
that
have
of performs
method
a
main()action
some
public
static void
or implements
method named
a behavior
main().
Method main() is part of class DisplayForecast
26
Sample output
27
Java Documentation
Familiarize yourself
documentation

with
the
Java
It will save you lots of time!
A link to it is on the website

We will go over it in a future lab as well
28
Good Commenting
Necessary so others can re-use your code

And so the graders can understand it!
A well commented program:
// Authors: J. P. Cohoon and J. W. Davidson
// Purpose: display a quotation in a console window
public class DisplayForecast {
// method main(): application entry point
public static void main(String[] args) {
System.out.print("I think there is a world market for");
System.out.println(" maybe five computers.");
System.out.println("
Thomas Watson, IBM, 1943.");
}
}
29
Bad commenting
//
//
//
//
Thomas J. Watson (February 17, 1874 - June 19, 1956) is
considered to be the founder of IBM. He was one of the
richest men of his time and called the world's greatest
salesman when he died.
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
While at NCR, he was convicted for illegal anticompetitive sales practices (e.g. he used to have
people sell deliberately faulty cash registers, either
second-hand NCR or from competitors; soon after the
second-hand NCR or competitors cash register failed,
an NCR salesperson would arrive to sell them a brand
new NCR cash register). He was sentenced, along with
John H. Patterson (the owner of NCR), to one year of
imprisonment. Their conviction was unpopular with the
public, due to the efforts of Patterson and Watson to
help those affected by the 1913 Dayton, Ohio floods,
but efforts to have them pardoned by President Woodrow
Wilson were unsuccessful. However, the Court of
Appeals overturned the conviction on appeal in 1915,
on the grounds that important defense evidence should
have been admitted.
//
//
//
//
//
//
//
//
//
//
//
Watson was born in Campbell, New York. His formal
education consisted of only a course in the Elmira
School of Commerce. His first job was at age 18 as
a bookkeeper in Clarence Risley's Market in Painted
Post, New York. Later he sold sewing machines and
musical instruments before joining the National Cash
Register Company as a salesman in Buffalo. He eventually
worked his way up to general sales manager. Bent on
inspiring the dispirited NCR sales force, Watson
introduced the motto, "THINK," which later became
a widely known symbol of IBM.
//
//
//
//
//
//
//
//
//
//
//
Although he is well known for his alleged 1943 statement: public class DisplayForecast {
"I think there is a world market for maybe five computers"
there is no evidence he ever made it. The author Kevin
// method main(): application entry point
Maney tried to find the origin of the quote. He has been
public static void main(String[] args) {
unable to locate any speeches or documents of Watson's
System.out.print("I think there is a world market for
that contain this, nor is it present in any contemporary
System.out.println(" maybe five computers.");
articles about IBM. The earliest known citation is from
System.out.println("
Thomas Watson, IBM, 1943.");
1986 on Usenet in the signature of a poster from Convex
}
Computer Corporation as "I think there is a world market }
for about five computers" --Remark attributed to Thomas
30
J. Watson (Chairman of the Board of International
More bad commenting
From the context-switching code of Unix V6 (file: slp.c)
/*
* If the new process paused because it was
* swapped out, set the stack level to the last call
* to savu(u_ssav). This means that the return
* which is executed immediately after the call to aretu
* actually returns from the last routine which did
* the savu.
*
* You are not expected to understand this.
*/
if(rp->p_flag&SSWAP) {
rp->p_flag =& ~SSWAP;
aretu(u.u_ssav);
}
Source: http://www.tuhs.org/Archive/PDP-11/Trees/V6/usr/sys/ken/slp.c
31
Java and the Internet
Your machine
Your friend's machine
I think ...
DisplayForecast.java
Java
Compiler
JVM
DisplayForecast.class
DisplayForecast.class
Modem
Modem
Internet
32
Engineering software
Complexity of software grows as attempts are
made to make it easier to use

Rise of wizards
33
Software engineering
Goal

Production of software that is effective and reliable,
understandable, cost effective, adaptable, and
reusable
Makesto
Work
Because
Cost
Design
correctly
sense
software
develop
of thedue
and
long
so
and
to
not
that
lifetime
the
maintain
fail
new
great
many
features
costs
should
people
involved
and
notwill
capabilities
exceed
be
to have
involved
expected
flexible
can be
benefit
added
components
 Creation that can be used in other software



Debugging
Maintenance
Enhancement
Two-thirds of the cost is typically beyond creation
34
Principles of software
engineering
Abstraction
Encapsulation
Construct
Determine
Ranking
Separateor
acomponents
the
system
ordering
relevant
from
of
properties
into external
objects
and features
and
components
and
internal
while
ignoring
aspects
packages
nonessential details
Modularity
Hierarchy
35
A bit of humor:
1989 Computer
Advertisement
Guess the price!
36
Object-oriented design
Purpose

Promote thinking about software in a way that
models the way we think and interact with the
physical word
Including specialization
Object


Properties or attributes
Behaviors
37
Programming
Class

Term for a type of software object
Object

An instance of a class
with specific properties
and attributes
38
Programming
Problem solving through the use of a
computer system
Maxim

You cannot make a computer do something if
you do not know how to do it yourself
39
Problem Solving
Why do you care?

We are all assigned tasks to do
At work
At home
At school
Why not do them


Right
Efficiently
40
Problem Solving
Why care about
computer-based
problem
solving (i.e.,
programming)?





Neat
Frontier of science
Profitable
Necessary
Quality of life
41
Problem Solving
Accept
Solutions
Remember

Oftenprocess
The
goal
require
is not
is iterative
aboth
cleverconcrete
solution but
anda correct
abstract
thinking
solution
In solving the problem increased understanding might
require
restarting
Teamwork
42
Problem Solving Process
What is it?




Analysis
Design
Implementation
Testing
Describe the components and associated processes for solving
the problem
Determine
the inputs,
and other
components
the
Develop
solutions
for outputs,
the components
and
use those of
components
Test the components
individually
and collectively
problem
to produce
Straightforward
an overalland
solution
flexible
Description
should
beflexible
sufficiently specific to allow you to solve
Method – process
Straightforward
and
the problem
Object – component and associated methods
43
Problem Solving Process
Determine
problem features
Describe objects
and methods
Produce the
classes and code
Examine for
correctness
Analysis
Rethink as
appropriate
Design
Implementation
Testing
44
Tips
Find out as much as you can
Reuse what has been done before
Expect future reuse
Break complex problems into subproblems
45
Tips
Find out as much as you can
Reuse what has been done before
Expect future reuse
Break complex problems into subproblems
Consider
Research
can is
require
and generate questions
Find out what
knownsignificant
about thetime
problem
Sketching
apresenter
solution
andbecause
then repeatedly
its components
The
worthwhile
the resultrefine
is a better
Talk effort
to theis
until
the entire process is specified
understanding
Determine what attempts have succeeded and what attempts
True
of the problem makes it easier to solve
have understanding
failed
46
Tips
Find out as much as you can
Reuse what has been done before
Expect future reuse
Break complex problems into subproblems
Your
Be
open
timetoisindirect
valuable
use of existing materials
Correctness is probably even more valuable
Use existing infrastructure that is known to work
47
Tips
Find out as much as you can
Reuse what has been done before
Expect future reuse
Break complex problems into subproblems
Make as few assumptions as necessary
Maximizes the likelihood that your effort can be used in future
situations
48
Tips
Find out as much as you can
Reuse what has been done before
Expect future reuse
Break complex problems into subproblems
Divide-and-conquer
Solve subproblems and combine into an overall solution
49
Tips
Read

Problem solving texts
George Polya, How to Solve It; A New
Aspect of Mathematical Method,
Princeton Press, 1988
Wayne Wickelgren, How to Solve Mathematical Problems,
Dover Publications, 1995
Paul Zeitz, The Art and Craft of Problem Solving, John Wiley,
1999

Sociological examination of different problem solving
styles
Sherry Turkle and Seymour Papert, Epistemological
Pluralism: Styles and Voices Within the Computer Culture,
Signs: A Journal of Women in Culture and Society, 1990
50
Motivational posters…
51
Download