Introduction to Programming
Total Page:16
File Type:pdf, Size:1020Kb
1 2 3 4 Computers & Data Instructions Flow Objects 5 7 8 5 Input/Output6 & Algorithms & Libraries & Pointers & Arrays Events Data Structures Languages Introduction to Programming UCSD Extension Philip J. Mercurio [email protected] 9th Edition Copyright © 2002 Philip J. Mercurio, All Rights Reserved. The purchaser of this document is hereby granted permission to print one copy of this document for their own use. Ownership of this document may not be transferred. Introduction to Programming Table of Contents Slide # Title Slide # Title 1-1 Introduction to Programming 1-49 Floating Point Numbers 1-2 Today’s Session 1-50 Floats and Doubles 1-3 Course Overview: Readings 1-51 Precision 1-4 When was the last time you gave 1-52 Real Numbers someone directions? 1-53 Text 1-5 You're Already A Programmer 1-54 ASCII Table 1-6 Languages 1-55 Latin-1 1-7 Programming a Computer 1-56 Unicode 1-8 Why C++? 1-57 Strings 1-9 Goals of this course 1-58 Numbers and Strings 1-10 What You'll Learn 1-59 Addresses 1-11 After taking this course, you can: 1-60 Representing Objects in Memory 1-12 Computers Are Stupid 1-61 Groups and Lists 1-13 Why Do Computers Appear Smart? 1-63 So Far 1-14 Fundamental Rule of Programming 1-15 Hardware & Software 2-1 Introduction to Programming 1-16 Computer Hardware 2-2 Last Session 1-17 The Processor 2-3 Today’s Session 1-18 Memory 2-4 The Processor (A Simplified Model) 1-19 What Does Memory Look Like? 2-5 Processor Registers 1-20 The Processor and Memory 2-6 Instructions 1-21 The Address Space 2-7 Processor's Environment 1-22 Devices 2-8 Processor's Environment 1-23 Memory Mapping: The Great 2-9 Load Processor Simplification 2-10 Fetch Instruction 1-24 Our Model of a Computer 2-11 Increment PC 1-25 Memory & Data 2-12 Instruction Set 1-26 Data Types 2-13 Representing Instructions 1-27 True/False (Boolean) Values 2-14 CLEAR ACCUM 1-28 Numbers 2-15 CLEAR ADDR 1-29 Bases 2-16 COPY ACCUM A0 1-30 Binary 2-17 Instructions and Data 1-31 Hexadecimal 2-18 LOAD ADDR 1-32 Hex 2-19 Data Transfer 1-33 Handy Hex Table 2-20 STORE 1-34 Unsigned Integers 2-21 FETCH 1-35 8-bit Unsigned Integer 2-22 Programs 1-36 More 8-bit Unsigned Integers 2-23 Running a Program 1-37 The Number Circle 2-24 Arithmetic 1-38 Bigger Numbers 2-25 ADD 1 1-39 8-bit Signed Integers 2-26 ADD INT 1-40 The Signed Number Circle 2-27 The Complete Program 1-41 ...Becomes the Number Line 2-28 CLEAR ACCUM 1-42 Correct 8-bit Signed Integers 2-29 CLEAR ADDR 1-43 The Correct Signed Number Circle 2-30 COPY ACCUM A0 1-44 ...Becomes the Correct Number Line 2-31 LOAD ADDR 1-45 Bigger Signed Numbers 2-32 STORE 1-46 Rational Numbers 2-33 STORE 1-47 8-bit Fixed Point 2-34 FETCH 1-48 Too Fixed 2-35 FETCH © 2002 Philip J. Mercurio i Introduction to Programming Table of Contents 2-36 ADD 1 3-11 C++ Routines 2-37 ADD INT 3-12 main() 2-38 LOAD ADDR 3-13 Program 3-1 2-39 STORE 3-14 Automatic Variables 2-40 STORE 3-15 The Stack 2-41 Languages 3-16 PUSH 2-42 What is a Computer Language? 3-17 POP 2-43 Assembly Language 3-19 Hypotenuses 2-44 What is Meaning? 3-20 Stack Animation 2-45 Low-Level and High-Level 3-58 The Routine's the Thing 2-46 Compilers and Interpreters 3-59 Arguments 2-47 Many Tongues 3-60 Arguments Animation 2-48 C++ 3-63 Return Values 2-49 Our C++ 3-64 Routines in Expressions 2-50 The Mechanics 3-65 Functions Animation 2-51 Code Development Cycles 3-77 Defining Routines 2-52 C++ Words 3-78 Declaring Routines 2-53 Reserved Words & Your Words 3-79 Declaring vs. Defining 2-55 Numbers 3-81 The Void 2-56 Constants 3-82 Namespaces 2-57 Text Constants 3-83 Namespace Example 2-58 Statements 3-84 Scope 2-59 Operations and Declarations 3-85 Scope Example 2-60 Back to Memory 3-86 Still Linear 2-61 Variables 3-87 BRZ A0 (Before) 2-62 Variable Declaration 3-88 BRZ A0 (After) 2-63 Variable Declaration Step 1 3-89 BRZ A0 (Before) 2-64 Variable Declaration Step 2 3-90 BRZ A0 (After) 2-65 Variable Declaration Step 3 3-91 Branching 2-66 C++ Data Types 3-92 The if Statement 2-68 Assignment Statement 3-93 What Is Truth? 2-69 Expressions 3-94 Relational Operators 2-70 Expression Examples 3-95 if Example 2-71 Expression Evaluation 3-96 absoluteValue Flowchart 2-72 Assignment Examples 3-97 Testing a Boolean 2-73 More Assignment Examples 3-98 Boolean Flowchart 2-74 Program Animation 3-99 Comparison Function 2-81 Printing an Expression 3-100 compare() Flowchart 2-82 The Whole Program 3-101 compare() Version 1 2-83 Flow 3-102 compare() Version 2 3-103 Complicated if's 3-1 Introduction to Programming 3-104 Logical Expressions 3-2 Last Session 3-105 ! Truth Table 3-3 Today’s Session 3-106 && Truth Table 3-4 Flow 3-107 || Truth Table 3-5 JMP A0 (Before) 3-108 isLowerCase() 3-6 JMP A0 (After) 3-109 matchedPair() 3-7 Jump! 3-110 Loops 3-8 Flowcharts 3-111 while Statement 3-9 Getting Back 3-112 do-while Statement 3-10 Routines and Subroutines 3-113 The operator-equals Shorthand © 2002 Philip J. Mercurio ii Introduction to Programming Table of Contents 3-114 The Increment and Decrement 4-59 Notes on Program 4-5 Operators 4-60 More Enhancements 3-115 power() Function 4-61 Rounding Numbers 3-116 Variable Table for power() 4-62 Program 4-6 3-117 power() Version 2 4-71 Program 4-6 Output 3-118 Streams 4-72 Overloading 3-119 Using Streams 4-74 Dealing With Complexity 3-120 cin and cout 4-75 Inheritance 3-121 Program 3-2 4-76 Dogs and Cats 3-123 Program 3-3 4-78 Animal Hierarchy 3-125 So Far 4-79 Program 4-7 4-89 Program 4-7 Output 4-1 Introduction to Programming 4-90 Inheritance Trees 4-2 Recap 4-91 Great Ancestors 4-3 Object-Oriented Programming 4-92 is-a and has-a 4-4 Models 4-93 So Far 4-5 Primitive Models 4-6 Building Models with Models 5-1 Introduction to Programming 4-7 Classes and Objects 5-2 Recap 4-9 Classes and Namespaces 5-3 Indirection 4-10 Other People's Objects 5-5 Example of Indirection 4-11 Streams 5-10 Why Indirection? 4-12 Encapsulation 5-11 Sharing Information 4-13 Our Virtual Dogs 5-12 Filling In Later 4-14 Where It’s At 5-13 What is a Variable? 4-15 Declaring the Dog class 5-14 Variables 4-16 Creating an Object 5-15 Pointers vs. Addresses 4-17 Playground 5-16 Declaring Pointers 4-18 Attributes (Instance Variables) 5-17 Pointer Data Types 4-19 Program 4-1 5-18 Pointer Operators 4-21 Weak Encapsulation 5-19 & The Address Operator 4-22 Methods 5-20 * The Indirection Operator 4-23 Methods: What’s In Scope 5-21 Duality 4-24 Code Files 5-22 Program 5-1 4-25 Virtual Dog Methods 5-23 Program 5-1 Animation 4-26 Comments 5-30 Pointers and Arguments 4-27 Program 4-2 5-31 Squaring a Complex Number 4-31 Notes on Program 4-2 5-32 Program 5-2 4-32 Controlling Attribute Values 5-34 Pointers and Objects 4-33 Constructors 5-36 Pointers and Objects Example 4-35 Fence() method 5-37 Managing Memory 4-36 Program 4-3 5-38 new 4-41 Program 4-3 Output 5-39 delete 4-42 True Encapsulation 5-40 Dynamic Memory 4-43 Program 4-4 5-42 Arrays 4-48 Program 4-4 Output 5-44 Using Arrays 4-49 Notes on Program 4-4 5-45 Initializing Arrays 4-50 Interface 5-46 Program 5-3 4-51 Extending an Interface 5-47 Notes on Program 5-3 4-52 Program 4-5 5-48 The Dog Kennel 4-58 Program 4-5 Output 5-49 Declaring a Kennel © 2002 Philip J. Mercurio iii Introduction to Programming Table of Contents 5-50 Kennel Constructor 6-53 Color Table Animation 5-51 Kennel Destructor 6-54 Graphics Libraries 5-52 Arrays and Pointers 6-55 Display 5-54 Walking the Dogs 6-57 Drawing 5-55 Strings 6-59 Painting 5-56 String Constants 6-61 Graphical Input 5-57 String Variables 6-62 Primitive Graphics 5-59 Lines of Text 6-63 UI Toolkits 5-60 Pointers and Strings 6-64 UI Toolkits (Windows) 5-61 Copying Strings 6-65 UI Toolkits (Macintosh) 5-62 stringCopy() 6-66 UI Toolkits (Motif) 5-66 stringCopy() Animation 6-67 GUI Builders 5-79 Built-in String Routines 6-68 Many Toolkits 5-80 String Routines 6-69 Look & Feel vs. API 5-84 So Far 6-71 Separating Look & Feel from API 6-72 GUI Example 6-1 Introduction to Programming 6-74 1-D vs.