lecture 13 - web.eecs.utk.eduweb.eecs.utk.edu/~bmaclenn/classes/100/lectures/lecture13big.pdf ·...

25
Lecture 13 2/23/04 23:21 CS 100 1 2/23/04 CS 100 - Lecture 13 1 Lecture 13 Lecture 13 Introduction to Introduction to High-Level Programming High-Level Programming (S&G, (S&G, §§ §§ 7.1 7.1– 7.6) 7.6) 2/23/04 CS 100 - Lecture 13 2 From Algorithms to Programs Algorithms are essentially abstract things that can have – Linguistic realizations (in pseudocode or programming languages) – Hardware realizations (in digital circuitry) (slide < S. Levy)

Upload: others

Post on 11-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 13 - web.eecs.utk.eduweb.eecs.utk.edu/~bmaclenn/Classes/100/lectures/Lecture13big.pdf · Lecture 13 2/23/04 23:21 CS 100 7 2/23/04 CS 100 - Lecture 13 13 Basic Data Types

Lecture 13 2/23/04 23:21

CS 100 1

2/23/04 CS 100 - Lecture 13 1

Lecture 13Lecture 13

Introduction toIntroduction toHigh-Level ProgrammingHigh-Level Programming

(S&G, (S&G, §§§§7.17.1––7.6)7.6)

2/23/04 CS 100 - Lecture 13 2

From Algorithms to Programs

• Algorithms are essentially abstract thingsthat can have– Linguistic realizations (in pseudocode or

programming languages)– Hardware realizations (in digital circuitry)

(slide < S. Levy)

Page 2: Lecture 13 - web.eecs.utk.eduweb.eecs.utk.edu/~bmaclenn/Classes/100/lectures/Lecture13big.pdf · Lecture 13 2/23/04 23:21 CS 100 7 2/23/04 CS 100 - Lecture 13 13 Basic Data Types

Lecture 13 2/23/04 23:21

CS 100 2

2/23/04 CS 100 - Lecture 13 3

What Is a Program?

• Usually, one or more algorithms written in aprogramming language that can betranslated to run on a real machine

• We sometimes call programs software

(slide < S. Levy)

2/23/04 CS 100 - Lecture 13 4

What Is a ProgrammingLanguage?

• A bit like pseudocode, but with very strictsyntax rules

• Examples: Basic, Pascal, C++, Java

(slide < S. Levy)

Page 3: Lecture 13 - web.eecs.utk.eduweb.eecs.utk.edu/~bmaclenn/Classes/100/lectures/Lecture13big.pdf · Lecture 13 2/23/04 23:21 CS 100 7 2/23/04 CS 100 - Lecture 13 13 Basic Data Types

Lecture 13 2/23/04 23:21

CS 100 3

2/23/04 CS 100 - Lecture 13 5

From Algorithms to Hardware

Algorithm

Program

Translate (by a human being)

A real computer

Translate (by another program)

(slide < S. Levy)

2/23/04 CS 100 - Lecture 13 6

The Program DevelopmentProcess (Data Flow)

Editor

Compiler

A real computer

Algorithm

Program in programming language

Program in machine’s language

Input Output

(slide < S. Levy)

Page 4: Lecture 13 - web.eecs.utk.eduweb.eecs.utk.edu/~bmaclenn/Classes/100/lectures/Lecture13big.pdf · Lecture 13 2/23/04 23:21 CS 100 7 2/23/04 CS 100 - Lecture 13 13 Basic Data Types

Lecture 13 2/23/04 23:21

CS 100 4

2/23/04 CS 100 - Lecture 13 7

The Program DevelopmentProcess (Control Flow)

Edit

Compile

Run

Syntax errors

Input Output

Runtime errors

(slide < S. Levy)

2/23/04 CS 100 - Lecture 13 8

Java

• Was developed at Sun Microsystems in theearly 1990s

• A general-purpose language but– Programs could run in small devices

(embedded systems)– Internet-ready and Web-ready– Standard support for multimedia applications

(slide < K. Lambert)

