Lecture 13 2/23/04 23:21 From Algorithms to Programs • Algorithms are essentially abstract things that can have Lecture 13 – Linguistic realizations (in pseudocode or programming languages) – Hardware realizations (in digital circuitry) Introduction to High-Level Programming (S&G, §§7.1 –7.6) §§7.1– 2/23/04 CS 100 - Lecture 13 1 2/23/04 CS 100 - Lecture 13 2 (slide < S. Levy) What Is a Programming Language? 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 • A bit like pseudocode, but with very strict syntax rules • Examples: Basic, Pascal, C++, Java 2/23/04 CS 100 - Lecture 13 (slide < S. Levy) The Program Development Process (Data Flow) From Algorithms to Hardware Algorithm Algorithm Editor Translate (by a human being) Program Program in machine’s language A real computer CS 100 - Lecture 13 (slide < S. Levy) CS 100 Program in programming language Compiler Translate (by another program) 2/23/04 4 (slide < S. Levy) Input 5 2/23/04 A real computer CS 100 - Lecture 13 Output 6 (slide < S. Levy) 1 Lecture 13 2/23/04 23:21 The Program Development Process (Control Flow) Java • Was developed at Sun Microsystems in the early 1990s • A general-purpose language but Edit Syntax errors Compile Input Run – Programs could run in small devices (embedded systems) – Internet-ready and Web-ready – Standard support for multimedia applications Output Runtime errors 2/23/04 CS 100 - Lecture 13 7 2/23/04 CS 100 - Lecture 13 (slide < S. Levy) Compiling and Running a Java Program Portability • 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 • 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 User inputs Java code 2/23/04 Compiler byte code JVM Program outputs CS 100 - Lecture 13 9 – In a PDA – In a desktop computer – In a Web browser 2/23/04 CS 100 - Lecture 13 (slide < K. Lambert) Example Java Statements // This is a comment. // Comments are not executable statements // but exist solely for the benefit of human readers Pseudocode: Set the value of <identifier> to <arithmetic expr> Java: <identifier> = <arithmetic expr> ; Examples: i = i + 1; force = mass * acceleration; average = sum / n; CS 100 - Lecture 13 (slide < R. Priebe) CS 100 10 (slide < K. Lambert) Statements in Java: Assignment 2/23/04 8 (slide < K. Lambert) 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. 11 2/23/04 CS 100 - Lecture 13 12 (slide < S. Levy) 2 Lecture 13 2/23/04 23:21 Algorithms that use arrays, such as Sequential Search Basic Data Types Type Name Example Values int 34, 0, 10000000 double 3.14, 0.563333333 String "Java rules!" char 'a', '9', '%' boolean true, false 2/23/04 CS 100 - Lecture 13 Given values for Name, N1, …, N 10 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 13 2/23/04 (slide < S. Levy) Statements in Java: Input • Here are examples of creating arrays: 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 14 (slide < R. Priebe) Translating to Java int[ ] years = new int[10]; // a list double[ ] area = new double[2000]; // // String[ ] name = new String[40]; // CS 100 - Lecture 13 warning: array elements are numbered starting at zero ! the last element in the list of area’s CS 100 - Lecture 13 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"); 15 2/23/04 (slide < R. Priebe) CS 100 - Lecture 13 16 (slide < R. Priebe) Statements in Java: Output Example Problem Pseudocode: Output the value of <identifier> Write a program that computes the area of a circle. 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 (slide < R. Priebe) CS 100 17 2/23/04 CS 100 - Lecture 13 18 (slide < S. Levy) 3 Lecture 13 2/23/04 23:21 From Problem to Algorithm Pseudocode for Program 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 input radius area = π × radius × radius output area 19 2/23/04 CS 100 - Lecture 13 20 (slide adapt. < S. Levy) (slide adapt. < S. Levy) Basic Computation Complete Program public class CircleArea { double radius, area; radius = Console.readInt ("Radius?"); area = Math.PI * radius * radius; System.out.println ("Area = " + area); public static void main (String[] args) { radius = Console.readInt ("Radius?"); area = Math.PI * radius * radius; System.out.println ("Area = " + area); } From pseudocode From pseudocode } 2/23/04 CS 100 - Lecture 13 21 2/23/04 CS 100 - Lecture 13 (slide adapt. < S. Levy) Digression: What Does It All Mean? Java Methods // A method is like a little program that performs a complex task 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. CS 100 - Lecture 13 (slide < S. Levy) CS 100 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. — Martin Heidegger 2/23/04 22 (slide adapt. < S. Levy) Methods receive information in parameters and can return information to the rest of the program. 23 2/23/04 CS 100 - Lecture 13 24 (slide < S. Levy) 4 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 Java Control Structures Conditionals (if statements) 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 2/23/04 CS 100 - Lecture 13 26 (slide < S. Levy) Syntax of Compound Statements Syntax of Selection Statements { } <statement 1> <statement 2> … <statement n> A compound statement is a sequence of zero or more statements. 2/23/04 CS 100 - Lecture 13 27 2/23/04 (slide adapted < K. Lambert) if (<Boolean expression>) <statement> // One-way decision if (<Boolean expression>) <statement> else <statement> // Two-way decision CS 100 - Lecture 13 28 (slide < K. Lambert) Semantics of Selection Statements if (<Boolean expression>) <statement> true ? false Java Control Structures statement if (<Boolean expression>) <statement> else <statement> ? true statement 2/23/04 CS 100 - Lecture 13 (slide < K. Lambert) CS 100 Iteration/Loops (while statements and for statements) false statement 29 2/23/04 CS 100 - Lecture 13 30 (slide < S. Levy) 5 Lecture 13 2/23/04 23:21 Example: Summation Example: Summation Write a program that computes the sum of the numbers between 1 and 10. 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 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 31 2/23/04 (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> } while (<Boolean expression>) <statement> … 32 (slide < S. Levy) Syntax and Semantics of while Statements while (<Boolean expression>) { <statement 1> CS 100 - Lecture 13 true ? false statement <statement n> } 2/23/04 CS 100 - Lecture 13 33 2/23/04 (slide < S. Levy) Count-controlled Loops Count-controlled Loops // General form <initialize a counter variable> while (<test counter for termination condition>){ <do something> <change the value of counter> } // General form <initialize a counter variable> while (<test counter for termination condition>){ <do something> <change the value of counter> } CS 100 - Lecture 13 (slide < S. Levy) CS 100 34 // Count up <initialize a counter variable> while (<counter is less than a limit value>){ <do something> <increase 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 (slide < S. Levy) int counter = 1; while (counter <= 10){ <do something> counter = counter + 1; } 35 2/23/04 CS 100 - Lecture 13 36 (slide < S. Levy) 6 Lecture 13 2/23/04 23:21 Count-controlled Loops Count-controlled Loops // General form <initialize a counter variable> while (<test counter for termination condition>){ <do something> <change the value of counter> } // 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> } // Count down <initialize a counter variable> while (<counter is greater than a limit value>){ <do something> <decrease the value of counter> } 2/23/04 2/23/04 CS 100 - Lecture 13 37 2/23/04 (slide < S. Levy) (slide < S. Levy) Increment and Decrement Increment and Decrement int counter = 1; while (counter <= 10){ <do something> counter = counter + 1; } int counter = 1; while (counter <= 10){ <do something> counter++; } int counter = 10; while (counter > 0){ <do something> counter = counter - 1; } int counter = 10; while (counter > 0){ <do something> counter--; } CS 100 - Lecture 13 39 2/23/04 (slide < S. Levy) Designing Correct Loops Off-by-One Error int counter = 1; while (counter <= 10){ <do something> counter++; } – Plan how many iterations, then set the counter and the limit accordingly int counter = 1; while (counter < 10){ <do something> counter++; } • Check the logic of the termination condition • Update the loop control variable properly CS 100 - Lecture 13 (slide < S. Levy) CS 100 CS 100 - Lecture 13 (slide < S. Levy) • Initialize all variables properly 2/23/04 int counter = 10; while (counter > 0){ <do something> counter = counter - 1; } CS 100 - Lecture 13 41 2/23/04 38 40 // Executes 10 passes // Executes 9 passes CS 100 - Lecture 13 42 (slide < S. Levy) 7 Lecture 13 2/23/04 23:21 The for Loop Infinite Loop int counter = 1; while (counter <= 10){ <do something> counter = counter + 2; } int counter = 1; while (counter <= 10){ <do something> counter++; } // Executes 5 passes for (int counter = 1; counter <= 10; counter++) <do something> int counter = 1; while (counter != 10){ <do something> counter = counter + 2; } // Runs forever In general, avoid using != in loop termination conditions. 2/23/04 CS 100 - Lecture 13 43 2/23/04 CS 100 - Lecture 13 44 (slide < S. Levy) (slide < S. Levy) The for Loop Syntax and Semantics of the for Loop int counter = 10; while (counter > 0){ <do something> counter--; } for (<initializer>; <termination>; <update>) <statement> for (int counter = 10; counter > 0; counter--) <do something> false initializer termination true statement update 2/23/04 CS 100 - Lecture 13 45 2/23/04 CS 100 - Lecture 13 (slide < S. Levy) (slide < S. Levy) The Life Cycle of Programs: Waterfall Model Maintenance 46 Analysis -----------Verify • Programs might have a lifetime of 5-20 years • Requirements might change, and minor or major changes would be necessary Design -----------Verify Implementation ------------------Test Integration ------------Test Maintenance 2/23/04 CS 100 - Lecture 13 (slide < S. Levy) CS 100 47 2/23/04 CS 100 - Lecture 13 48 (slide < S. Levy) 8 Lecture 13 2/23/04 23:21 The Relative Costs of the Phases 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 (slide < S. Levy) CS 100 49 2/23/04 CS 100 - Lecture 13 50 (slide < S. Levy) 9