Lecture 13 From Algorithms to Programs

advertisement
Lecture 13
2/23/04 23:21
Lecture 13
Introduction to
High-Level Programming
(S&G, §§7.1
–7.6)
§§7.1–
2/23/04
CS 100 - Lecture 13
1
From Algorithms to Programs
• Algorithms are essentially abstract things
that can have
– Linguistic realizations (in pseudocode or
programming languages)
– Hardware realizations (in digital circuitry)
2/23/04
CS 100 - Lecture 13
2
(slide < S. Levy)
CS 100
1
Lecture 13
2/23/04 23:21
What Is a Program?
• Usually, one or more algorithms written in a
programming language that can be
translated to run on a real machine
• We sometimes call programs software
2/23/04
CS 100 - Lecture 13
3
(slide < S. Levy)
What Is a Programming
Language?
• A bit like pseudocode, but with very strict
syntax rules
• Examples: Basic, Pascal, C++, Java
2/23/04
CS 100 - Lecture 13
4
(slide < S. Levy)
CS 100
2
Lecture 13
2/23/04 23:21
From Algorithms to Hardware
Algorithm
Translate (by a human being)
Program
Translate (by another program)
A real computer
2/23/04
CS 100 - Lecture 13
5
(slide < S. Levy)
The Program Development
Process (Data Flow)
Algorithm
Editor
Program in programming language
Compiler
Program in machine’s language
Input
2/23/04
A real computer
CS 100 - Lecture 13
Output
6
(slide < S. Levy)
CS 100
3
Lecture 13
2/23/04 23:21
The Program Development
Process (Control Flow)
Edit
Syntax errors
Compile
Input
Run
Output
Runtime errors
2/23/04
CS 100 - Lecture 13
7
(slide < S. Levy)
Java
• Was developed at Sun Microsystems in the
early 1990s
• A general-purpose language but
– Programs could run in small devices
(embedded systems)
– Internet-ready and Web-ready
– Standard support for multimedia applications
2/23/04
CS 100 - Lecture 13
8
(slide < K. Lambert)
CS 100
4
Lecture 13
2/23/04 23:21
Compiling and Running
a Java Program
• A Java compiler translates a Java program
to an equivalent program in byte code
• A Java virtual machine (JVM) is a program
that interprets a Java byte code program to
execute it
User inputs
Java code
2/23/04
Compiler byte code
JVM
CS 100 - Lecture 13
Program outputs
9
(slide < K. Lambert)
Portability
• A Java program translates to a standard byte
code
• A Java byte code can run on any JVM
• A JVM, and thus a Java program, can run
– In a PDA
– In a desktop computer
– In a Web browser
2/23/04
CS 100 - Lecture 13
10
(slide < K. Lambert)
CS 100
5
Lecture 13
2/23/04 23:21
Statements in Java: Assignment
Pseudocode:
Set the value of <identifier> to <arithmetic expr>
Java:
<identifier> = <arithmetic expr> ;
Examples:
i = i + 1;
force = mass * acceleration;
average = sum / n;
2/23/04
CS 100 - Lecture 13
11
(slide < R. Priebe)
Example Java Statements
// This is a comment.
// Comments are not executable statements
// but exist solely for the benefit of human readers
int width = 20;
// Declare an integer variable and set its
// value to 20
int height = 35;
int area;
// Declare an integer variable; its
// default value is 0
area = height * width;
// An assignment statement
// Pseudocode: set area to height * width
All variables must be declared before they are used in statements.
Each variable has a data type, such as int, double or String.
2/23/04
CS 100 - Lecture 13
12
(slide < S. Levy)
CS 100
6
Lecture 13
2/23/04 23:21
Basic Data Types
2/23/04
Type Name
Example Values
int
34, 0, 10000000
double
3.14,
0.563333333
String
"Java rules!"
char
'a', '9', '%'
boolean
true, false
CS 100 - Lecture 13
13
(slide < S. Levy)
Algorithms that use arrays, such
as Sequential Search
Given values for Name, N1, …, N10 and for T1, …, T10
Set the value of i to 1 and set the value of Found to NO
Repeat until either Found = YES or i > 10
In pseudo code, we use
If Name is equal to Ni, then
subscripts
to create a list …
Print Ti
Set the value of Found to YES
Else
… and to reference
Increment i
particular elements of it
If (Found = NO) then
Print “Sorry, but the name’s not in the directory”
Stop
2/23/04
CS 100 - Lecture 13
14
(slide < R. Priebe)
CS 100
7
Lecture 13
2/23/04 23:21
Translating to Java
• Here are examples of creating arrays:
int[ ] years = new int[10]; // a list
double[ ] area = new double[2000]; //
//
String[ ] name = new String[40];
//
of 10 integers
a list of 2000
real numbers
a list of 40 strings
• Here are examples of referencing elements:
firstyear = years[0];
//
//
lastarea = area[1999]; //
//
2/23/04
warning: array elements are
numbered starting at zero !
the last element in the
list of area’s
CS 100 - Lecture 13
15
(slide < R. Priebe)
Statements in Java: Input
Pseudocode:
Ask user to enter a value of <identifier>
Java:
<identifier> = Console.readInt(<prompt>);
<identifier> = Console.readDouble(<prompt>);
<identifier> = Console.readChar(<prompt>);
Examples:
int speed;
speed = Console.readInt ("Please enter" +
" the speed you traveled");
2/23/04
CS 100 - Lecture 13
16
(slide < R. Priebe)
CS 100
8
Lecture 13
2/23/04 23:21
Statements in Java: Output
Pseudocode:
Output the value of <identifier>
Java w/ examples:
System.out.println("thanks for the data");
System.out.println("The time for your trip is " + time);
System.out.println("A trip of " + distance + " miles "
+ "at " + speed + " mph " + "requires " + time + " hours");
2/23/04
CS 100 - Lecture 13
17
(slide < R. Priebe)
Example Problem
Write a program that computes the area of a circle.
2/23/04
CS 100 - Lecture 13
18
(slide < S. Levy)
CS 100
9
Lecture 13
2/23/04 23:21
From Problem to Algorithm
The algorithm
• uses area = πr2
• accepts as input a decimal point number
representing the radius of the circle
• computes and displays the area of the
corresponding circle
2/23/04
CS 100 - Lecture 13
19
(slide adapt. < S. Levy)
Pseudocode for Program
input radius
area = π × radius × radius
output area
2/23/04
CS 100 - Lecture 13
20
(slide adapt. < S. Levy)
CS 100
10
Lecture 13
2/23/04 23:21
Basic Computation
radius = Console.readInt ("Radius?");
area = Math.PI * radius * radius;
System.out.println ("Area = " + area);
2/23/04
CS 100 - Lecture 13
From pseudocode
21
(slide adapt. < S. Levy)
Complete Program
public class CircleArea
{
double radius, area;
public static void main (String[] args)
{
radius = Console.readInt ("Radius?");
area = Math.PI * radius * radius;
System.out.println ("Area = " + area);
}
From pseudocode
}
2/23/04
CS 100 - Lecture 13
22
(slide adapt. < S. Levy)
CS 100
11
Lecture 13
2/23/04 23:21
Digression: What Does It All
Mean?
The language machine regulates and adjusts in advance the
mode of our possible usage of language through mechanical
energies and functions. The language machine is — and above
all, is still becoming — one way in which modern technology
controls the mode and the world of language as such.
Meanwhile, the impression is still maintained that man is the
master of the language machine. But the truth of the matter
might well be that the language machine takes language into its
management and thus masters the essence of the human being.
— Martin Heidegger
2/23/04
CS 100 - Lecture 13
23
(slide < S. Levy)
Java Methods
// A method is like a little program that performs a complex task
double d = Math.sqrt(25);
// d is 5.0
int i = Math.abs(-4);
// i is 4
i = Math.pow(2, 10);
// i is 1024
System.out.println("Hello there!");
// Displays output in
// the terminal window
The data in the parentheses are called parameters.
Methods receive information in parameters and can return
information to the rest of the program.
2/23/04
CS 100 - Lecture 13
24
(slide < S. Levy)
CS 100
12
Lecture 13
2/23/04 23:21
Structure of Simple Program:
The Main Method
public class <name of program>
{
public static void main (String[] args)
{
<declarations>
<statements>
} //end of main method
}
//end of program
A Java method describes data and an algorithm as a chunk of
program code.
The main method is run when the program starts up.
2/23/04
CS 100 - Lecture 13
25
(slide < S. Levy)
Java Control Structures
Conditionals
(if statements)
2/23/04
CS 100
CS 100 - Lecture 13
26
13
Lecture 13
2/23/04 23:21
Syntax of Compound Statements
{
}
<statement1>
<statement2>
…
<statementn>
A compound statement is a sequence of zero or more
statements.
2/23/04
CS 100 - Lecture 13
27
(slide adapted < K. Lambert)
Syntax of Selection Statements
2/23/04
if (<Boolean expression>)
<statement>
// One-way decision
if (<Boolean expression>)
<statement>
else
<statement>
// Two-way decision
CS 100 - Lecture 13
28
(slide < K. Lambert)
CS 100
14
Lecture 13
2/23/04 23:21
Semantics of
Selection Statements
if (<Boolean expression>)
<statement>
true
?
false
statement
if (<Boolean expression>)
<statement>
else
<statement>
true
?
false
statement
2/23/04
statement
CS 100 - Lecture 13
29
(slide < K. Lambert)
Java Control Structures
Iteration/Loops
(while statements and for statements)
2/23/04
CS 100 - Lecture 13
30
(slide < S. Levy)
CS 100
15
Lecture 13
2/23/04 23:21
Example: Summation
Write a program that computes the sum of the
numbers between 1 and 10.
Set counter to 1
Set sum to 0
While counter ≤ 10 do
Set sum to sum + counter
Increment counter
Print sum
2/23/04
CS 100 - Lecture 13
31
(slide < S. Levy)
Example: Summation
Write a program that computes the sum of the
numbers between 1 and 10.
int counter = 1;
int sum = 0;
while (counter <= 10)
{
sum = sum + counter;
counter = counter + 1;
}
System.out.println (sum);
2/23/04
CS 100 - Lecture 13
32
(slide < S. Levy)
CS 100
16
Lecture 13
2/23/04 23:21
Syntax and Semantics of
while Statements
while (<Boolean expression>)
<statement>
while (<Boolean expression>)
{
<statement 1>
…
true
?
false
statement
<statement n>
}
2/23/04
CS 100 - Lecture 13
33
(slide < S. Levy)
Count-controlled Loops
// General form
<initialize a counter variable>
while (<test counter for termination condition>){
<do something>
<change the value of counter>
}
2/23/04
CS 100 - Lecture 13
34
(slide < S. Levy)
CS 100
17
Lecture 13
2/23/04 23:21
Count-controlled Loops
// General form
<initialize a counter variable>
while (<test counter for termination condition>){
<do something>
<change the value of counter>
}
// Count up
<initialize a counter variable>
while (<counter is less than a limit value>){
<do something>
<increase the value of counter>
}
2/23/04
CS 100 - Lecture 13
35
(slide < S. Levy)
Count-controlled Loops
// General form
<initialize a counter variable>
while (<test counter for termination condition>){
<do something>
<change the value of counter>
}
// Count up
<initialize a counter variable>
while (<counter is less than a limit value>){
<do something>
<increase the value of counter>
}
int counter = 1;
while (counter <= 10){
<do something>
counter = counter + 1;
}
2/23/04
CS 100 - Lecture 13
36
(slide < S. Levy)
CS 100
18
Lecture 13
2/23/04 23:21
Count-controlled Loops
// General form
<initialize a counter variable>
while (<test counter for termination condition>){
<do something>
<change the value of counter>
}
// Count down
<initialize a counter variable>
while (<counter is greater than a limit value>){
<do something>
<decrease the value of counter>
}
2/23/04
CS 100 - Lecture 13
37
(slide < S. Levy)
Count-controlled Loops
// General form
<initialize a counter variable>
while (<test counter for termination condition>){
<do something>
<change the value of counter>
}
// Count down
<initialize a counter variable>
while (<counter is greater than a limit value>){
<do something>
<decrease the value of counter>
}
2/23/04
int counter = 10;
while (counter > 0){
<do something>
counter = counter - 1;
}
CS 100 - Lecture 13
38
(slide < S. Levy)
CS 100
19
Lecture 13
2/23/04 23:21
Increment and Decrement
int counter = 1;
while (counter <= 10){
<do something>
counter = counter + 1;
}
int counter = 10;
while (counter > 0){
<do something>
counter = counter - 1;
}
2/23/04
CS 100 - Lecture 13
39
(slide < S. Levy)
Increment and Decrement
int counter = 1;
while (counter <= 10){
<do something>
counter++;
}
int counter = 10;
while (counter > 0){
<do something>
counter--;
}
2/23/04
CS 100 - Lecture 13
40
(slide < S. Levy)
CS 100
20
Lecture 13
2/23/04 23:21
Designing Correct Loops
• Initialize all variables properly
– Plan how many iterations, then set the counter
and the limit accordingly
• Check the logic of the termination condition
• Update the loop control variable properly
2/23/04
CS 100 - Lecture 13
41
(slide < S. Levy)
Off-by-One Error
int counter = 1;
while (counter <= 10){
<do something>
counter++;
}
int counter = 1;
while (counter < 10){
<do something>
counter++;
}
2/23/04
// Executes 10 passes
// Executes 9 passes
CS 100 - Lecture 13
42
(slide < S. Levy)
CS 100
21
Lecture 13
2/23/04 23:21
Infinite Loop
int counter = 1;
while (counter <= 10){
<do something>
counter = counter + 2;
}
int counter = 1;
while (counter != 10){
<do something>
counter = counter + 2;
}
// Executes 5 passes
// Runs forever
In general, avoid using != in loop termination conditions.
2/23/04
CS 100 - Lecture 13
43
(slide < S. Levy)
The for Loop
int counter = 1;
while (counter <= 10){
<do something>
counter++;
}
for (int counter = 1; counter <= 10; counter++)
<do something>
2/23/04
CS 100 - Lecture 13
44
(slide < S. Levy)
CS 100
22
Lecture 13
2/23/04 23:21
The for Loop
int counter = 10;
while (counter > 0){
<do something>
counter--;
}
for (int counter = 10; counter > 0; counter--)
<do something>
2/23/04
CS 100 - Lecture 13
45
(slide < S. Levy)
Syntax and Semantics of
the for Loop
for (<initializer>; <termination>; <update>)
<statement>
false
initializer
termination
true
statement
update
2/23/04
CS 100 - Lecture 13
46
(slide < S. Levy)
CS 100
23
Lecture 13
2/23/04 23:21
The Life Cycle of Programs:
Waterfall Model
Analysis
-----------Verify
Design
-----------Verify
Implementation
------------------Test
Integration
------------Test
Maintenance
2/23/04
CS 100 - Lecture 13
47
(slide < S. Levy)
Maintenance
• Programs might have a lifetime of 5-20
years
• Requirements might change, and minor or
major changes would be necessary
2/23/04
CS 100 - Lecture 13
48
(slide < S. Levy)
CS 100
24
Lecture 13
2/23/04 23:21
The Cost of Correcting an Error
Cost of
Correcting
a Fault
implementation
maintenance
analysis
integration
design
Software Development Phase
2/23/04
CS 100 - Lecture 13
49
(slide < S. Levy)
The Relative Costs of the
Phases
2/23/04
CS 100 - Lecture 13
50
(slide < S. Levy)
CS 100
25
Download