Chapter 8 Introduction to HighLevel Language Programming Invitation

  • Slides: 51
Download presentation
Chapter 8: Introduction to High-Level Language Programming Invitation to Computer Science, Java Version, Third

Chapter 8: Introduction to High-Level Language Programming Invitation to Computer Science, Java Version, Third Edition

Objectives In this chapter, you will learn about n Where do we stand? n

Objectives In this chapter, you will learn about n Where do we stand? n High-level languages n Introduction to Java n Virtual data storage n Statement types Invitation to Computer Science, Java Version, Third Edition 2

Objectives (continued) n Meeting expectations n Managing complexity n Object-oriented programming n Graphical programming

Objectives (continued) n Meeting expectations n Managing complexity n Object-oriented programming n Graphical programming n The big picture: Software engineering Invitation to Computer Science, Java Version, Third Edition 3

Where Do We Stand? n Early days of computing q Programmers were satisfied with

Where Do We Stand? n Early days of computing q Programmers were satisfied with assembly language n n Programs mostly written by very technically oriented people Later decades q Programmers demanded a more comfortable programming environment n Programs were now also written by “nontechie” people Invitation to Computer Science, Java Version, Third Edition 4

High-Level Languages n High-level programming languages q q n Called third-generation languages Created to

High-Level Languages n High-level programming languages q q n Called third-generation languages Created to overcome deficiencies of assembly language Expectations of a high-level language program q The programmer need not manage the details of the movement of data items within memory nor exactly where those items are stored Invitation to Computer Science, Java Version, Third Edition 5

High-level Languages (continued) n Expectations of a high-level language program (continued) q q q

High-level Languages (continued) n Expectations of a high-level language program (continued) q q q Programmer can take a macroscopic view of tasks; “primitive operations” can be larger Program will be portable Programming statements will be closer to standard English and use standard mathematical notation Invitation to Computer Science, Java Version, Third Edition 6

Introduction to Java: A Simple Java Program n Comments q n Class header q

Introduction to Java: A Simple Java Program n Comments q n Class header q n Announces that a class is about to be defined Class q n Give information to human readers of code A collection of methods Method q A section of code that performs a service Invitation to Computer Science, Java Version, Third Edition 7

Figure 8. 2 A Simple Java Program Invitation to Computer Science, Java Version, Third

Figure 8. 2 A Simple Java Program Invitation to Computer Science, Java Version, Third Edition 8

Running a Java Program n File containing the Java code q q q n

Running a Java Program n File containing the Java code q q q n Same name as the class File extension. java Example: Travel. Planner. java Running a Java program q Program compiled n q Example: File Travel. Planner. class created Translation to object code completed; program linked, loaded, and executed Invitation to Computer Science, Java Version, Third Edition 9

Virtual Data Storage n Identifiers q n Names in a programming language Keyword q

Virtual Data Storage n Identifiers q n Names in a programming language Keyword q Has a special meaning in Java is a case-sensitive, free-format language n Variable q A named location in memory q Must be declared before it can be used Invitation to Computer Science, Java Version, Third Edition 10

Figure 8. 4 Some of the Java Primitive Data Types Invitation to Computer Science,

Figure 8. 4 Some of the Java Primitive Data Types Invitation to Computer Science, Java Version, Third Edition 11

Virtual Data Storage (continued) n An array q Groups together a collection of memory

Virtual Data Storage (continued) n An array q Groups together a collection of memory locations, all storing data of the same type Figure 8. 5 A 12 -Element Array Hits Invitation to Computer Science, Java Version, Third Edition 12

Statement Types n Input/output statements q Input statement n q Collects a specific value

Statement Types n Input/output statements q Input statement n q Collects a specific value from the user for a variable within the program Output statement n Writes a message or the value of a program variable to the user’s screen or to a file Invitation to Computer Science, Java Version, Third Edition 13

Statement Types (continued) n Assignment statement q n Assigns a value to a program

Statement Types (continued) n Assignment statement q n Assigns a value to a program variable Control statement q Directs the flow of control n Can cause it to deviate from the usual sequential flow Invitation to Computer Science, Java Version, Third Edition 14

Input Statements n A prompt q n A message that tells the user what

Input Statements n A prompt q n A message that tells the user what kind of input the program wants Console class q q Not a standard Java class; written for this book Can be used to handle both the prompt and the retrieval of the value in one statement Invitation to Computer Science, Java Version, Third Edition 15

Input Statements (continued) n n Methods q read. Int q read. Double q read.

Input Statements (continued) n n Methods q read. Int q read. Double q read. Char Syntax variable 1 = Console. read. Int(prompt); variable 2 = Console. read. Double(prompt); variable 3 = Console. read. Char(prompt); Invitation to Computer Science, Java Version, Third Edition 16

Output Statements n Output to the screen System. out. println(string); n The string can

Output Statements n Output to the screen System. out. println(string); n The string can be q Empty System. out. println(); q Literal string System. out. println("Here's your answer. " ); q Composed of two or more items System. out. println("Give me" + 5); Invitation to Computer Science, Java Version, Third Edition 17

