Lecture 13 Introduction to High-Level Programming (S&G, §§7.1–7.6)

Slides:



Advertisements
Similar presentations
Designing a Program & the Java Programming Language
Advertisements

Programing Concept Ken Youssefi/Ping HsuIntroduction to Engineering – E10 1 ENGR 10 Introduction to Engineering (Part A)
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
Introduction to Computers and Programming Lecture 7:
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Loops – While, Do, For Repetition Statements Introduction to Arrays
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
CS107 Introduction to Computer Science Java Basics.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
CSE 1301 J Lecture 2 Intro to Java Programming Richard Gesick.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Computer Science 101 Introduction to Programming.
High-Level Programming Languages: C++
CIS Computer Programming Logic
1. Reference  2  Algorithm :- Outline the essence of a computational procedure, step by step instructions.  Program :- an.
DAT602 Database Application Development Lecture 5 JAVA Review.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
CS107 Introduction to Computer Science Java Basics.
Invitation to Computer Science, Java Version, Second Edition.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Computer Science 101 Introduction to Programming.
Introduction to Computer Systems and the Java Programming Language.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
Computer Science 101 Introduction to Programming.
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CSCI 1226 FALL 2015 MIDTERM #1 REVIEWS.  Types of computers:  Personal computers  Embedded systems  Servers  Hardware:  I/O devices: mice, keyboards,
Introduction to Computers and Programming Lecture 7:
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Information and Computer Sciences University of Hawaii, Manoa
Fundamentals of Programming I Overview of Programming
Lecture 4b Repeating With Loops
The Machine Model Memory
REPETITION CONTROL STRUCTURE
Key Ideas from day 1 slides
GC211Data Structure Lecture2 Sara Alhajjam.
CS1001 Programming Fundamentals 3(3-0) Lecture 2
Primitive Data, Variables, Loops (Maybe)
Data types and variables
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
User input We’ve seen how to use the standard output buffer
Chapter 4 – Control Structures Part 1
Algorithm and Ambiguity
Computer Science 101 While Statement.
Java Programming: From Problem Analysis to Program Design, 4e
SELECTION STATEMENTS (1)
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Arrays, For loop While loop Do while loop
OPERATORS (2) CSC 111.
Outline Altering flow of control Boolean expressions
Introduction CSC 111.
3 Control Statements:.
CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Chapter 6: Repetition Statements
Algorithm and Ambiguity
Java Review Most of these slides are based on
Boolean Expressions to Make Comparisons
Chapter 4 - Control Structures: Part 1
Repetition Statements (Loops) - 2
Repetition Statements
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Presentation transcript:

Lecture 13 Introduction to High-Level Programming (S&G, §§7.1–7.6) 4/21/2019 5:17 PM Lecture 13 Introduction to High-Level Programming (S&G, §§7.1–7.6) 4/21/2019 CS 100 - Lecture 13 CS 100

From Algorithms to Programs Lecture 13 4/21/2019 5:17 PM From Algorithms to Programs Algorithms are essentially abstract things that can have Linguistic realizations (in pseudocode or programming languages) Hardware realizations (in digital circuitry) 4/21/2019 CS 100 - Lecture 13 (slide < S. Levy) CS 100

Lecture 13 4/21/2019 5:17 PM 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 4/21/2019 CS 100 - Lecture 13 (slide < S. Levy) CS 100

What Is a Programming Language? Lecture 13 4/21/2019 5:17 PM What Is a Programming Language? A bit like pseudocode, but with very strict syntax rules Examples: Basic, Pascal, C++, Java 4/21/2019 CS 100 - Lecture 13 (slide < S. Levy) CS 100

From Algorithms to Hardware Lecture 13 4/21/2019 5:17 PM From Algorithms to Hardware Algorithm Program Translate (by a human being) A real computer Translate (by another program) 4/21/2019 CS 100 - Lecture 13 (slide < S. Levy) CS 100

The Program Development Process (Data Flow) Lecture 13 4/21/2019 5:17 PM The Program Development Process (Data Flow) Algorithm Editor Program in programming language Compiler Program in machine’s language A real computer Input Output 4/21/2019 CS 100 - Lecture 13 (slide < S. Levy) CS 100