Page 5: Lecture 13 - web.eecs.utk.eduweb.eecs.utk.edu/~bmaclenn/Classes/100/lectures/Lecture13big.pdf · Lecture 13 2/23/04 23:21 CS 100 7 2/23/04 CS 100 - Lecture 13 13 Basic Data Types

Lecture 13 2/23/04 23:21

CS 100 5

2/23/04 CS 100 - Lecture 13 9

Compiling and Runninga Java Program

• A Java compiler translates a Java programto an equivalent program in byte code

• A Java virtual machine (JVM) is a programthat interprets a Java byte code program toexecute it

CompilerJava code JVMbyte code

User inputs

Program outputs

(slide < K. Lambert)

2/23/04 CS 100 - Lecture 13 10

Portability

• A Java program translates to a standard bytecode

• 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

(slide < K. Lambert)

Page 6: Lecture 13 - web.eecs.utk.eduweb.eecs.utk.edu/~bmaclenn/Classes/100/lectures/Lecture13big.pdf · Lecture 13 2/23/04 23:21 CS 100 7 2/23/04 CS 100 - Lecture 13 13 Basic Data Types

Lecture 13 2/23/04 23:21

CS 100 6

2/23/04 CS 100 - Lecture 13 11

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;

(slide < R. Priebe)

2/23/04 CS 100 - Lecture 13 12

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 20int 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.

(slide < S. Levy)

Page 7: Lecture 13 - web.eecs.utk.eduweb.eecs.utk.edu/~bmaclenn/Classes/100/lectures/Lecture13big.pdf · Lecture 13 2/23/04 23:21 CS 100 7 2/23/04 CS 100 - Lecture 13 13 Basic Data Types

Lecture 13 2/23/04 23:21

CS 100 7

2/23/04 CS 100 - Lecture 13 13

Basic Data Types

true, falseboolean

'a', '9', '%'char

"Java rules!"String

3.14,0.563333333

double

34, 0, 10000000int

Example ValuesType Name

(slide < S. Levy)

2/23/04 CS 100 - Lecture 13 14

Algorithms that use arrays, suchas 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 NORepeat until either Found = YES or i > 10

If Name is equal to Ni, then Print Ti

Set the value of Found to YESElse

Increment i If (Found = NO) then

Print “Sorry, but the name’s not in the directory”Stop

In pseudo code, we usesubscripts to create a list …

… and to referenceparticular elements of it

(slide < R. Priebe)

Page 8: Lecture 13 - web.eecs.utk.eduweb.eecs.utk.edu/~bmaclenn/Classes/100/lectures/Lecture13big.pdf · Lecture 13 2/23/04 23:21 CS 100 7 2/23/04 CS 100 - Lecture 13 13 Basic Data Types

Lecture 13 2/23/04 23:21

CS 100 8

2/23/04 CS 100 - Lecture 13 15

Translating to Java• Here are examples of creating arrays:

int[ ] years = new int[10]; // a list of 10 integersdouble[ ] area = new double[2000]; // a list of 2000

// real numbers String[ ] name = new String[40]; // a list of 40 strings

• Here are examples of referencing elements: firstyear = years[0]; // warning: array elements are

// numbered starting at zero ! lastarea = area[1999]; // the last element in the // list of area’s

(slide < R. Priebe)

2/23/04 CS 100 - Lecture 13 16

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");

(slide < R. Priebe)

Page 9: Lecture 13 - web.eecs.utk.eduweb.eecs.utk.edu/~bmaclenn/Classes/100/lectures/Lecture13big.pdf · Lecture 13 2/23/04 23:21 CS 100 7 2/23/04 CS 100 - Lecture 13 13 Basic Data Types

Lecture 13 2/23/04 23:21

CS 100 9

2/23/04 CS 100 - Lecture 13 17

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");

(slide < R. Priebe)

2/23/04 CS 100 - Lecture 13 18

Example Problem

Write a program that computes the area of a circle.

(slide < S. Levy)

Page 10: Lecture 13 - web.eecs.utk.eduweb.eecs.utk.edu/~bmaclenn/Classes/100/lectures/Lecture13big.pdf · Lecture 13 2/23/04 23:21 CS 100 7 2/23/04 CS 100 - Lecture 13 13 Basic Data Types

Lecture 13 2/23/04 23:21

