COMP1406 Course Notes: Introduction To Computer Science II

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

COMP1406

Introduction to Computer Science II

Course Notes

Notes maintained by Mark Lanthier (2017 version)


Table of Contents
1 Programming in JAVA ............................................................….............. 1
1.1 Object-Oriented Programming and JAVA ................................……………............................. 2
1.2 Writing Your First JAVA Program ……………………………………………….......................... 6
1.3 Python vs. JAVA ..................................…………...…………....…......................................... 8
1.4 Getting User Input ………………………………………......................…………........................ 11
1.5 Formatting Text .......................................................................................................................15

2 Creation and Storage of JAVA Objects .....….............…………………… 21


2.1 Using Existing JAVA Objects …………………...……......…………............………................... 22
2.2 Creating Your Own Objects in JAVA ...………...……......…………............………................... 24
2.3 Memory Allocation and Object Storage …………...………………............……….................... 27

3 Defining Object Behavior ..…………………….............…………………… 39


3.1 Object Constructors (Re-Visited) …………………...………………............………................... 40
3.2 Defining Methods ……………………………………………….................................................. 44
3.3 Null Pointer Exceptions …………………….……………......................................…................ 52
3.4 Overloading ….……………………………………..........................................…………............ 54
3.5 Instance vs. Class (i.e., static) Methods .....………………………............................................ 56
3.6 Encapsulation - Protecting An Object's Internals .....................................................................60
3.7 Changing How Objects Look When Printed .............................................................................69
3.8 A Bank Example ......................................................................................................................73

4 Class Hierarchies and Inheritance ..………………………………………… 84


4.1 Organizing Classes ………………………...………………….......................…………..……..... 85
4.2 Inheritance ……..……………………………….........…………………………............................. 90
4.3 Abstract Classes & Methods ……………………………………………..................................... 108
4.4 JAVA Interfaces ...................................................................................................................... 115
4.5 Polymorphism ......................................................................................................................... 120

5 Graphical User Interfaces .........................................……………………... 134


5.1 User Interfaces and Java FX Applications …………………………........................................ 135
5.2 Components and Containers .………………………………………………............................... 140
5.3 Grouping Components Together .……………………………………………….......................... 146
5.4 Event Handling .………………………………………………...................................................... 153

6 Proper Coding Style Using MVC …..……........................………………… 168


6.1 Separating Model, View and Controller Components …………………………………………....169
6.2 Preparing Your Model Classes for the GUI ..............…………………………………….............170
6.3 Developing a Proper View .................…………………………………………………………...... 174
6.4 Developing a Proper Controller ...............................................................................................179

7 User Interface Extensions ……………………………….…………………....186


7.1 Automatic Resizing Using Layouts …….............………........................................................ 187
7.2 Adding Menus …...…………………………………................................................................... 207
7.3 Standard Dialog Boxes ……………………..............…………………………………………...... 215
7.4 Making Your Own Dialog Boxes ………………................……………………………………..... 233

ii
8 Abstract Data Types …...…………………………..………………….............250
8.1 Common Abstract Data Types ..………........…………………………………………………...... 251
8.2 The List ADT ………………………………….....……….…………………………....................... 253
8.3 The Queue ADT .…………………………………………………………………........................... 274
8.4 The Deque ADT ……...................…………………………………………………….................... 281
8.5 The Stack ADT ……...................……………………………………………………..................... 283
8.6 The Set ADT ……...................……………………………………………………........................ 289
8.7 The Dictionary / Map ADT ……...................…………………………………………………….... 297
8.8 Collections Class Tools ……...................…………………………………………………….........308
8.9 Implementing an ADT (Doubly-Linked Lists) .…………………………………………………... 311

9 Recursion With Data Structures ………………………..…………………….320


9.1 Recursive Efficiency …..…………………………………………….....……................................ 321
9.2 Examples With Self-Referencing Data Structures ……………………………………………... 323
9.3 A Maze Searching Example ………………………………………………....…........................... 343

10 Exception Handling ………………………..…………..…………............… 351


10.1 Simple Debugging ……………………………………………................................................... 352
10.2 Exceptions ………………………………………………....….................................................... 354
10.3 Examples of Handling Exceptions …..…………………………………………….....……......... 364
10.4 Creating and Throwing Your Own Exceptions …..…………………………………………...... 371

11 Saving and Loading Information ………………………..…………………. 379


11.1 Introduction to Files and Streams ……………………………………………............................ 380
11.2 Reading and Writing Binary Data ……………………………....….......................................... 382
11.3 Reading and Writing Text Data …..…………………………………………….....…….............. 388
11.4 Reading and Writing Whole Objects …..…………………………………………….................. 391
11.5 Saving and Loading Example …..……………………………………………............................ 395
11.6 The File Class …..…………………………………………….................................................... 402

12 Network Programming ……...…………………..……………………….........406


12.1 Networking Basics ……………………………………………................................................... 407
12.2 Reading Files From the Internet (URLs) ..…...........................................................................411
12.3 Client/Server Communications …..………………………………………...................................415
12.4 Datagram Sockets …..……………………………………………...............................................422

13 Other Interesting JAVA Classes ……………………........…..……………...427


13.1 The String Class ……………………………....….................................................................... 428
13.2 The StringBuilder & Character Classes …..……………………………………….................... 434
13.3 The Date and Calendar Classes …..……………………………………………....................... 437

14 Graphics …………………….......................…..……………………….......... 444


14.1 Doing Simple Graphics .........…………………………………...................................................445
14.2 Displaying Images and Manipulating Pixels ......................................................................... 451
14.3 Graph Editor Example ......………………………………………….............................................454
14.4 Adding Features to the Graph Editor ......................................................................................475

iii

You might also like