The Program Development Process (Control Flow) Lecture 13 4/21/2019 5:17 PM The Program Development Process (Control Flow) Edit Syntax errors Compile Run Input Output Runtime errors 4/21/2019 CS 100 - Lecture 13 (slide < S. Levy) CS 100

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 4/21/2019 CS 100 - Lecture 13 (slide < K. Lambert)

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 Compiler Java code JVM byte code User inputs Program outputs 4/21/2019 CS 100 - Lecture 13 (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 4/21/2019 CS 100 - Lecture 13 (slide < K. Lambert)

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; 4/21/2019 CS 100 - Lecture 13 (slide < R. Priebe)

Example Java Statements Lecture 13 4/21/2019 5:17 PM 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. 4/21/2019 CS 100 - Lecture 13 (slide < S. Levy) CS 100

Basic Data Types true, false boolean 'a', '9', '%' char "Java rules!" Lecture 13 4/21/2019 5:17 PM Basic Data Types true, false boolean 'a', '9', '%' char "Java rules!" String 3.14, 0.563333333 double 34, 0, 10000000 int Example Values Type Name 4/21/2019 CS 100 - Lecture 13 (slide < S. Levy) CS 100

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 If Name is equal to Ni, then Print Ti Set the value of Found to YES Else Increment i If (Found = NO) then Print “Sorry, but the name’s not in the directory” Stop In pseudo code, we use subscripts to create a list … … and to reference particular elements of it 4/21/2019 CS 100 - Lecture 13 (slide < R. Priebe)

Translating to Java Here are examples of creating arrays: int[ ] years = new int[10]; // a list of 10 integers double[ ] 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 4/21/2019 CS 100 - Lecture 13 (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"); 4/21/2019 CS 100 - Lecture 13 (slide < R. Priebe)

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"); 4/21/2019 CS 100 - Lecture 13 (slide < R. Priebe)

Example Problem Write a program that computes the area of a circle. Lecture 13 4/21/2019 5:17 PM Example Problem Write a program that computes the area of a circle. 4/21/2019 CS 100 - Lecture 13 (slide < S. Levy) CS 100

From Problem to Algorithm Lecture 13 4/21/2019 5:17 PM 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 4/21/2019 CS 100 - Lecture 13 (slide adapt. < S. Levy) CS 100

Pseudocode for Program Lecture 13 4/21/2019 5:17 PM Pseudocode for Program input radius area =   radius  radius output area 4/21/2019 CS 100 - Lecture 13 (slide adapt. < S. Levy) CS 100

(slide adapt. < S. Levy) Lecture 13 4/21/2019 5:17 PM Basic Computation radius = Console.readInt ("Radius?"); area = Math.PI * radius * radius; System.out.println ("Area = " + area); From pseudocode 4/21/2019 CS 100 - Lecture 13 (slide adapt. < S. Levy) CS 100

(slide adapt. < S. Levy) Lecture 13 4/21/2019 5:17 PM 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 4/21/2019 CS 100 - Lecture 13 (slide adapt. < S. Levy) CS 100

Digression: What Does It All Mean? Lecture 13 4/21/2019 5:17 PM 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 4/21/2019 CS 100 - Lecture 13 (slide < S. Levy) CS 100

Java Methods The data in the parentheses are called parameters. Lecture 13 4/21/2019 5:17 PM 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. 4/21/2019 CS 100 - Lecture 13 (slide < S. Levy) CS 100

Structure of Simple Program: The Main Method Lecture 13 4/21/2019 5:17 PM 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. 4/21/2019 CS 100 - Lecture 13 (slide < S. Levy) CS 100

Java Control Structures Lecture 13 4/21/2019 5:17 PM Java Control Structures Conditionals (if statements) 4/21/2019 CS 100 - Lecture 13 CS 100

Syntax of Compound Statements { <statement1> <statement2> … <statementn> } A compound statement is a sequence of zero or more statements. 4/21/2019 CS 100 - Lecture 13 (slide adapted < K. Lambert)

Syntax of Selection Statements if (<Boolean expression>) // One-way decision <statement> if (<Boolean expression>) // Two-way decision else 4/21/2019 CS 100 - Lecture 13 (slide < K. Lambert)

Semantics of Selection Statements if (<Boolean expression>) <statement> else false ? true statement false ? true statement statement 4/21/2019 CS 100 - Lecture 13 (slide < K. Lambert)

Java Control Structures Lecture 13 4/21/2019 5:17 PM Java Control Structures Iteration/Loops (while statements and for statements) 4/21/2019 CS 100 - Lecture 13 (slide < S. Levy) CS 100

Example: Summation Write a program that computes the sum of the Lecture 13 4/21/2019 5:17 PM 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 4/21/2019 CS 100 - Lecture 13 (slide < S. Levy) CS 100

Example: Summation Write a program that computes the sum of the Lecture 13 4/21/2019 5:17 PM 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); 4/21/2019 CS 100 - Lecture 13 (slide < S. Levy) CS 100