CS 100 10

2/23/04 CS 100 - Lecture 13 19

From Problem to Algorithm

The algorithm• uses area = πr2

• accepts as input a decimal point numberrepresenting the radius of the circle

• computes and displays the area of thecorresponding circle

(slide adapt. < S. Levy)

2/23/04 CS 100 - Lecture 13 20

Pseudocode for Program

input radiusarea = π × radius × radiusoutput area

(slide adapt. < S. Levy)

Page 11: Lecture 13 - web.eecs.utk.eduweb.eecs.utk.edu/~bmaclenn/Classes/100/lectures/Lecture13big.pdf · Lecture 13 2/23/04 23:21 CS 100 7 2/23/04 CS 100 - Lecture 13 13 Basic Data Types

Lecture 13 2/23/04 23:21

CS 100 11

2/23/04 CS 100 - Lecture 13 21

From pseudocode radius = Console.readInt ("Radius?"); area = Math.PI * radius * radius; System.out.println ("Area = " + area);

Basic Computation

(slide adapt. < S. Levy)

2/23/04 CS 100 - Lecture 13 22

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); } }

(slide adapt. < S. Levy)

From pseudocode

Complete Program

Page 12: Lecture 13 - web.eecs.utk.eduweb.eecs.utk.edu/~bmaclenn/Classes/100/lectures/Lecture13big.pdf · Lecture 13 2/23/04 23:21 CS 100 7 2/23/04 CS 100 - Lecture 13 13 Basic Data Types

Lecture 13 2/23/04 23:21

CS 100 12

2/23/04 CS 100 - Lecture 13 23

The language machine regulates and adjusts in advance themode of our possible usage of language through mechanicalenergies and functions. The language machine is — and aboveall, is still becoming — one way in which modern technologycontrols the mode and the world of language as such.Meanwhile, the impression is still maintained that man is themaster of the language machine. But the truth of the mattermight well be that the language machine takes language into itsmanagement and thus masters the essence of the human being.

— Martin Heidegger

Digression: What Does It AllMean?

(slide < S. Levy)

2/23/04 CS 100 - Lecture 13 24

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 returninformation to the rest of the program.

(slide < S. Levy)

Page 13: Lecture 13 - web.eecs.utk.eduweb.eecs.utk.edu/~bmaclenn/Classes/100/lectures/Lecture13big.pdf · Lecture 13 2/23/04 23:21 CS 100 7 2/23/04 CS 100 - Lecture 13 13 Basic Data Types

Lecture 13 2/23/04 23:21

CS 100 13

2/23/04 CS 100 - Lecture 13 25

