Invitation to Computer Science 6 th Edition Chapter

  • Slides: 72
Download presentation
Invitation to Computer Science 6 th. Edition Chapter Java Programming in Java

Invitation to Computer Science 6 th. Edition Chapter Java Programming in Java

Objectives In this chapter, you will learn about: • Creating and running a simple

Objectives In this chapter, you will learn about: • Creating and running a simple Java program • Virtual data storage • Statement types • Another example Invitation to Computer Science, 6 th. Edition 2

Objectives (continued) • Managing complexity • Object-oriented programming • Graphical programming Invitation to Computer

Objectives (continued) • Managing complexity • Object-oriented programming • Graphical programming Invitation to Computer Science, 6 th. Edition 3

Introduction to Java • Figure 1 shows a simple but complete Java program •

Introduction to Java • Figure 1 shows a simple but complete Java program • Figure 2 shows the same program with a number in front of each line • Comments – Anything appearing on a line after the double slash symbol (//) is ignored by the compiler • Prologue comment – Introductory comment that comes first Invitation to Computer Science, 6 th. Edition 4

Figure 1 A Simple Java Program Invitation to Computer Science, 6 th. Edition 5

Figure 1 A Simple Java Program Invitation to Computer Science, 6 th. Edition 5

Figure 2 The Program of Figure 1 (line numbers added for reference) Invitation to

Figure 2 The Program of Figure 1 (line numbers added for reference) Invitation to Computer Science, 6 th. Edition 6

Introduction to Java (continued) • Class header – Announces that a class is about

Introduction to Java (continued) • Class header – Announces that a class is about to be defined • Class – Collection of sections of code called methods • Free-format language – Does not matter where things are placed on a line Invitation to Computer Science, 6 th. Edition 7

Creating and Running a Java Program • Three-step process – First step: type the

Creating and Running a Java Program • Three-step process – First step: type the program into a text editor – Second step: the program in the. java file must be compiled using a Java compiler for your computer – Third step: operates on the. class file; it finishes the translation to object code and links, loads, and executes your program • Integrated Development Environment – Lets the programmer perform a number of tasks within the shell of a single application program Invitation to Computer Science, 6 th. Edition 8

Virtual Data Storage • Assembly language – Does not require us to give the

Virtual Data Storage • Assembly language – Does not require us to give the actual memory address of the storage location to be used for each item • Identifiers – Names in a programming language – Cannot be one of the few keywords • Java is a case-sensitive language – Uppercase letters are distinguished from lowercase letters Invitation to Computer Science, 6 th. Edition 9

Virtual Data Storage (continued) • Constants – Quantities that are fixed throughout the duration

Virtual Data Storage (continued) • Constants – Quantities that are fixed throughout the duration of the program – Values are known ahead of time • Variables – Values that are not known ahead of time but must be obtained from the computer user • Variable declaration – Consists of a data type followed by a list of one or more identifiers of that type Invitation to Computer Science, 6 th. Edition 10

Figure 3 Some of the Java Primitive Data Types Invitation to Computer Science, 6

Figure 3 Some of the Java Primitive Data Types Invitation to Computer Science, 6 th. Edition 11

Virtual Data Storage (continued) • Array – Groups together a collection of memory locations,

Virtual Data Storage (continued) • Array – Groups together a collection of memory locations, all storing data of the same type – Allows us to talk about an entire table of values or the individual elements making up that table Invitation to Computer Science, 6 th. Edition 12

Figure 4 A 12 -Element Array hits Invitation to Computer Science, 6 th. Edition

Figure 4 A 12 -Element Array hits Invitation to Computer Science, 6 th. Edition 13

Statement Types • Input statement – Collects a value from the user for a

Statement Types • Input statement – Collects a value from the user for a variable within the program • Output statement – Writes a message or the value of a program variable to the user’s screen • Assignment statement – Assigns a value to a program variable Invitation to Computer Science, 6 th. Edition 14

Statement Types (continued) • Control statements – Affect the order in which instructions are

Statement Types (continued) • Control statements – Affect the order in which instructions are executed • Flow of control in the program – The path through the program that is traced by following the currently executing statement Invitation to Computer Science, 6 th. Edition 15

Input/Output Statements • Scanner class – Provides a convenient way to collect user data

Input/Output Statements • Scanner class – Provides a convenient way to collect user data entered at the keyboard – Found in the Java “utility” library – Represents a new data type • Literal strings and variables – Can be concatenated together in all sorts of combinations Invitation to Computer Science, 6 th. Edition 16

The Assignment Statement • Pseudocode operation Set the value of “variable” to “arithmetic expression”

The Assignment Statement • Pseudocode operation Set the value of “variable” to “arithmetic expression” – Java equivalent variable = expression; • Basic arithmetic operations + * / Addition Subtraction Multiplication Division Invitation to Computer Science, 6 th. Edition 17

Control Statements • Control mechanisms – Sequential: instructions are executed in order – Conditional:

Control Statements • Control mechanisms – Sequential: instructions are executed in order – Conditional: which instruction executes next depends on some condition – Looping: a group of instructions may be executed many times • Boolean condition – Can be either true or false – Often involves comparing the values of two expressions and determining whether they are equal Invitation to Computer Science, 6 th. Edition 18

Figure 5 Sequential Flow of Control Invitation to Computer Science, 6 th. Edition 19

Figure 5 Sequential Flow of Control Invitation to Computer Science, 6 th. Edition 19

Figure 6 Java Comparison Operators Invitation to Computer Science, 6 th. Edition 20

Figure 6 Java Comparison Operators Invitation to Computer Science, 6 th. Edition 20

Figure 7 Java Boolean Operators Invitation to Computer Science, 6 th. Edition 21

Figure 7 Java Boolean Operators Invitation to Computer Science, 6 th. Edition 21

Figure 8 Conditional Flow of Control (if-else) Invitation to Computer Science, 6 th. Edition

Figure 8 Conditional Flow of Control (if-else) Invitation to Computer Science, 6 th. Edition 22

Control Statements (continued) • Compound statement – Can be used anywhere a single statement

Control Statements (continued) • Compound statement – Can be used anywhere a single statement is allowed – Example { System. out. println(“This is the first statement. ”); System. out. println(“This is the second statement. ”); System. out. println(“This is the third statement. ”); } Invitation to Computer Science, 6 th. Edition 23

Figure 9 If-Else with Empty Else Invitation to Computer Science, 6 th. Edition 24

Figure 9 If-Else with Empty Else Invitation to Computer Science, 6 th. Edition 24

Figure 10 The Travel. Planner Program with a Conditional Statement Invitation to Computer Science,

Figure 10 The Travel. Planner Program with a Conditional Statement Invitation to Computer Science, 6 th. Edition 25

Figure 10 The Travel. Planner Program with a Conditional Statement (continued) Invitation to Computer

Figure 10 The Travel. Planner Program with a Conditional Statement (continued) Invitation to Computer Science, 6 th. Edition 26

Figure 11 While Loop Invitation to Computer Science, 6 th. Edition 27

Figure 11 While Loop Invitation to Computer Science, 6 th. Edition 27

Control Statements (continued) • Sentinel value – One extra integer that is not part

Control Statements (continued) • Sentinel value – One extra integer that is not part of the legitimate data but is instead a signal that there are no more data • Infinite loop – The condition, once true, would remain true forever, and the loop body would be endlessly executed Invitation to Computer Science, 6 th. Edition 28

Figure 12 The Travel. Planner Program with Looping Invitation to Computer Science, 6 th.

Figure 12 The Travel. Planner Program with Looping Invitation to Computer Science, 6 th. Edition 29

Figure 12 The Travel. Planner Program with Looping (continued) Invitation to Computer Science, 6

Figure 12 The Travel. Planner Program with Looping (continued) Invitation to Computer Science, 6 th. Edition 30

Another Example • Example – Write a program to assist Sports. World, a company

Another Example • Example – Write a program to assist Sports. World, a company that installs circular swimming pools • To estimate costs for swimming pool covers or fencing – Sports. World needs to know the area or circumference of a pool, given its radius Invitation to Computer Science, 6 th. Edition 31

Figure 13 A Pseudocode Version of the Sports. World Program Invitation to Computer Science,

Figure 13 A Pseudocode Version of the Sports. World Program Invitation to Computer Science, 6 th. Edition 32

Figure 14 The Sports. World Program Invitation to Computer Science, 6 th. Edition 33

Figure 14 The Sports. World Program Invitation to Computer Science, 6 th. Edition 33

Figure 14 The Sports. World Program (continued) Invitation to Computer Science, 6 th. Edition

Figure 14 The Sports. World Program (continued) Invitation to Computer Science, 6 th. Edition 34

Figure 15 A Sample Session Using the Program of Figure 14 Invitation to Computer

Figure 15 A Sample Session Using the Program of Figure 14 Invitation to Computer Science, 6 th. Edition 35

Figure 15 A Sample Session Using the Program of Figure 14 (continued) Invitation to

Figure 15 A Sample Session Using the Program of Figure 14 (continued) Invitation to Computer Science, 6 th. Edition 36

Managing Complexity • Divide and conquer – A problem-solving approach and not just a

Managing Complexity • Divide and conquer – A problem-solving approach and not just a computer programming technique • Figure 16(a) – An example of a structure chart or structure diagram Invitation to Computer Science, 6 th. Edition 37

Figure 16 Structure Charts Invitation to Computer Science, 6 th. Edition 38

Figure 16 Structure Charts Invitation to Computer Science, 6 th. Edition 38

Using Methods • Methods – Modules of code – Named using ordinary Java identifiers,

Using Methods • Methods – Modules of code – Named using ordinary Java identifiers, customarily starting with a lowercase letter • Void method – Carries out some task but does not pass any new values back to the main method • Nonvoid method – Returns a single new value back to the main method Invitation to Computer Science, 6 th. Edition 39

Figure 17 Methods in the Circle Class Invitation to Computer Science, 6 th. Edition

Figure 17 Methods in the Circle Class Invitation to Computer Science, 6 th. Edition 40

Figure 18 Pseudocode for the Sports. World Main Method Using the Circle Class Invitation

Figure 18 Pseudocode for the Sports. World Main Method Using the Circle Class Invitation to Computer Science, 6 th. Edition 41

Figure 19 A Modularized Version of the Sports. World Program Invitation to Computer Science,

Figure 19 A Modularized Version of the Sports. World Program Invitation to Computer Science, 6 th. Edition 42

Figure 19 A Modularized Version of the Sports. World Program (continued) Invitation to Computer

Figure 19 A Modularized Version of the Sports. World Program (continued) Invitation to Computer Science, 6 th. Edition 43

Writing Methods • Method header has the general form scope-indicator return-indicator identifier(parameter list) •

Writing Methods • Method header has the general form scope-indicator return-indicator identifier(parameter list) • Arguments passed by value – Method can use the argument value but cannot permanently change it • Return statement – Returns a single value to the main method – Syntax: return expression; Invitation to Computer Science, 6 th. Edition 44

Figure 20 The Outline for a Java Method Invitation to Computer Science, 6 th.

Figure 20 The Outline for a Java Method Invitation to Computer Science, 6 th. Edition 45

Figure 21 The do. Circumference Method Invitation to Computer Science, 6 th. Edition 46

Figure 21 The do. Circumference Method Invitation to Computer Science, 6 th. Edition 46

Figure 22 The Circle Class in a Modularized Version of the Sports. World Program

Figure 22 The Circle Class in a Modularized Version of the Sports. World Program Invitation to Computer Science, 6 th. Edition 47

Writing Methods (continued) • Java program – Always begins execution with the main method

Writing Methods (continued) • Java program – Always begins execution with the main method • Modularizing a program is useful for: – – – Planning Coding Testing Modifying Reading Invitation to Computer Science, 6 th. Edition 48

Figure 23 Some Java Terminology Invitation to Computer Science, 6 th. Edition 49

Figure 23 Some Java Terminology Invitation to Computer Science, 6 th. Edition 49

Object-Oriented Programming • A program is considered a simulation of some part of the

Object-Oriented Programming • A program is considered a simulation of some part of the world that is the domain of interest – “Objects” populate this domain • When an object-oriented program is executed – The program generates requests for services that go to the various objects • Terms associated with object-oriented programming – Encapsulation – Inheritance – Polymorphism Invitation to Computer Science, 6 th. Edition 50

Figure 24 Three Key Elements of OOP Invitation to Computer Science, 6 th. Edition

Figure 24 Three Key Elements of OOP Invitation to Computer Science, 6 th. Edition 51

Java and OOP • Static method – One that doesn’t need to be invoked

Java and OOP • Static method – One that doesn’t need to be invoked by an object of that class • Objects – Instances of classes • Services – Instance methods • Calling object – Object that invokes a method Invitation to Computer Science, 6 th. Edition 52

Figure 25 The New Circle Class Invitation to Computer Science, 6 th. Edition 53

Figure 25 The New Circle Class Invitation to Computer Science, 6 th. Edition 53

Figure 26 The New Sports. World Class Invitation to Computer Science, 6 th. Edition

Figure 26 The New Sports. World Class Invitation to Computer Science, 6 th. Edition 54

Figure 26 The New Sports. World Class (continued) Invitation to Computer Science, 6 th.

Figure 26 The New Sports. World Class (continued) Invitation to Computer Science, 6 th. Edition 55

One More Example • In Figure 27 – The Circle object has a radius

One More Example • In Figure 27 – The Circle object has a radius property – The Rectangle object has a width property and a height property – The Square object has a side property – The Square 2 object doesn’t have any properties or any way to compute its area Invitation to Computer Science, 6 th. Edition 56

Figure 27 A Java Program with Polymorphism and Inheritance Invitation to Computer Science, 6

Figure 27 A Java Program with Polymorphism and Inheritance Invitation to Computer Science, 6 th. Edition 57

Figure 27 A Java Program with Polymorphism and Inheritance (continued) Invitation to Computer Science,

Figure 27 A Java Program with Polymorphism and Inheritance (continued) Invitation to Computer Science, 6 th. Edition 58

Figure 27 A Java Program with Polymorphism and Inheritance (continued) Invitation to Computer Science,

Figure 27 A Java Program with Polymorphism and Inheritance (continued) Invitation to Computer Science, 6 th. Edition 59

Figure 27 A Java Program with Polymorphism and Inheritance (continued) Figure 29 Invitation to

Figure 27 A Java Program with Polymorphism and Inheritance (continued) Figure 29 Invitation to Computer Science, 6 th. Edition 60

One More Example (continued) • Square – Separate class with a side property and

One More Example (continued) • Square – Separate class with a side property and a do. Area function • Square 2 class – Recognizes the fact that squares are special kinds of rectangles – Subclass of the Rectangle class – Inherits the width and height properties from the “parent” Rectangle class Invitation to Computer Science, 6 th. Edition 61

Figure 28 A Hierarchy of Geometric Classes Invitation to Computer Science, 6 th. Edition

Figure 28 A Hierarchy of Geometric Classes Invitation to Computer Science, 6 th. Edition 62

What Have We Gained? • Reasons why OOP is a popular way to program

What Have We Gained? • Reasons why OOP is a popular way to program – Software reuse – A more natural “worldview” • Software reuse – Useful class that has been implemented and tested becomes a component available for use in future software development Invitation to Computer Science, 6 th. Edition 63

A More “Natural” Worldview • Object-oriented programming – Recognizes that in the “real world,

A More “Natural” Worldview • Object-oriented programming – Recognizes that in the “real world, ” tasks are done by entities (objects) – Allows the programmer to come closer to modeling or simulating the world as we see it • Object-oriented program design – Begins by identifying objects that are important in the domain of the program Invitation to Computer Science, 6 th. Edition 64

Graphical Programming • Graphics – Make it easier to manage tasks of the operating

Graphical Programming • Graphics – Make it easier to manage tasks of the operating system – Can help us visualize and make sense of massive amounts of output produced by programs that model complex physical, social, and mathematical systems Invitation to Computer Science, 6 th. Edition 65

Figure 29 An Example of the Use of Graphics to Simplify Machine Operation Invitation

Figure 29 An Example of the Use of Graphics to Simplify Machine Operation Invitation to Computer Science, 6 th. Edition 66

Graphics Primitives • Bitmapped display – Screen is made up of thousands of individual

Graphics Primitives • Bitmapped display – Screen is made up of thousands of individual picture elements, or pixels, laid out in a two-dimensional grid • High-resolution terminals – Terminals with a high density of pixels • Frame buffer – Memory that stores the actual screen image Invitation to Computer Science, 6 th. Edition 67

Figure 30 Pixel-Numbering System in a Bitmapped Display Invitation to Computer Science, 6 th.

Figure 30 Pixel-Numbering System in a Bitmapped Display Invitation to Computer Science, 6 th. Edition 68

Figure 31 Display of Information on the Terminal Invitation to Computer Science, 6 th.

Figure 31 Display of Information on the Terminal Invitation to Computer Science, 6 th. Edition 69

Graphics Software • Graphics library – Collection of software modules • Abstract Windowing Toolkit

Graphics Software • Graphics library – Collection of software modules • Abstract Windowing Toolkit – Contains dozens of methods that allow users to create powerful interfaces Invitation to Computer Science, 6 th. Edition 70

Summary • In a high-level language, the programmer: – Need not manage the storage

Summary • In a high-level language, the programmer: – Need not manage the storage or movement of data values in memory – Can think about the problem at a higher level – Can use program instructions that are both more powerful and more natural language – Can write a program that is much more portable among various hardware platforms Invitation to Computer Science, 6 th. Edition 71

Summary (continued) • Modularization – Allows the program to be more cleanly structured •

Summary (continued) • Modularization – Allows the program to be more cleanly structured • Object-oriented programming – Allows a more intuitive view of the problem solution – Provides the possibility for reuse of helpful classes Invitation to Computer Science, 6 th. Edition 72