6804068 chapter 01 background
DESCRIPTION
background, chapter 1TRANSCRIPT
-
7/21/2019 6804068 Chapter 01 Background
1/75
Chapter 1
Class 2
CS 101
Chapter 1: Introduction
Aaron Bloomfield
Spring 2007
-
7/21/2019 6804068 Chapter 01 Background
2/75
2
Lets begin
Goal
Teach you how to program effectively
Skills and information to be acquired What the heck all this means
Problem solving
Object-oriented design
Java
-
7/21/2019 6804068 Chapter 01 Background
3/75
3
So, what is a computer, anyway?
-
7/21/2019 6804068 Chapter 01 Background
4/75
4
What is a computer?
Not a rhetorical question!
Adevice that computes
especially a programmable electronic machine thatperforms high-speed mathematical or logicaloperations or that assembles, stores, correlates, or
otherwise processes information From American Heritage Dictionary of the English
Language, 4thEdition
-
7/21/2019 6804068 Chapter 01 Background
5/75
5
So what is a computation?
The act or process of computing
Duh!
Definition of computing:
To determine by the use of a computer
To determine by mathematics, especially by numericalmethods: computed the tax due
My revised definition for computing:
The act of taking a problem with specific inputs anddetermining a specific answer (output)
-
7/21/2019 6804068 Chapter 01 Background
6/75
6
Axiom
By definition, a (properly functioning) computer willalwaysproduce the same output given the sameinput
So how do we compute random numbers?
-
7/21/2019 6804068 Chapter 01 Background
7/75
7
The first computers
Abacusperformed mathematical computations
Primarily thought of as Chinese, but also Japanese, Mayan, Russian,and Roman versions
Can do square roots and cube roots
Scales computed relative weight oftwo items
Computed if the first items weightwas less than, equal to, or greater
than the second itemsweight
-
7/21/2019 6804068 Chapter 01 Background
8/75
8
Stonehenge
-
7/21/2019 6804068 Chapter 01 Background
9/75
9
Computer Size
ENIAC then
ENIAC today
-
7/21/2019 6804068 Chapter 01 Background
10/75
10
Algorithms
-
7/21/2019 6804068 Chapter 01 Background
11/75
11
So what do we do with a computer
now that we have one?
We have to tell a computer what to do!
Computers have no intelligence of their own
We tell a computer what to do by writing a
computer program, oralgorithm In this course, welluse Java
-
7/21/2019 6804068 Chapter 01 Background
12/75
12
Algorithms
What is an algorithm?
A step-by-step problem-solving procedure,
especially an established, recursive computationalprocedure for solving a problem in a finite numberof steps
From American Heritage Dictionary of the EnglishLanguage, 4thEdition
Weveseen lots of algorithms before
-
7/21/2019 6804068 Chapter 01 Background
13/75
13
Example algorithm: map directions
-
7/21/2019 6804068 Chapter 01 Background
14/75
14
Example algorithm: car radio removal
-
7/21/2019 6804068 Chapter 01 Background
15/75
15
Example algorithm: Recipes
-
7/21/2019 6804068 Chapter 01 Background
16/75
16
An optical illusion
-
7/21/2019 6804068 Chapter 01 Background
17/75
17
Algorithm correctness
-
7/21/2019 6804068 Chapter 01 Background
18/75
18
Incorrect algorithms
Not all algorithms aregood
So then what makes an algorithmbad?
Can be wrong
Can be inefficient
Can never stop
Can have other problems as well
-
7/21/2019 6804068 Chapter 01 Background
19/75
19
Incorrect algorithms: just plain wrong
From http://en.for-ua.com/blog/2005/12/09/102028.html
http://en.for-ua.com/blog/2005/12/09/102028.htmlhttp://en.for-ua.com/blog/2005/12/09/102028.htmlhttp://en.for-ua.com/blog/2005/12/09/102028.htmlhttp://en.for-ua.com/blog/2005/12/09/102028.htmlhttp://en.for-ua.com/blog/2005/12/09/102028.htmlhttp://en.for-ua.com/blog/2005/12/09/102028.htmlhttp://en.for-ua.com/blog/2005/12/09/102028.htmlhttp://en.for-ua.com/blog/2005/12/09/102028.htmlhttp://en.for-ua.com/blog/2005/12/09/102028.htmlhttp://en.for-ua.com/blog/2005/12/09/102028.htmlhttp://en.for-ua.com/blog/2005/12/09/102028.htmlhttp://en.for-ua.com/blog/2005/12/09/102028.htmlhttp://en.for-ua.com/blog/2005/12/09/102028.htmlhttp://en.for-ua.com/blog/2005/12/09/102028.htmlhttp://en.for-ua.com/blog/2005/12/09/102028.htmlhttp://en.for-ua.com/blog/2005/12/09/102028.htmlhttp://en.for-ua.com/blog/2005/12/09/102028.htmlhttp://en.for-ua.com/blog/2005/12/09/102028.htmlhttp://en.for-ua.com/blog/2005/12/09/102028.html -
7/21/2019 6804068 Chapter 01 Background
20/75
20
Inefficient algorithms: MapQuest directions
Consider directions to get around grounds
-
7/21/2019 6804068 Chapter 01 Background
21/75
21
Note that this is not an incorrect algorithm!
Just a veryinefficient one
-
7/21/2019 6804068 Chapter 01 Background
22/75
22
Incorrect algorithms: Shampoo directions
-
7/21/2019 6804068 Chapter 01 Background
23/75
23
-
7/21/2019 6804068 Chapter 01 Background
24/75
24
Incorrect algorithms: Shampoo directions
Lather, rinse, repeat
This algorithm repeats forever! It never halts
Note that humans know to not to spend forever performing thealgorithm
But computers do not! Remember, they have zero intelligence
Hence the overplayed (and not very funny) computer joke: How did the computer scientist die in the shower?
S/he read the directions: lather, rinse, repeat
-
7/21/2019 6804068 Chapter 01 Background
25/75
25
Incorrect algorithms: Inexact recipes
-
7/21/2019 6804068 Chapter 01 Background
26/75
26
Incorrect algorithms: summation
Consider this algorithm: Given an integer n
Keep track of an ongoing sum (starts at 0)
Repeat Add nto the ongoing sum
Subtract 1 from n
Until nis zero
Whatswrong with this algorithm? Will it ever stop?
Will it alwaysstop? Are you sure?
-
7/21/2019 6804068 Chapter 01 Background
27/75
27
Our goal
Is to write correct and efficient algorithms for acomputer to follow
Remember that computers are dumb!
We arentgoing to worry about the efficient part inthis course
But what doescorrectmean?
-
7/21/2019 6804068 Chapter 01 Background
28/75
28
Correct algorithms
Consider an algorithm to display the color blue
Is this blue?
Is this blue?
What about this?
And this one?
Definitely
Also, yes: two correct results!
Maybe (could be green)
Definitely not
-
7/21/2019 6804068 Chapter 01 Background
29/75
29
Note that this is not an incorrect algorithm!
Just a veryinefficient one
-
7/21/2019 6804068 Chapter 01 Background
30/75
30
So what does all this mean?
Humans specify algorithms without a lot of precision
Display the colorblue
Get me fromheretothere
When there isntmuch precision, there are often multiple answers
Computers need more precision Display the color 0x0000ff (royal blue):
There is only one possible outcome
Find the shortest route fromheretothere
We need to be very specific when we specify things to acomputer
Computers are dumb!
-
7/21/2019 6804068 Chapter 01 Background
31/75
31
Carved watermelons
-
7/21/2019 6804068 Chapter 01 Background
32/75
32
Programming a computer
-
7/21/2019 6804068 Chapter 01 Background
33/75
33
How do we tell all this to a computer?
Computer: Tea, Earl Gray, hot
Jean-Luc Picard from Star Trek
Unfortunately, that doesntwork so well today
Computers dontunderstand English
-
7/21/2019 6804068 Chapter 01 Background
34/75
34
First Programming Languages
Ada Lovelace (1833)
Programmed of Babbagesanalytical engine
ENIAC (1945)
Programmed by plugging wires
Binary
100100010101001010101010
Assembly FORTRAN (1954)
-
7/21/2019 6804068 Chapter 01 Background
35/75
35
Programming Languages
Common programming languages:
BASIC
COBOL
Pascal C (1972) by Dennis Ritchie
C++ (1985) by Bjarne Stroustrup
Java (1991) by James Gosling and others at Sun
Microsystems
-
7/21/2019 6804068 Chapter 01 Background
36/75
36
Background
-
7/21/2019 6804068 Chapter 01 Background
37/75
37
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
-
7/21/2019 6804068 Chapter 01 Background
38/75
38
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
Mega = (1024)2= 1,048,576
Giga = (1024)3= 1,073,741,824
Tera = (1024)4= 1,099,511,627,776
= Kibi (Ki)
= Mebi (Mi)
= Gibi (Gi)= Tebi (Ti)
-
7/21/2019 6804068 Chapter 01 Background
39/75
39
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
-
7/21/2019 6804068 Chapter 01 Background
40/75
40
A marketing trick
This hard drive has250,059,350,016bytes
= 250.06 Gigabytes = 232.89 Gibibytes
Guess which one
they use when theyare advertising thedrive?
T d d ti t
-
7/21/2019 6804068 Chapter 01 Background
41/75
41
Todays demotivators
-
7/21/2019 6804068 Chapter 01 Background
42/75
Chapter 1
Class 3
Our first Java program
-
7/21/2019 6804068 Chapter 01 Background
43/75
44
Programming Task
DisplayHelloWorld!
-
7/21/2019 6804068 Chapter 01 Background
44/75
45
HelloWorld.java
// Purpose: say hello!
public class HelloWorld {
public static void main(String[] args) {
System.out.println (Hello,world!");
}
}One statement makes up the action of method main()Method main() is part of class HelloWorldA method is a named piece of code that performs some action orimplements a behaviorAn application program is required to have a public static voidmethod named main().
-
7/21/2019 6804068 Chapter 01 Background
45/75
46
Sample output
-
7/21/2019 6804068 Chapter 01 Background
46/75
47
Program parts
-
7/21/2019 6804068 Chapter 01 Background
47/75
48
Common Language Elements
There are some concepts that are common tovirtually all programming languages.
Common concepts:
Key words Operators
Punctuation
Programmer-defined identifiers
Strict syntactic rules
-
7/21/2019 6804068 Chapter 01 Background
48/75
49
Java Documentation
Familiarize yourself with the Java documentation
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
-
7/21/2019 6804068 Chapter 01 Background
49/75
50
Key Words
Key words in the sample program are: public
class
static
void
String String is not really a key word but is the name of a predefined
class in Java
Wellgo over the difference between these later Key words
lower case (Java is a case sensitive language).
cannot be used as a programmer-defined identifier.
-
7/21/2019 6804068 Chapter 01 Background
50/75
51
Programming Languages
Some Java key words have no meaning but arereserved to prevent their use. (ex. goto, const,include)
Semi-colons are used to end Java statements;however, not all lines of a Java program end astatement.
Part of learning Java is to learn where to properly
use the punctuation.
-
7/21/2019 6804068 Chapter 01 Background
51/75
52
Lines vs Statements
There is a difference between lines and statementswhen discussing source code.
System.out.println(
message);
This is one Java statement written using two lines.Do you see the difference?
A statement is a complete Java instruction thatcauses the computer to perform an action.
-
7/21/2019 6804068 Chapter 01 Background
52/75
53
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.");
}
}
-
7/21/2019 6804068 Chapter 01 Background
53/75
54
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.
// 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:
// "I think there is a world market for maybe five computers"
// there is no evidence he ever made it. The author Kevin
// Maney tried to find the origin of the quote. He has been
// unable to locate any speeches or documents of Watson's
// that contain this, nor is it present in any contemporary
// articles about IBM. The earliest known citation is from
// 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
// J. Watson (Chairman of the Board of International
// Business Machines),1943
// While at NCR, he was convicted for illegal anti-
// competitive 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.
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.");
}
}
-
7/21/2019 6804068 Chapter 01 Background
54/75
55
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
-
7/21/2019 6804068 Chapter 01 Background
55/75
56
Human stupidity
-
7/21/2019 6804068 Chapter 01 Background
56/75
Chapter 1
Extra Material
The Programming Process
-
7/21/2019 6804068 Chapter 01 Background
57/75
59
The Programming Process
1. Clearly define what the program is to do.
2. Visualize the program running on the computer.
3. Use design tools to create a model of the program.
4. Check the model for logical errors.
-
7/21/2019 6804068 Chapter 01 Background
58/75
60
The Programming Process
5. Enter the code and compile it.
6. Correct any errors found during compilation.Repeat Steps 5 and 6 as many times as necessary.
7. Run the program with test data for input.
8. Correct any runtime errors found while running theprogram.Repeat Steps 5 through 8 as many times as necessary.
9. Validate the results of the program.
-
7/21/2019 6804068 Chapter 01 Background
59/75
61
Software Engineering
Encompasses the whole process of crafting computersoftware.
Software engineers perform several tasks in the
development of complex software projects. designing,
writing,
testing,
debugging,
documenting, modifying, and
maintaining.
-
7/21/2019 6804068 Chapter 01 Background
60/75
62
Software Engineering
Software engineers develop:
program specifications,
diagrams of screen output,
diagrams representing the program components and theflow of data,
pseudocode,
examples of expected input and desired output.
-
7/21/2019 6804068 Chapter 01 Background
61/75
63
Other buzzwords from the chapter
Im not expecting you to fully understand theseafter reading chapter 1
Procedural programming
Object oriented programming Data hiding
Code reusability
Classes vs. objects
Inheritance We will see all of these before the semester ends
-
7/21/2019 6804068 Chapter 01 Background
62/75
64
Computer Systems
-
7/21/2019 6804068 Chapter 01 Background
63/75
65
Computer Systems
Hardware
The central processing unit (CPU)
Main memory
Secondary storage devices Input and Output devices
Software
Operating systems
Application software
-
7/21/2019 6804068 Chapter 01 Background
64/75
66
Computer Systems: Hardware
Input Devices
Output Devices
Input / OutputDevices
ALU
Control Unit
RAM
-
7/21/2019 6804068 Chapter 01 Background
65/75
67
Computer Systems: Hardware
Computer hardware components are the physicalpieces of the computer.
The major hardware components of a computer
are: The central processing unit (CPU)
Main memory
Secondary storage devices
Input and Output devices
-
7/21/2019 6804068 Chapter 01 Background
66/75
68
Central Processing Unit
Arithmetic
Logic
Unit
ControlUnit
CPU
Instruction (input) Result (output)
-
7/21/2019 6804068 Chapter 01 Background
67/75
69
Central Processing Unit
The CPU performs the fetch, decode, execute cyclein order to process program information.
Fetch
The CPUs control unit fetches, from main memory,
the next instruction in the sequence of programinstructions.
Decode
The instruction is encoded in the form of a
number. The control unit decodes the
instruction and generates an electronic signal.
ExecuteThe signal is routed to the appropriate
component of the computer (such as the ALU,
a disk drive, or some other device). The signal
causes the component to perform an
operation.
-
7/21/2019 6804068 Chapter 01 Background
68/75
70
Main Memory (aka RAM)
Commonly known as random-access memory
(RAM)
RAM
contains: currently running programs
data used by those programs
is volatile when the computer is turned off, the contents of RAM are
erased.
short-term memory
-
7/21/2019 6804068 Chapter 01 Background
69/75
71
Secondary Storage
Secondary storage devices are capable of storinginformation for longer periods of time
non-volatile
long-term memory Examples
Hard drive
CD RW drive
DVD RAM drive Compact Flash card
-
7/21/2019 6804068 Chapter 01 Background
70/75
72
Input Devices
Input is any data the computer collects from theoutside world.
That data comes from devices known as input
devices. Common input devices:
Keyboard
Mouse
Scanner
Digital camera
O i
-
7/21/2019 6804068 Chapter 01 Background
71/75
73
Output Devices
Output is any data the computer sends to theoutside world.
That data is displayed on devices known as output
devices Common output devices:
Monitors
Printers
Some devices such as disk drives perform inputand output and are called I/O devices(input/output).
C S S f
-
7/21/2019 6804068 Chapter 01 Background
72/75
74
Computer Systems: Software
Software refers to the programs that run on acomputer.
Two classifications of software:
Operating Systems Application Software
O ti S t
-
7/21/2019 6804068 Chapter 01 Background
73/75
75
Operating Systems
An operating system has two functions: Control the system resources.
Provide the user with a means of interaction with thecomputer.
Operating systems can be either
single tasking (run one program at a time)
DOS
multi-tasking (run many programs at once) Windows
Unix
Apple
O ti S t
-
7/21/2019 6804068 Chapter 01 Background
74/75
76
Operating Systems
Operating systems can also be categorized as single user
only one user to operate the computer at a time
Examples:
DOS Windows 95/98/ME
multi-user allow several users to run programs and operate the computer at
once.
Examples Unix
BSD
Windows NT/2000/XP
OS/X
A li ti S ft
-
7/21/2019 6804068 Chapter 01 Background
75/75
Application Software
Programs that make the computer useful to theuser
Spreadsheets
Word processors Accounting software
Tax software
Games