Syntax and Semantics of while Statements Lecture 13 4/21/2019 5:17 PM Syntax and Semantics of while Statements while (<Boolean expression>) <statement> { <statement 1> … <statement n> } false ? true statement 4/21/2019 CS 100 - Lecture 13 (slide < S. Levy) CS 100

Count-controlled Loops Lecture 13 4/21/2019 5:17 PM Count-controlled Loops // General form <initialize a counter variable> while (<test counter for termination condition>){ <do something> <change the value of counter> } 4/21/2019 CS 100 - Lecture 13 (slide < S. Levy) CS 100

Count-controlled Loops Lecture 13 4/21/2019 5:17 PM 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> } 4/21/2019 CS 100 - Lecture 13 (slide < S. Levy) CS 100

Count-controlled Loops Lecture 13 4/21/2019 5:17 PM 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; } 4/21/2019 CS 100 - Lecture 13 (slide < S. Levy) CS 100

Count-controlled Loops Lecture 13 4/21/2019 5:17 PM 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> } 4/21/2019 CS 100 - Lecture 13 (slide < S. Levy) CS 100

Count-controlled Loops Lecture 13 4/21/2019 5:17 PM 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; } 4/21/2019 CS 100 - Lecture 13 (slide < S. Levy) CS 100

Increment and Decrement Lecture 13 4/21/2019 5:17 PM 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; } 4/21/2019 CS 100 - Lecture 13 (slide < S. Levy) CS 100

Increment and Decrement Lecture 13 4/21/2019 5:17 PM Increment and Decrement int counter = 1; while (counter <= 10){ <do something> counter++; } int counter = 10; while (counter > 0){ <do something> counter--; } 4/21/2019 CS 100 - Lecture 13 (slide < S. Levy) CS 100

Designing Correct Loops Lecture 13 4/21/2019 5:17 PM 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 4/21/2019 CS 100 - Lecture 13 (slide < S. Levy) CS 100

Off-by-One Error int counter = 1; Lecture 13 4/21/2019 5:17 PM Off-by-One Error int counter = 1; while (counter <= 10){ // Executes 10 passes <do something> counter++; } int counter = 1; while (counter < 10){ // Executes 9 passes <do something> counter++; } 4/21/2019 CS 100 - Lecture 13 (slide < S. Levy) CS 100

Lecture 13 4/21/2019 5:17 PM Infinite Loop int 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. 4/21/2019 CS 100 - Lecture 13 (slide < S. Levy) CS 100

The for Loop int counter = 1; while (counter <= 10){ Lecture 13 4/21/2019 5:17 PM The for Loop int counter = 1; while (counter <= 10){ <do something> counter++; } for (int counter = 1; counter <= 10; counter++) <do something> 4/21/2019 CS 100 - Lecture 13 (slide < S. Levy) CS 100

The for Loop int counter = 10; while (counter > 0){ Lecture 13 4/21/2019 5:17 PM The for Loop int counter = 10; while (counter > 0){ <do something> counter--; } for (int counter = 10; counter > 0; counter--) <do something> 4/21/2019 CS 100 - Lecture 13 (slide < S. Levy) CS 100

Syntax and Semantics of the for Loop Lecture 13 4/21/2019 5:17 PM Syntax and Semantics of the for Loop for (<initializer>; <termination>; <update>) <statement> false termination initializer true statement update 4/21/2019 CS 100 - Lecture 13 (slide < S. Levy) CS 100

The Life Cycle of Programs: Waterfall Model Lecture 13 4/21/2019 5:17 PM The Life Cycle of Programs: Waterfall Model 4/21/2019 CS 100 - Lecture 13 (slide < S. Levy) CS 100

Maintenance Programs might have a lifetime of 5-20 years Lecture 13 4/21/2019 5:17 PM Maintenance Programs might have a lifetime of 5-20 years Requirements might change, and minor or major changes would be necessary 4/21/2019 CS 100 - Lecture 13 (slide < S. Levy) CS 100

The Cost of Correcting an Error Lecture 13 4/21/2019 5:17 PM The Cost of Correcting an Error 4/21/2019 CS 100 - Lecture 13 (slide < S. Levy) CS 100

The Relative Costs of the Phases Lecture 13 4/21/2019 5:17 PM The Relative Costs of the Phases 4/21/2019 CS 100 - Lecture 13 (slide < S. Levy) CS 100