public class <name of program> { public static void main (String[] args) { <declarations> <statements> } //end of main method } //end of program

Structure of Simple Program:The Main Method

A Java method describes data and an algorithm as a chunk ofprogram code.The main method is run when the program starts up.

(slide < S. Levy)

2/23/04 CS 100 - Lecture 13 26

Java Control Structures

Conditionals(if statements)

Page 14: Lecture 13 - web.eecs.utk.eduweb.eecs.utk.edu/~bmaclenn/Classes/100/lectures/Lecture13big.pdf · Lecture 13 2/23/04 23:21 CS 100 7 2/23/04 CS 100 - Lecture 13 13 Basic Data Types

Lecture 13 2/23/04 23:21

CS 100 14

2/23/04 CS 100 - Lecture 13 27

Syntax of Compound Statements

{ <statement1> <statement2> … <statementn>}

A compound statement is a sequence of zero or morestatements.

(slide adapted < K. Lambert)

2/23/04 CS 100 - Lecture 13 28

Syntax of Selection Statementsif (<Boolean expression>) // One-way decision <statement>

if (<Boolean expression>) // Two-way decision <statement>else <statement>

(slide < K. Lambert)

Page 15: Lecture 13 - web.eecs.utk.eduweb.eecs.utk.edu/~bmaclenn/Classes/100/lectures/Lecture13big.pdf · Lecture 13 2/23/04 23:21 CS 100 7 2/23/04 CS 100 - Lecture 13 13 Basic Data Types

Lecture 13 2/23/04 23:21

CS 100 15

2/23/04 CS 100 - Lecture 13 29

Semantics ofSelection Statements

if (<Boolean expression>) <statement>

if (<Boolean expression>) <statement>else <statement>

?

statement

true

?

statement

false

true

false

statement

(slide < K. Lambert)

2/23/04 CS 100 - Lecture 13 30

Java Control Structures

Iteration/Loops(while statements and for statements)

(slide < S. Levy)

Page 16: Lecture 13 - web.eecs.utk.eduweb.eecs.utk.edu/~bmaclenn/Classes/100/lectures/Lecture13big.pdf · Lecture 13 2/23/04 23:21 CS 100 7 2/23/04 CS 100 - Lecture 13 13 Basic Data Types

Lecture 13 2/23/04 23:21

CS 100 16

2/23/04 CS 100 - Lecture 13 31

Example: Summation

Write a program that computes the sum of thenumbers between 1 and 10.

Set counter to 1Set sum to 0While counter ≤ 10 do Set sum to sum + counter Increment counterPrint sum

(slide < S. Levy)

2/23/04 CS 100 - Lecture 13 32

Example: Summation

Write a program that computes the sum of thenumbers between 1 and 10.

int counter = 1;int sum = 0;while (counter <= 10){ sum = sum + counter; counter = counter + 1;}System.out.println (sum);

(slide < S. Levy)

Page 17: Lecture 13 - web.eecs.utk.eduweb.eecs.utk.edu/~bmaclenn/Classes/100/lectures/Lecture13big.pdf · Lecture 13 2/23/04 23:21 CS 100 7 2/23/04 CS 100 - Lecture 13 13 Basic Data Types

Lecture 13 2/23/04 23:21

CS 100 17

2/23/04 CS 100 - Lecture 13 33

Syntax and Semantics ofwhile Statements

while (<Boolean expression>) <statement>

while (<Boolean expression>){ <statement 1> … <statement n>}

?

statement

true

false

(slide < S. Levy)

2/23/04 CS 100 - Lecture 13 34

Count-controlled Loops// General form<initialize a counter variable>while (<test counter for termination condition>){ <do something> <change the value of counter>}

(slide < S. Levy)

Page 18: Lecture 13 - web.eecs.utk.eduweb.eecs.utk.edu/~bmaclenn/Classes/100/lectures/Lecture13big.pdf · Lecture 13 2/23/04 23:21 CS 100 7 2/23/04 CS 100 - Lecture 13 13 Basic Data Types

Lecture 13 2/23/04 23:21

CS 100 18

2/23/04 CS 100 - Lecture 13 35

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>}

(slide < S. Levy)

2/23/04 CS 100 - Lecture 13 36

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;}

(slide < S. Levy)

Page 19: Lecture 13 - web.eecs.utk.eduweb.eecs.utk.edu/~bmaclenn/Classes/100/lectures/Lecture13big.pdf · Lecture 13 2/23/04 23:21 CS 100 7 2/23/04 CS 100 - Lecture 13 13 Basic Data Types

Lecture 13 2/23/04 23:21

CS 100 19

2/23/04 CS 100 - Lecture 13 37

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>}

(slide < S. Levy)

2/23/04 CS 100 - Lecture 13 38

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>}

int counter = 10;while (counter > 0){ <do something> counter = counter - 1;}

(slide < S. Levy)

Page 20: Lecture 13 - web.eecs.utk.eduweb.eecs.utk.edu/~bmaclenn/Classes/100/lectures/Lecture13big.pdf · Lecture 13 2/23/04 23:21 CS 100 7 2/23/04 CS 100 - Lecture 13 13 Basic Data Types

Lecture 13 2/23/04 23:21

CS 100 20

2/23/04 CS 100 - Lecture 13 39

Increment and Decrementint counter = 1;while (counter <= 10){ <do something> counter = counter + 1;}

int counter = 10;while (counter > 0){ <do something> counter = counter - 1;}

(slide < S. Levy)

2/23/04 CS 100 - Lecture 13 40

Increment and Decrementint counter = 1;while (counter <= 10){ <do something> counter++;}

int counter = 10;while (counter > 0){ <do something> counter--;}