The Assignment Statement n General form variable = expression; n Expression is evaluated first;

The Assignment Statement n General form variable = expression; n Expression is evaluated first; the result is written into the memory location specified on the left Invitation to Computer Science, Java Version, Third Edition 18

The Assignment Statement (continued) n Examples B = 2; n Suppose that B is

The Assignment Statement (continued) n Examples B = 2; n Suppose that B is an integer variable A = B + C; n Suppose that A, B, and C are integer variables Letter = 'm'; n Suppose that Letter is a variable of type char Invitation to Computer Science, Java Version, Third Edition 19

Control Statements n Types of control mechanisms q Sequential n q Conditional n q

Control Statements n Types of control mechanisms q Sequential n q Conditional n q Instructions are executed in order The choice of which instructions to execute next depends on some condition Looping n A group of instructions may be executed many times Invitation to Computer Science, Java Version, Third Edition 20

Control Statements (continued) n Sequential is default mode of execution n Conditional flow of

Control Statements (continued) n Sequential is default mode of execution n Conditional flow of control q q Evaluation of a Boolean condition (also called a Boolean expression) The programming statement to execute next is based on the value of the Boolean condition (true or false) Invitation to Computer Science, Java Version, Third Edition 21

Control Statements (continued) n Conditional flow of control (continued) q if-else statement if (Boolean

Control Statements (continued) n Conditional flow of control (continued) q if-else statement if (Boolean condition) S 1; else S 2; q if variation of the if-else statement if (Boolean condition) S 1; Invitation to Computer Science, Java Version, Third Edition 22

Figure 8. 10 Conditional Flow of Control (If-Else) Invitation to Computer Science, Java Version,

Figure 8. 10 Conditional Flow of Control (If-Else) Invitation to Computer Science, Java Version, Third Edition 23

Figure 8. 11 If-Else with Empty Else Invitation to Computer Science, Java Version, Third

Figure 8. 11 If-Else with Empty Else Invitation to Computer Science, Java Version, Third Edition 24

Control Statements (continued) n Looping (iteration) q q The loop body may be executed

Control Statements (continued) n Looping (iteration) q q The loop body may be executed repeatedly based on the value of the Boolean condition while statement while (Boolean condition) S 1; Invitation to Computer Science, Java Version, Third Edition 25

Figure 8. 13 While Loop Invitation to Computer Science, Java Version, Third Edition 26

Figure 8. 13 While Loop Invitation to Computer Science, Java Version, Third Edition 26

Meeting Expectations n Java meets the four expectations for a high-level programming language n

Meeting Expectations n Java meets the four expectations for a high-level programming language n Expectations q The programmer need not manage the details of the movement of data items within memory nor pay any attention to where specifically they are stored Invitation to Computer Science, Java Version, Third Edition 27

Meeting Expectations (continued) n Expectations (continued) q q q The programmer can take a

Meeting Expectations (continued) n Expectations (continued) q q q The programmer can take a macroscopic view of tasks, thinking at a higher level of problem solving Programs written in high-level languages will be portable rather than machine-specific Programming statements in a high-level language n Will be closer to standard English n Will use standard mathematical notation Invitation to Computer Science, Java Version, Third Edition 28

Managing Complexity: Divide and Conquer n Divide and conquer q Divide the problem into

Managing Complexity: Divide and Conquer n Divide and conquer q Divide the problem into small pieces q In a computer program n Divide the code into modules or subprograms, each of which does some part of the overall task n Empower these modules to work together to solve the original problem Invitation to Computer Science, Java Version, Third Edition 29

Figure 8. 20 Structure Charts Invitation to Computer Science, Java Version, Third Edition 30

Figure 8. 20 Structure Charts Invitation to Computer Science, Java Version, Third Edition 30

Using Methods n Method q A module of code in Java q Named using

Using Methods n Method q A module of code in Java q Named using ordinary Java identifiers q By custom, name starts with a lowercase letter Invitation to Computer Science, Java Version, Third Edition 31

Using Methods (continued) n Two types of methods q void method n q nonvoid

Using Methods (continued) n Two types of methods q void method n q nonvoid method n n Does not pass any value back to the main method Returns a single new value back to the main method Overall form of a method invocation class-identifier. method-identifier(argument list) Invitation to Computer Science, Java Version, Third Edition 32

Writing Methods n General form of the method header scope-indicator return-indicator identifier(parameter list) n

Writing Methods n General form of the method header scope-indicator return-indicator identifier(parameter list) n Arguments in Java are passed by value n A variable declared within a method can be used only within that method n Return statement q Syntax return expression; Invitation to Computer Science, Java Version, Third Edition 33

Figure 8. 27 Some Java Terminology Invitation to Computer Science, Java Version, Third Edition

Figure 8. 27 Some Java Terminology Invitation to Computer Science, Java Version, Third Edition 34

Object-Oriented Programming: What Is It? n Object-oriented programming (OOP) q q n A program

Object-Oriented Programming: What Is It? n Object-oriented programming (OOP) q q n A program is a simulation of some part of the world that is the domain of interest Each object is an example drawn from a class of similar objects Key elements of OOP q Encapsulation n A class consists of its subtask modules and its properties, and both components are “encapsulated” with the class Invitation to Computer Science, Java Version, Third Edition 35

What Is It? (continued) n Key elements of OOP (continued) q Inheritance n q

What Is It? (continued) n Key elements of OOP (continued) q Inheritance n q Once a class A of objects is defined, a class B of objects can be defined as a “subclass” of A Polymorphism n One name, the name of the service to be performed, has several meanings, depending on the class of the object providing the service Invitation to Computer Science, Java Version, Third Edition 36

Java and OOP n Java is an object-oriented programming language n Static method q

Java and OOP n Java is an object-oriented programming language n Static method q Can be invoked by giving the class name, a dot, the method name, and a list of arguments n Objects: Instances of a class n Instance variables: Properties n Instance methods: Services Invitation to Computer Science, Java Version, Third Edition 37

Java and OOP (continued) n Syntax to request an object to invoke a method

Java and OOP (continued) n Syntax to request an object to invoke a method object-identifier. method-identifier(argument list) n Calling object q The object that invokes a method Invitation to Computer Science, Java Version, Third Edition 38

What Have We Gained? n Two major advantages of OOP q Software reuse q

What Have We Gained? n Two major advantages of OOP q Software reuse q A more natural “world view” Invitation to Computer Science, Java Version, Third Edition 39

Graphical Programming: Graphics Hardware n Bitmapped display q n Frame buffer q n The

Graphical Programming: Graphics Hardware n Bitmapped display q n Frame buffer q n The screen is made up of thousands of individual picture elements, or pixels, laid out in a twodimensional grid Memory that stores the actual screen image Terminal hardware displays the frame buffer value of every individual pixel on the screen Invitation to Computer Science, Java Version, Third Edition 40

Figure 8. 34 Pixel Numbering System in a Bitmapped Display Invitation to Computer Science,

Figure 8. 34 Pixel Numbering System in a Bitmapped Display Invitation to Computer Science, Java Version, Third Edition 41

Graphics Software n Graphics library q n Abstract Windowing Toolkit (AWT) q n Contains

Graphics Software n Graphics library q n Abstract Windowing Toolkit (AWT) q n Contains a collection of software routines that control the setting and clearing of pixels Contains routines that allow users to create powerful interfaces Swing components q Even more powerful GUI components than AWT Invitation to Computer Science, Java Version, Third Edition 42

Graphics Software (continued) n Graphics class q Contains drawing commands that allow you to

Graphics Software (continued) n Graphics class q Contains drawing commands that allow you to n Draw geometric shapes (lines, rectangles, ovals, polygons, and so on) n Set, change, and define colors n Fill in or shade objects n Create text in a range of fonts and sizes n Produce graphs and charts Invitation to Computer Science, Java Version, Third Edition 43

The Big Picture: Software Engineering n Software life cycle q q The overall sequence

The Big Picture: Software Engineering n Software life cycle q q The overall sequence of steps needed to complete a large-scale software project Implementation represents a relatively small part of the cycle Invitation to Computer Science, Java Version, Third Edition 44

Figure 8. 36 Steps in the Software Development Life Cycle Invitation to Computer Science,

Figure 8. 36 Steps in the Software Development Life Cycle Invitation to Computer Science, Java Version, Third Edition 45

Scaling Up n Programs written by students q n Real-world programs q n No

Scaling Up n Programs written by students q n Real-world programs q n No longer than a few hundred lines 2, 3, or 4 orders of magnitude larger Large-scale software development q Extensive planning and design needed q A team of programmers needed q Software engineering Invitation to Computer Science, Java Version, Third Edition 46

The Software Life Cycle n Each step in the software development life cycle q

The Software Life Cycle n Each step in the software development life cycle q Has a specific purpose and activities q Should result in a written document n The feasibility study n Problem specification n Program design Invitation to Computer Science, Java Version, Third Edition 47

The Software Life Cycle (continued) n Algorithm selection or development, and analysis n Coding

The Software Life Cycle (continued) n Algorithm selection or development, and analysis n Coding n Debugging n Testing, verification, and benchmarking n Documentation n Maintenance Invitation to Computer Science, Java Version, Third Edition 48

Modern Environments n Integrated Development Environment (IDE) speeds program development by providing q A

Modern Environments n Integrated Development Environment (IDE) speeds program development by providing q A text editor q A file manager q A compiler q A linker and loader q Tools for debugging Invitation to Computer Science, Java Version, Third Edition 49

Summary n In a high-level language, the programmer q q q n Need not

Summary n In a high-level language, the programmer q q q n Need not manage storage nor movement of data values in memory Can use more powerful program instructions that are more like natural language Can write a much more portable program Java is an object-oriented, high-level programming language Invitation to Computer Science, Java Version, Third Edition 50

Summary (continued) n In Java, an if-else statement can be used to create a

Summary (continued) n In Java, an if-else statement can be used to create a conditional flow of control n In Java, a while loop can be used for iteration n Software life cycle: Overall sequence of steps needed to complete a large-scale software project Invitation to Computer Science, Java Version, Third Edition 51