Question? Leave a message!




Object Oriented Programming Lecture Notes

object oriented programming c++ lecture notes and lecture notes in c++ | pdf free download
ShawnPacinocal Profile Pic
ShawnPacinocal,United States,Researcher
Published Date:09-07-2017
Website URL
Comment
1005ICT Object Oriented Programming Lecture Notes School of Information and Communication Technology Griffith University Semester 2, 2015 12 CONTENTS Contents Preface 12 1 Introduction 13 1.1 7005ICT Programming Principles 2 . . . . . . . . . . . . . . . . . . . . . 13 1.2 Take notes? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.2.1 Take notes today . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.3 Course Convenors & Lecturers . . . . . . . . . . . . . . . . . . . . . . . . 13 1.4 Course web site . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.5 What the course is about . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 1.6 The choice of programming languages . . . . . . . . . . . . . . . . . . . . 14 1.7 Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 1.8 Classes and assessment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 1.8.1 Lectures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 1.8.2 Workshops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 1.8.3 Labs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 1.8.4 Common times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 1.8.5 Final project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 1.8.6 Attendance summary . . . . . . . . . . . . . . . . . . . . . . . . . 15 1.8.7 Assessment summary . . . . . . . . . . . . . . . . . . . . . . . . . 15 1.8.8 Minimum requirements for labs and quizzes . . . . . . . . . . . . 15 1.8.9 Supplementary exam . . . . . . . . . . . . . . . . . . . . . . . . . 16 1.9 Lecture notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 1.10 Griffith Sciences Laboratory Induction . . . . . . . . . . . . . . . . . . . . 16 1.11 Peer Assisted Study Sessions . . . . . . . . . . . . . . . . . . . . . . . . . 16 1.12 Revision . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 1.13 Section summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 1.14 End of section feedback questions . . . . . . . . . . . . . . . . . . . . . . 17 1.15 Things to do . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 2 Kick-start Java 19 2.1 Hello, World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.1.1 System.out.println() . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.1.2 The class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.1.3 The main method . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 2.1.4 Compiling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 2.1.5 Running . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 2.2 Hello, You . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 2.3 Add . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 2.4 Section summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 2.5 End of section feedback questions . . . . . . . . . . . . . . . . . . . . . . 25 2.6 Self-practice exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26CONTENTS 3 3 The Software Development Process 27 3.1 The waterfall model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 3.2 Other models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 3.3 Human communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 3.4 Use the right tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 3.5 Section summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 3.6 End of section feedback questions . . . . . . . . . . . . . . . . . . . . . . 29 4 Programming In the Large 30 4.1 Small To large programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 4.2 The standard Java API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 4.2.1 Where to find the Java API documentation . . . . . . . . . . . . . 31 4.3 Small To large programming features . . . . . . . . . . . . . . . . . . . . 33 4.3.1 Subroutines to methods . . . . . . . . . . . . . . . . . . . . . . . . 33 4.3.2 Modules to classes . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 4.3.3 Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 4.4 Java package declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 4.5 Java import declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 4.5.1 When you need import declarations . . . . . . . . . . . . . . . . . 36 4.5.2 When you don’t need import declarations . . . . . . . . . . . . . 36 4.5.3 Avoid collisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 4.5.4 Example with imports . . . . . . . . . . . . . . . . . . . . . . . . . 37 4.6 Section summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 4.7 End of section feedback questions . . . . . . . . . . . . . . . . . . . . . . 38 5 Case Study: Class java.lang.Math 39 5.1 Package java.lang . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 5.2 Finding a class in the API . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 5.3 Reading class Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 5.4 The design of class Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 5.5 Example program using class Math . . . . . . . . . . . . . . . . . . . . . . 46 5.5.1 Version 1 – not reusable at all . . . . . . . . . . . . . . . . . . . . 46 5.5.2 Version 2 – with a function . . . . . . . . . . . . . . . . . . . . . . 47 5.5.3 Version 3 – with a function in its own class . . . . . . . . . . . . . 48 5.6 Section summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 5.7 End of section feedback questions . . . . . . . . . . . . . . . . . . . . . . 49 6 Basic Java Data Types 50 6.1 Simple data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 6.2 Compound data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 6.2.1 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 6.2.2 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 6.2.3 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 6.3 Section summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544 CONTENTS 6.4 End of section feedback questions . . . . . . . . . . . . . . . . . . . . . . 54 7 Case Study: Class java.lang.String 55 7.1 String API overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 7.1.1 Heading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 7.1.2 Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 7.1.3 Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 7.1.4 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 7.2 Using strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 7.2.1 Class String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 7.2.2 String objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 7.2.3 String references . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 7.3 Equality for strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 7.4 Building strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 7.5 Unicode consequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 7.6 The design of class String . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 7.7 Section summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 7.8 End of section feedback questions . . . . . . . . . . . . . . . . . . . . . . 63 7.9 Self-practice exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 8 Case Study: java.util.Scanner 64 8.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 8.2 Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 8.3 Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 8.3.1 Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 8.3.2 Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 8.3.3 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 8.4 State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 8.5 Example: Average . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 8.6 Example: MovingAverage . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 8.6.1 Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 8.6.2 Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 8.6.3 Client program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 8.7 Section summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 8.8 End of section feedback questions . . . . . . . . . . . . . . . . . . . . . . 71 8.9 Self-practice exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 9 Essential Object Oriented Programming 73 9.1 Review of definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 9.1.1 Static versus dynamic . . . . . . . . . . . . . . . . . . . . . . . . . 73 9.1.2 Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 9.1.3 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 9.1.4 Fields, properties, attributes . . . . . . . . . . . . . . . . . . . . . 74 9.1.5 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74CONTENTS 5 9.1.6 Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 9.1.7 Members of a class . . . . . . . . . . . . . . . . . . . . . . . . . . 74 9.1.8 Instances of a class . . . . . . . . . . . . . . . . . . . . . . . . . . 74 9.2 New definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 9.2.1 Accessors and mutators . . . . . . . . . . . . . . . . . . . . . . . . 74 9.2.2 Message passing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 9.2.3 Object oriented programming . . . . . . . . . . . . . . . . . . . . 75 9.2.4 Object oriented design . . . . . . . . . . . . . . . . . . . . . . . . 75 9.3 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 9.3.1 Diagramming inheritance in UML . . . . . . . . . . . . . . . . . . 76 9.3.2 Practical uses for inheritance . . . . . . . . . . . . . . . . . . . . . 78 9.3.3 extends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 9.3.4 java.lang.Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 9.3.5 protected . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 9.3.6 final classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 9.3.7 Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 9.3.8 Abstract classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 9.4 Section summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 9.5 End of section feedback questions . . . . . . . . . . . . . . . . . . . . . . 80 10 Case Study: Number Classes 81 10.1 java.lang.Double . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 10.2 Wrapper classes – boxing and unboxing . . . . . . . . . . . . . . . . . . . 81 10.3 Abstract class Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 10.4 Optional parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 10.5 printf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 10.6 String.format() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 10.7 Section summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 10.8 End of section feedback questions . . . . . . . . . . . . . . . . . . . . . . 85 11 Errors and Exceptions 86 11.1 Kinds of errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 11.2 Error handling in programs . . . . . . . . . . . . . . . . . . . . . . . . . . 86 11.2.1 Defensive programming . . . . . . . . . . . . . . . . . . . . . . . 87 11.2.2 Programming by contract . . . . . . . . . . . . . . . . . . . . . . . 89 11.2.3 Unavoidable, unexpected errors . . . . . . . . . . . . . . . . . . . 89 11.3 Java’s Errors and Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . 90 11.3.1 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 11.3.2 OpenURI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 11.3.3 throws – passing the buck . . . . . . . . . . . . . . . . . . . . . . . 92 11.3.4 try-catch – the buck stops here . . . . . . . . . . . . . . . . . . . . 93 11.3.5 throw . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 11.4 Section summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 11.5 End of section feedback questions . . . . . . . . . . . . . . . . . . . . . . 966 CONTENTS 12 Java Language Goodies 97 12.1 Operator goodies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 12.1.1 Operator summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 12.1.2 Shorthand operators . . . . . . . . . . . . . . . . . . . . . . . . . . 99 12.1.3 instanceof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 12.2 this goodness right here . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 12.2.1 For constructors and mutators . . . . . . . . . . . . . . . . . . . . 100 12.2.2 One constructor calling another . . . . . . . . . . . . . . . . . . . 100 12.3 Array goodies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 12.3.1 java.util.Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 12.3.2 The for-in statement . . . . . . . . . . . . . . . . . . . . . . . . . . 101 12.4 Section summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 12.5 End of section feedback questions . . . . . . . . . . . . . . . . . . . . . . 102 13 UML Class Diagrams 103 13.1 It’s about communication . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 13.1.1 Efficiency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 13.1.2 Programming language independence . . . . . . . . . . . . . . . . 103 13.1.3 Illustration not definition . . . . . . . . . . . . . . . . . . . . . . . 103 13.2 Class diagram elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 13.2.1 Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 13.2.2 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 13.2.3 Attribute basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 13.2.4 Method basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 13.2.5 Visibility of members . . . . . . . . . . . . . . . . . . . . . . . . . 105 13.2.6 Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 13.2.7 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 13.2.8 Static members . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 13.3 Class relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 13.3.1 Dependency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 13.3.2 Association . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 13.3.3 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 13.3.4 Abstract classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 13.4 Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 13.5 Section summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 13.6 End of section feedback questions . . . . . . . . . . . . . . . . . . . . . . 110 14 2D Graphics In a Window 111 14.1 History: AWT and Swing . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 14.2 Top level containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 14.3 Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 14.4 Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 14.5 JFrame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 14.6 JComponent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115CONTENTS 7 14.7 Graphics and Graphics2D . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 14.8 Section summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 14.9 End of section feedback questions . . . . . . . . . . . . . . . . . . . . . . 117 14.10Self-practice exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 15 Advanced Data Structures 119 15.1 Abstract data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 15.2 The built-ins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 15.2.1 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 15.3 The built . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 15.3.1 Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 15.3.2 Bags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 15.3.3 Lists or sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 15.3.4 Stacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 15.3.5 Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 15.3.6 Deques – double ended queues . . . . . . . . . . . . . . . . . . . . 124 15.3.7 Priority queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 15.3.8 Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 15.4 Implementing advanced data types . . . . . . . . . . . . . . . . . . . . . . 125 15.4.1 With arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 15.4.2 With dynamic, linked, recursive data structures . . . . . . . . . . 126 15.4.3 Singly linked lists . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 15.4.4 Doubly linked lists . . . . . . . . . . . . . . . . . . . . . . . . . . 126 15.4.5 Binary search trees . . . . . . . . . . . . . . . . . . . . . . . . . . 127 15.4.6 Hash tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 15.5 Section summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 15.6 End of section feedback questions . . . . . . . . . . . . . . . . . . . . . . 130 16 Generics 131 16.1 Before parameterised classes . . . . . . . . . . . . . . . . . . . . . . . . . 131 16.1.1 Example – Deque . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 16.1.2 Example problem requiring a deque . . . . . . . . . . . . . . . . . 135 16.1.3 An employee . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 16.1.4 The program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 16.2 Parameterised classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 16.2.1 Generics syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 16.2.2 Deque with generics . . . . . . . . . . . . . . . . . . . . . . . . . . 141 16.2.3 The client program with generics . . . . . . . . . . . . . . . . . . 142 16.3 Section summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 16.4 End of section feedback questions . . . . . . . . . . . . . . . . . . . . . . 1438 CONTENTS 17 Interfaces 144 17.1 More abstract than abstract classes . . . . . . . . . . . . . . . . . . . . . . 144 17.1.1 Abstract classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 17.1.2 Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 17.2 UML representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 17.3 Defining an interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 17.4 Implementing an interface . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 17.5 Case study: java.lang.Runnable . . . . . . . . . . . . . . . . . . . . . . . . 147 17.6 Case study: Comparable and Comparator . . . . . . . . . . . . . . . . . . 147 17.7 Case study: Iterable and Iterator . . . . . . . . . . . . . . . . . . . . . . . 148 17.8 Section summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 17.9 End of section feedback questions . . . . . . . . . . . . . . . . . . . . . . 149 18 The Collections Framework 150 18.1 The interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 18.1.1 Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 18.1.2 Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 18.2 The classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 18.2.1 ArrayList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 18.2.2 ArrayDeque . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 18.2.3 LinkedList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 18.2.4 TreeSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 18.2.5 TreeMap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 18.2.6 HashSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 18.2.7 HashMap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 18.3 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 18.3.1 Dumb HRM example . . . . . . . . . . . . . . . . . . . . . . . . . 158 18.4 Indicating container classes in UML . . . . . . . . . . . . . . . . . . . . . 159 18.5 A more complex example: CrossRef . . . . . . . . . . . . . . . . . . . . . 160 18.6 Section summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 18.7 End of section feedback questions . . . . . . . . . . . . . . . . . . . . . . 160 19 Inner Classes 161 19.1 General costs and benefits . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 19.2 Static member classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 19.3 Dynamic member classes . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 19.3.1 Example with UML representation . . . . . . . . . . . . . . . . . 162 19.4 Local classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 19.5 Anonymous local classes . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 19.6 Section summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 19.7 End of section feedback questions . . . . . . . . . . . . . . . . . . . . . . 164CONTENTS 9 20 GUI Components and Events 166 20.1 The problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 20.2 Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 20.2.1 GUI design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 20.2.2 Class designs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 20.3 New console implementation . . . . . . . . . . . . . . . . . . . . . . . . . 169 20.4 GUI implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 20.4.1 main and frame . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 20.4.2 Components and layout . . . . . . . . . . . . . . . . . . . . . . . . 174 20.4.3 Event handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 20.5 Section summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 20.6 End of section feedback questions . . . . . . . . . . . . . . . . . . . . . . 181 21 Sundries 182 21.1 Expressions as statements . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 21.2 Order of field initialisation . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 21.3 Advanced inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 21.3.1 Overriding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 21.3.2 super-duper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 21.3.3 super in constructors . . . . . . . . . . . . . . . . . . . . . . . . . 184 21.3.4 super to access superclass fields . . . . . . . . . . . . . . . . . . . 184 21.3.5 super to access overridden methods . . . . . . . . . . . . . . . . . 186 21.4 Section summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 21.5 End of section feedback questions . . . . . . . . . . . . . . . . . . . . . . 186 22 Fit and Finish 187 22.1 Use an IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 22.2 Doc comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 22.2.1 Summary sentence . . . . . . . . . . . . . . . . . . . . . . . . . . 188 22.2.2 Additional paragraphs . . . . . . . . . . . . . . . . . . . . . . . . . 188 22.2.3 Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 22.3 Packaging as a jar file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 22.3.1 Running a jar file . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 22.4 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 22.5 NetBeans demonstration . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 22.6 Last words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 22.6.1 Thanks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 22.6.2 What to do next . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 22.7 Section summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 22.8 End of section feedback questions . . . . . . . . . . . . . . . . . . . . . . 19010 CONTENTS A Conventions Used In these Notes 192 A.1 Typefaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 A.2 Program code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 A.3 Program code templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 A.4 Console sessions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 A.5 Section summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 A.6 End of section feedback questions . . . . . . . . . . . . . . . . . . . . . . 193 B Syntax Diagram Notation 194 B.1 Section summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 B.2 End of section feedback questions . . . . . . . . . . . . . . . . . . . . . . 194 C Installing the JDK 195 C.1 JDK versus JRE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 C.2 For Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 C.2.1 Setting the path . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 C.3 For Mac OS X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 C.4 For Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 C.5 Section summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 C.6 End of section feedback questions . . . . . . . . . . . . . . . . . . . . . . 196 D UML Class Diagrams Summary 197 D.1 It’s about communication . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 D.1.1 Efficiency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 D.1.2 Programming language independence . . . . . . . . . . . . . . . . 197 D.1.3 Illustration not definition . . . . . . . . . . . . . . . . . . . . . . . 197 D.2 Class diagram elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 D.2.1 Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 D.2.2 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 D.2.3 Visibility of members . . . . . . . . . . . . . . . . . . . . . . . . . 198 D.2.4 Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 D.2.5 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 D.2.6 Static members . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202 D.3 Class relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202 D.3.1 Dependency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202 D.3.2 Association . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202 D.3.3 Aggregation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 D.3.4 Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 D.3.5 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 D.3.6 Abstract classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 D.3.7 Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 D.3.8 Parameterised classes . . . . . . . . . . . . . . . . . . . . . . . . . 204 D.3.9 Container classes . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 D.3.10 Inner classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205CONTENTS 11 D.3.11 Anonymous inner classes . . . . . . . . . . . . . . . . . . . . . . . 205 D.4 Section summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 D.5 End of section feedback questions . . . . . . . . . . . . . . . . . . . . . . 206 Glossary 207 Acronyms 216 Index 21812 CONTENTS Preface These are the lecture notes for 1005ICT Object Oriented Programming and 7005ICT Pro- gramming Principles 2. The version you are reading contains all of the text of the slides I use in lectures, and so much more: a table of contents; and index; appendices; and a glossary. This is the version that I hope you will use to actually read and refer to, in preference to the slides. It has been formatted for A5 paper, as that size is very readable on a tablet or laptop screen. It prints very nicely on A5 paper, and is designed to be printed double- sided. Colour printing is helpful, but not essential. Using it in electronic form has extra benefits. Coloured text is usually a hypertext link to the web or within the document. The appendices contain a lot of background information that we don’t have time to cover in the actual lectures, and a lot of reference information. A good one to read as soon as possible is appendix A, which explains the formatting of the text in these notes, and other course materials. The glossary is a list of the terms used in the course, part of the professional vocabu- lary for information technologists. For nearly all glossary entries, I have added a link to Wikipedia or some other reputable source for further reading on the topic. I always consider my courses and lecture notes as works in progress. Let me know if you see any errors, or would like glossary or index entries for more terms. Use the feedback questions at the end of each section. They link to my feedback system. I hope you enjoy these notes and the course. Andrew Rock a.rockgriffith.edu.au13 1 Introduction Welcome to 1005ICT object oriented programming (OOP). These lecture notes are divided into sections. This one describes the course’s: • content; • roles and responsibilities; • assessment; • resources; and • some things you need to do now. 1.1 7005ICT Programming Principles 2 For this semester, the class also includes Masters students enrolled in 7005ICT Program- ming Principles 2 (PP2). The course materials and assessment items will be the same as for the undergraduate course, however the grading basis will be different. Please note that this is the second part of our introduction to programming. If your undergraduate program did not include programming, please see the course convenor or your program convenor for advice as to whether this is the right course for you. 1.2 Take notes? These notes are posted on the web. So you don’t have to copy them all down. But we say more than goes in the slides. You should be prepared to take notes that capture the extra information. 1.2.1 Take notes today Write down the things in colour. 1.3 Course Convenors & Lecturers course convenor Andrew Rock & lecturer a.rockgriffith.edu.au Technology Building (N44) 1.37 1.4 Course web site These notes will be posted on the web at: http://www.ict.griffith.edu.au/arock/oop/ You will find the laboratory notes and anything else you need there too. Write down the uniform resource locator (URL) now14 1 INTRODUCTION You can also access these resources via LearningGriffith, though direct access via the above URL will be quicker. Lectures are captured and available, after editing, on LearningGriffith’s Echocentre. 1.5 What the course is about This is the second part of an introduction to: • programming tools; • programming concepts; • problem solving; and • programming languages. The emphasis will be on the object oriented aspects of the Java programming language. 1.6 The choice of programming languages In your working life you will write lots of programs, in lots of programming languages. Any time you create a script, automation, spreadsheet, or animation, it’s programming. It may be that you never program in Java after completion of your programming classes. However, Java is a programming language that has all of the attributes found in all of the common application and scripting languages. Its notation is similar to many, including C, C++, C, JavaScript, and ActionScript. 1.7 Prerequisites This course assumes some knowledge of programming in an imperative programming language. Most of you will have completed 1001ICT Introduction To Programming (ITP). That course uses an imperative subset of Java, Making Stuff Happen (MaSH). If you have not done that particular course, any prior course on programming should provide an equivalent background. MaSH is very similar to C, Pascal, and related languages. If you do not have a pass or better for 1001ICT or an equivalent course, you are not qualified to undertake this course. See me asap, if you are unsure that you have the prerequisites. 1001ICT and 7001ICT are also offered this semester. 1.8 Classes and assessment 1.8.1 Lectures In a 2-hour lecture every week, we will learn the concepts and strategies used in program- ming. During 5 of the lectures, we will test your learning with quizzes. 3+4+5+6+7=25marks1.8 Classes and assessment 15 1.8.2 Workshops In a 1-hour workshop every week we demonstrate more examples and provide and discuss extra non-assessed problems. 1.8.3 Labs In 10 2-hour assessed lab classes you put the theory into practice. 2+2+3+3+4+4+5+5+6+6=40marks 1.8.4 Common times A common time is a class where there are no set activities, but you can get individual help if you need it or we can challenge you with extra, harder problems if you want them. 1.8.5 Final project The course ends with a larger programming project (35 marks). 1.8.6 Attendance summary Students are expected to attend all lectures and workshops. You are enrolled in one 2 hour labs class. Attend that every week from week 2. Attend any common time as needed, from week 2. “Eighty percent of success is showing up.” – Woody Allen. 1.8.7 Assessment summary quizzes 3+4+5+6+7= 25 labs 2+2+3+3+4+4+5+5+6+6= 40 project 35 total 100 1.8.8 Minimum requirements for labs and quizzes The labs are quizzes are treated as one assessment item each. If you do not attend the majority of each, we don’t think you are really attempting them. If you don’t attend at least 6 labs, you don’t get any lab marks at all. If you don’t attend at least 3 quizzes, you don’t get any quiz marks at all. Either of these outcomes will make it almost impossible to pass the course. You may even be awarded a grade of Fail Not Sat, which would be a better indication of your performance than a plain Fail.16 1 INTRODUCTION 1.8.9 Supplementary exam Supplementary assessment, if passed, can raise your grade from a 3 to the lowest passing grade (a 4). To be eligible, you must have been awarded a 3, have submitted/sat the majority of the assessed labs, the majority of the quizzes, and at least part of the final project. For this course, the supplementary assessment takes the form of a two-hour, closed- book, written examination. 1.9 Lecture notes The lecture notes are available on the course web site in various formats. The most useful format is the A5 version, which: • has a table of contents; • has an index; • has clickable links; • includes appendices that summarise the programming language and environments, plus other useful reference information; • is ideally formatted for a tablet or laptop screen; and • prints double-sided as a small book. 1.10 Griffith Sciences Laboratory Induction You will have been briefed in orientation about workplace health and safety, and the on- line course and test on LearningGriffith in the organisation Griffith Sciences Laboratory Induction that you must complete. In this course we will be checking that you have passed that test. You must print the certificate that shows that you passed that test, within the last year, and bring it to your first programming lab class (in week 2). 1.11 Peer Assisted Study Sessions At Nathan, this course has a student learning support service, Peer Assisted Study Ses- sions (PASS). PASS offers free extra weekly study sessions, led by more experienced fellow students (PASS leaders). The details about PASS will be presented by the PASS leaders in the week 2 lecture. PASS is optional (and limited access), but students who use it have been shown to get better results than students who don’t. At Logan PASS is not available. However, we are trying to find an alternate means of extra support, and change the Nathan PASS timetable so it is possible for Logan students to commute.1.12 Revision 17 Please use PASS or the alternatives Attendance is counted and if it drops below a certain level they get withdrawn. Just go to be social, so they are there when you need them. Don’t forget that they are led by high-achieving students, and they may have tips for you beyond the course content. 1.12 Revision There is no section in these lecture notes, marked “revision”. That would be boring to write, boring for students who had passed 1001ICT, and a bit mystifying for students with other backgrounds. All students should do some revision with their previous course materials. If you did 1001ICT, the last section Why MaSH Is Rubbish is a good place to start. As we walk through the new material, I will endeavour to indicate what is similar to what you have seen before and what is truly new. 1.13 Section summary This section covered: • why this course on programming is an important part of your program of studies; • prerequisites; • how this course is organised, taught and assessed; • where the resources for this course can be found on the web; • who the teachers are and how to contact them; and • what you need to do to get started in this course. 1.14 End of section feedback questions Send us your answers to these questions any time you like by clicking on them. • What was the most useful topic in this section? • What was the least useful topic in this section? • What was the least clear topic in this section? • What topic in this section would you like to like to know more about? • Did you find an error in this section?18 1 INTRODUCTION 1.15 Things to do What you need to be doing as soon as possible to get started: • Get a diary or organiser and plan how you can spend your week profitably, including the following items. • Make sure you are properly enrolled in this course and in a laboratory. • Read the course outline and the course web site. • Prepare for the first workshop by reading the exercises on the web site. • Prepare for laboratory 1, in week 2. Find the notes on the web site. There are some questions you need to answer before your lab class. • Complete the Griffith Sciences Laboratory Induction on LearningGriffith, and bring the printed certificate to labs at least once. • Revise your previous programming course notes. • Lecture notes appendices A and B contain useful information that you should read now.19 2 Kick-start Java This section provides just enough Java to enable the practical activities in the first labora- tory class (in week 2). The goals of the first laboratory will be to ensure that everyone (no matter their previous background) can edit and run a Java program using the most basic tools. The emphasis in this section is on how to do some basic things, but not on why we do them that way. 2.1 Hello, World This was the first and last example program in 1001ICT: public class Hello public static void main(String args) System.out.println("Hello, World"); It must be saved in a file calledHello.java. All of its components will be discussed below. 2.1.1 System.out.println() Use the procedure System.out.println() to print one anything – one String, oneint, oneboolean, etc. – and then a newline. Examples: System.out.println("Hello, World"); // String System.out.println(42); // int System.out.println(true); // boolean System.out.println(4.2); // double System.out.println("Meaning = " + 42); // What type? There is alsoSystem.out.print(), which does not output a newline. 2.1.2 The class The whole program is encapsulated in a class: public class Hello 20 2 KICK-START JAVA A class is the compilation unit for Java, that is, the smallest language element that the compiler will accept. normalClassDeclaration class identifier classBody classModifier (This is a syntax diagram. See Appendix B for an explanation of the notation.) public is an optional class modifier, but usual for a class that is to be made available for use. classModifier public private static Not all of these modifiers are useable with all kinds of classes. As we learn more about Java’s object oriented aspects, more modifiers will be introduced. The identifier is the name of the class. identifier javaLetter javaLetter digit not keyword booleanLiteral A class’s name must start with a capital letter. The name of apublic class must match the name of the file.