(slide < S. Levy)

Page 21: Lecture 13 - web.eecs.utk.eduweb.eecs.utk.edu/~bmaclenn/Classes/100/lectures/Lecture13big.pdf · Lecture 13 2/23/04 23:21 CS 100 7 2/23/04 CS 100 - Lecture 13 13 Basic Data Types

Lecture 13 2/23/04 23:21

CS 100 21

2/23/04 CS 100 - Lecture 13 41

Designing Correct Loops• Initialize all variables properly

– Plan how many iterations, then set the counterand the limit accordingly

• Check the logic of the termination condition• Update the loop control variable properly

(slide < S. Levy)

2/23/04 CS 100 - Lecture 13 42

Off-by-One Errorint counter = 1;while (counter <= 10){ // Executes 10 passes <do something> counter++;}

int counter = 1;while (counter < 10){ // Executes 9 passes <do something> counter++;}

(slide < S. Levy)

Page 22: Lecture 13 - web.eecs.utk.eduweb.eecs.utk.edu/~bmaclenn/Classes/100/lectures/Lecture13big.pdf · Lecture 13 2/23/04 23:21 CS 100 7 2/23/04 CS 100 - Lecture 13 13 Basic Data Types

Lecture 13 2/23/04 23:21

CS 100 22

2/23/04 CS 100 - Lecture 13 43

Infinite Loopint counter = 1;while (counter <= 10){ // Executes 5 passes <do something> counter = counter + 2;}

int counter = 1;while (counter != 10){ // Runs forever <do something> counter = counter + 2;}

In general, avoid using != in loop termination conditions.

(slide < S. Levy)

2/23/04 CS 100 - Lecture 13 44

The for Loopint counter = 1;while (counter <= 10){ <do something> counter++;}

for (int counter = 1; counter <= 10; counter++) <do something>

(slide < S. Levy)

Page 23: Lecture 13 - web.eecs.utk.eduweb.eecs.utk.edu/~bmaclenn/Classes/100/lectures/Lecture13big.pdf · Lecture 13 2/23/04 23:21 CS 100 7 2/23/04 CS 100 - Lecture 13 13 Basic Data Types

Lecture 13 2/23/04 23:21

CS 100 23

2/23/04 CS 100 - Lecture 13 45

The for Loopint counter = 10;while (counter > 0){ <do something> counter--;}

for (int counter = 10; counter > 0; counter--) <do something>

(slide < S. Levy)

2/23/04 CS 100 - Lecture 13 46

Syntax and Semantics ofthe for Loop

for (<initializer>; <termination>; <update>) <statement>

termination

statement

true

falseinitializer

update

(slide < S. Levy)

Page 24: Lecture 13 - web.eecs.utk.eduweb.eecs.utk.edu/~bmaclenn/Classes/100/lectures/Lecture13big.pdf · Lecture 13 2/23/04 23:21 CS 100 7 2/23/04 CS 100 - Lecture 13 13 Basic Data Types

Lecture 13 2/23/04 23:21

CS 100 24

2/23/04 CS 100 - Lecture 13 47

The Life Cycle of Programs:Waterfall Model

Analysis------------Verify

Design------------Verify

Implementation-------------------

Test

Integration-------------Test

Maintenance

(slide < S. Levy)

2/23/04 CS 100 - Lecture 13 48

Maintenance

• Programs might have a lifetime of 5-20years

• Requirements might change, and minor ormajor changes would be necessary

(slide < S. Levy)

Page 25: Lecture 13 - web.eecs.utk.eduweb.eecs.utk.edu/~bmaclenn/Classes/100/lectures/Lecture13big.pdf · Lecture 13 2/23/04 23:21 CS 100 7 2/23/04 CS 100 - Lecture 13 13 Basic Data Types

Lecture 13 2/23/04 23:21

CS 100 25

2/23/04 CS 100 - Lecture 13 49

The Cost of Correcting an Error

Cost ofCorrectinga Fault

Software Development Phase

analysisdesign

implementationintegration

maintenance

(slide < S. Levy)

2/23/04 CS 100 - Lecture 13 50

The Relative Costs of thePhases

(slide < S. Levy)