Object-Oriented Programming For Engineers

Dr. Derek Molloy

Dublin City University
School of Electronic Engineering

Please notify me by e-mail if you spot any errors or omissions. I would also be interested in any feedback you may have about these documents.

Legal Information

This document was written by Dr. Derek Molloy for students in the School Of Electronic Engineering, Dublin City University. You may not pass this document to anybody.

By using this document, you must obey the following conditions: You must be taking this module for personal private study, and will not reuse any of the materials supplied (i.e. course notes, software ..etc.) for any purpose other than personal private study. You are bound by the copyright statement "This Hypermedia document (Object-Oriented Programming For Engineers) and its associated materials are copyrighted © 1999-2011/12. All rights reserved. While every reasonable effort is made to ensure the reliability and continuity of this Hypermedia document, this material is supplied for general reference only and should not be relied on without further specific inquiry. SUBSCRIBERS are granted a limited license to make a single copy of documents in this Hypermedia document in print and/or electronic form for personal research or educational use. Course notes may be downloaded, stored on a hard drive or other storage device, or printed in hard copy form, with the following restrictions and exceptions: Systematic or multiple-copy reproduction or republication; electronic retransmission to another location; print or electronic duplication of any material in this course for a fee or for commercial purposes; or altering or recompiling any contents of the course and its associated materials are not permitted."

By choosing to view, download, or print any portion of this course and its associated materials, you agree to all the provisions of the copyright law protecting it and to the terms and conditions established by the copyright holder.

Document Format Information

This document is formatted into chapters and sections. You can use the controls at the top and bottom of the document to move between sections. The back and forward buttons refer to the next and last section as organised in the notes - not the last section you have visited. At any time you can return to the higher-level index by using the up button at the bottom centre of the page. The home button in the same location will return you to the main index. This is a dynamic document that will be updated during the year. You can select to download a .pdf version of this document, but please be aware that any additions made to the document will automatically be made to the .pdf version. If you choose to download and print the .pdf version at the start of the year, please check for changes.

The notation in this document is as follows:

  • A class is represented as SomeClass, an interface by SomeInterface and and exception by SomeException - All classes, interfaces and exceptions begin with a capital letter and further words in the name are also capitalised. If a class, interface or exception is underlined (hyperlinked) then you can follow the link to the API documentation directly, so for example String will display the Java API documentation for the String class. This is linked to the SUN site, so you will have to remain on-line to use this feature. It will open the pages in a new window (always the same one).

  • An object is represents as someObject - All objects begin with a lower-case letter and further words in the object name are capitalised.

  • A method is represented as someMethod() even if it has parameters. All methods begin with a lower-case letter.

  • In general language a literal is given as public, a type as int, and a variable as someInt.

  • A command, such as at the DOS prompt is represented as javac Test.java for example.

  • A filename is represented as SomeFile.txt.

  • If something is important it is represented like this.

Technical Document and Content Information

This document has been written in XML and is parsed to HTML and PDF using the following technologies: Docbook (document standard and DTD), Saxon (XML parser), XSLT (for HTML output), FOP (for PDF output), JIMI (for image formatting), CSS 2 (for local browser style), XSL (for HTML and PDF formatting) and edited in Cooktop (XML editor). I have tried to make the HTML output as generic as possible to work on all browsers. This document has been checked for CSS validity by WC3 - CSS Validator. All images have been edited in Adobe Photoshop and Illustrator and converted to .gif, .png formats or .eps format (for some PDF images). Encryption of PDF documents has been implemented using Bouncy Castle.

All Java code has been tested with JDK 1.4.2 and run under Windows XP Professional - If you notice any problems on other JDK versions or operating systems, please notify me.

The multi-choice questions are implemented using Java Servlets on the Tomcat/JBoss servlet/EJB engine, built with an XML standard and internal Xerces and Xalan parsing. Output is styled using CSS only.



Dedicated to the hard work of all the students that have completed this module, with special thanks to those who have provided suggestions or helped correct errors and omissions throughout the years.

Table of Contents

1. Introduction to Object-oriented Programming
Introduction to Object-Oriented Programming
An Object-Oriented Class
An Object
Object-Oriented Terminology
Object-Oriented Analysis and Design
Why use the object-oriented approach?
An Example Design Problem
EE553 - Self-Assessments for OOP Introduction
OOP Assessments
2. Introduction to C++ (The "C" of C++)
A First Application
Setting up a compiler
General Language
Methods (or Functions) in C++
Strings in C++
Assignment statements
Scope of Variables
Pointers in C/C++
C++ Expressions
Precedence Reference:
3. C++ and Object-Oriented Programming
Classes in C++
Constructors and Member Initialisation Lists
Classes and Pointers
Class Hierarchy
Static and Dynamic Types
Abstract Classes
C++ Explicit Casts
Method Parameters
Default Parameters
Constant Parameters
Operator Overloading
Multiple Constructors
The copy constructor
Separate Compilation
Dynamic Creation of Objects
Arrays of Objects
Multiple Inheritance
Friend Methods
Static States of Classes
More Scope Resolution Operations
Structs in C++
Unions in C++
Non-Virtual Methods
Inline Methods:
Full Source Code Examples
The Account Class
The Account and CurrentAccount Classes
Multiple Inheritance Example (with problems)
The Multiple Inheritance Example Fixed
A Final Working Version of the Cashsave example
Friend Methods, Pointers to Objects and Destructors
Now with a Copy Constructor!
Destructors and Arrays.
4. Introduction to Java
Introduction to Java
The Java LifeCycle
Just-In-Time Compilation (Dynamic Translation)
The Java 2 Software Development Kit (Java SDK)
Installation of the Java 2 Software Development Kit
Testing the JDK
The Java Runtime Environment (JRE)
New Concepts in Java
Garbage Collection
An Example Java Class
New concepts in Java
super and this predefined variables
Abstract Classes in Java
Strings in Java
Java Arrays
The Object class
The Class class
The Java API Documentation
5. Differences and Similarities between C++ and Java
The General Language
Data Types
Memory management
No Pointers
Global Functions
Method Parameters
OOP Concepts in C++ and Java
Similar Constructors
No Destructors!
Access Specifiers
Access Specifiers when Inheriting Classes
Virtual and Non-Virtual Methods
Multiple Inheritance
No scope resolution operator
Nested Classes
Inline Methods
The Compiler
General Compiler issues
Default Arguments
No Preprocessor
6. C++ Standard Libraries
What are templates?
Function Templates
Class Templates
STL Introduction
STL Containers
STL Iterators
STL Algorithms
A for_each example
Functors etc.
7. Java™ Applets - An Introduction
A Simple Applet Example - Hello World!
The Life Cycle of an Applet
Hello World Applet - In Detail
ChessApplet - An Applet for you to try!
Passing Parameters To An Applet
An Exercise For You! Passing parameters to an Applet.
8. Java™ Applets - Images and Audio
Displaying Graphics in Applets
An Image Display Example
Playing Audio Clips
A Sound Playing Example Applet
Using the Mouse in Applets
A Mouse Event Example
An Exercise for You. Images, Sound and Mouse!
9. Java™ Graphical User Interface(GUI) Components
AWT Components
Button (java.awt.Button)
Checkboxes (java.awt.Checkbox)
Radio Buttons (java.awt.CheckboxGroup)
Choice Buttons (java.awt.Choice)
Labels (java.awt.Label)
TextFields (java.awt.TextField)
An Example Component Application
Layout Policies
FlowLayout (java.awt.FlowLayout)
GridLayout (java.awt.GridLayout)
BorderLayout (java.awt.BorderLayout)
The Panel Container (java.awt.Panel)
A Layout Exercise
More AWT GUI Components
List (java.awt.List)
TextArea (java.awt.TextArea)
Scrollbar (java.awt.Scrollbar)
Using these Components
ActionEvent (java.awt.event.ActionEvent)
Other Event Types
Using Other Components
A Scrollbar Example
Exercise - The Font Chooser Applet
Mixing Components and Graphics
The Canvas class
A Functional Canvas Example
An Exercise For You. The Colour Chooser Applet
10. Networking in Java - Client/Server Applications
Java Internet Aware Applications
The InetAddress class
A Simple "Who Is?" Application
Java TCP Client Applications
The Finger Client
The Basic Web Browser
The TCP Client/Server
The Date Client/Server Overview
The Running Server/Client Applications
The Source Code
An Exercise For You. The Updated Client/Server Application
11. Threads and Networking
Threads - An Introduction
Multiprocessing vs. Multithreading
Why use threads?
Threads - A better way?
A Working Counter
Threads - More Aspects
Suspending and Resuming Threads
Try this yourself! - An Up/Down Counter
Scheduling of Threads
Thread Priorities
An Example of Priorities and Threads
Synchronization of Threads
A Good Bad Example
Adding Synchronization to Code
A Multi-Threaded TCP Server
An Exercise For You. The Random Draw Thread
12. Swing - Lightweight Components
Developing with Swing
A Basic Swing Example
Example Extra Swing Features
Core Swing Components
Exercise - Swing Components
Other Properties added to Swing Components
Other Useful Swing Components
General Components
Exercise. Write an Egg Timer
Toolbars and Multiple Container Classes
Toolbars and Menues
Multi-Panel Containers
Dialogs and More Components
The File Chooser Dialog
Other Swing Components
An Exercise for You. The Image Loader Application
13. Java Client Server Networking
Remote Method Invocation (RMI)
The concept
Compilation and Running
14. More Java Concepts
More Storage Containers
More Java Arrays
Java Generics
More Exceptions
Debugging Code
Using Assertions in Java
Some Other Points
Static Imports
15. Exercise Solutions For Previous Chapters
Solutions to Java Exercises
Chess Applet with Parameters Solution
Images, Sound and Mouse! Solution
The Colour Chooser Applet Solution
The Updated Client/Server Application Solution
The Random Draw Thread Solution
The Image Loader Exercise Solution

List of Figures

1.1. The concept of a class - television example.
1.2. The Television class example.
1.3. The Television objects example.
1.4. The Television interface example.
1.5. The Television class example showing encapsulation.
1.6. The Duck class showing inheritance.
1.7. The grouped set of classes.
1.8. The Base class and Derived class.
1.9. The IS-A/IS-A-PART-OF relationships and the Vehicle class.
1.10. The over-ridden draw() method.
1.11. The abstract draw() method in the Vehicle class.
1.12. The abstract draw() method in the Vehicle and Car classes.
1.13. The Waterfall Model
1.14. The Spiral Model
1.15. The Object-Oriented Design Model
1.16. The Bank class.
1.17. The Account class.
1.18. The Transaction class.
2.1. Borland Compiler Download Dialog Warning
2.2. Setting the Path environment variable.
2.3. The Hello World Application in Action.
2.4. The Dev C++ Integrated Development Environment (IDE)
2.5. The Codeblocks Integrated Development Environment (IDE)
2.6. An example memory space with variables defined.
2.7. An example use of pointers.
2.8. The output from an example use of pointers with arrays.
2.9. The pointer example in operation, steps 1 to 4 as in the code sample above.
2.10. The pointer example in operation, steps 5 to 7 as in the code sample above.
2.11. The if structure.
2.12. The if else structure.
2.13. The while structure.
2.14. The do while structure.
2.15. The for structure.
2.16. The switch structure.
3.1. A Sample Bank Account Class
3.2. The Account and CurrentAccount class relationship.
3.3. The extended Account and CurrentAccount class relationship.
3.4. The output from the code segment above.
3.5. The pointers ptrA and ptrB have the same static and dynamic types.
3.6. Allowed: The pointer ptrA has static type Account and dynamic type CurrentAccount.
3.7. Not Allowed: Pointer ptrB has static type CurrentAccount and dynamic type Account.
3.8. The output from the Account Destructor Example.
3.9. The Account class with Multiple Constructors example output.
3.10. The Output from the Modified Copy Constructor Example.
3.11. Compilation, and the output from the Separately Compiled Example.
3.12. The Account Dynamic Allocation Example.
3.13. The Account Dynamic Allocation Illustration.
3.14. The Account Dynamic De-Allocation First Test.
3.15. The Account Dynamic De-Allocation Second Test.
3.16. Multiple Inheritance with the FundTransfer class.
3.17. Multiple Inheritance with the CashSave class.
3.18. The Account class with static state example output.
3.19. The full working Account class.
3.20. The full working Account and CurrentAccount classes.
3.21. The incomplete Cashsave class example.
3.22. The full working CashSave class example.
3.23. The final working CashSave class example.
3.24. Friend Methods, Pointers to Objects and Destructors
3.25. A copy constructor example.
3.26. Destructors and Arrays Example.
3.27. The Person class.
3.28. The Person and Staff class hierarchy.
4.1. The Java Life Cycle
4.2. The Java 2 Platform Overview (Figure from java.sun.com)
4.3. Setting the Java Environment Variables.
4.4. Testing the JDK.
4.5. The Java API Documentation
6.1. STL Iterator Categories
7.1. HelloWorldCount Command Prompt
7.2. HelloWorldCount Applet
7.3. ChessBoard Applet
7.4. DrawRedLine Applet
7.5. HelloWorld Applet With Parameter Passing
7.6. HelloWorld Applet With Bad Parameter Passing
7.7. The ChessSolution applet with a square Size of 2, 10, 20 with a reversal in starting colour.
8.1. Image Display Applet
8.2. Mouse Audio Applet
8.3. Mouse Exercise Applet
9.1. Example GUI: Font Dialog
9.2. A Button Component
9.3. A Checkbox Component
9.4. A Radio Button Component
9.5. A Choice Button Component
9.6. A Label Component
9.7. A TextField Component
9.8. A Component Applet
9.9. A Flow Layout Applet
9.10. A Grid Layout Applet
9.11. A Border Layout Applet
9.12. Layout Exercise GUI
9.13. A List Component
9.14. A TextArea Component
9.15. A Scrollbar Component
9.16. Button Events Applet
9.17. Component Events
9.18. A Scrollbar Example Applet
9.19. The FontChooser Applet Exercise
9.20. The Canvas Applet Example
9.21. The Colour Chooser Applet Exercise
10.1. InetAddress Class Example
10.2. WhoIs Example
10.3. Finger Client Example
10.4. Finger Client/Server Process
10.5. The Basic Web Browser Application
10.6. The Date Client Creates a Connection to the Date Server.
10.7. The Date Server creates a HandleConnection object.
10.8. The Date Client passes the command to the HandleConnection.
10.9. The HandleConnection gets the Date/Time and sends it to the Client.
10.10. The HandleConnection shuts down. The DateServer starts listening again.
10.11. The DateServer running on "localhost"
10.12. The Date Client specifing a server at "localhost"
10.13. My Date Client Exercise Solution
10.14. My Date Server Exercise Solution
11.1. Bad Counter Example
11.2. Bad Counter Fixed Example
11.3. A Suspend/Resume Multi-Counter Example
11.4. My Up/Down Counter Solution
11.5. Priority Counter Example
11.6. A Non-Synchronized Gate Counter Example
11.7. A Non-Synchronized Gate Counter Example (Program Flow)
11.8. A Synchronized Gate Counter Example
11.9. A Synchronized Gate Counter Example (Program Flow)
11.10. A Random Draw Thread Exercise Screen Capture.
12.1. The Swing Set 2 Example 1 Application
12.2. The Swing Set 2 Example 2 Application
12.3. A Simple Swing Application
12.4. A Swing Image Button Example
12.5. The Swing Component Hierarchy
12.6. A Swing Exercise for You!
12.7. An Example of Swing ToolTips
12.8. An Example of Swing Borders
12.9. A Password Field Example
12.10. A JSlider Example
12.11. A JProgressBar Example
12.12. The JToggleButton component example (a) selected (b) not selected.
12.13. The Egg Timer Exercise
12.14. The JToolBar Example Application. Toolbar (a) at top (b) at bottom.
12.15. The JToolBar Example with Floating Toolbar
12.16. The JMenuBar Example
12.17. The JTabbedPane Example (a) first tab (b) second tab.
12.18. The JInternalFrame Example
12.19. The JSplitPane Example Application. Split (a) centre right (b) more right of centre.
12.20. The Dialog Example Application
12.21. The Message Dialog
12.22. The Input Dialog
12.23. The Confirm Dialog
12.24. The Input Dialog with Options
12.25. The MyDialog (modal and modeless look the same)
12.26. The JFileChooser Dialog in Action.
12.27. The JList Example Application
12.28. The JTree Example Application
12.29. The JTable Example Application. (a) As it appears on startup (b) moved and edited a bit
12.30. The JTable Example Application with ListSelection events.
12.31. The Image Loader Application
12.32. The Image Loader Application (with File Chooser open)
12.33. The Image Loader Application (with confirm on close)
13.1. Skeleton and stub communication
13.2. RMI Distributed Application
13.3. Screen Grab of the Request Server, Compilation and Execution.
13.4. Screen Grab of the RMI Cient Application, Compilation and Execution.
15.1. The ChessSolution applet with a square Size of 2, 10, 20 with a reversal in starting colour
15.2. Mouse Exercise Applet
15.3. The Colour Chooser Applet Exercise Solution
15.4. My Date Client Exercise Solution
15.5. My Date Server Exercise Solution
15.6. A Random Draw Thread Exercise Screen Capture - Solution.
15.7. The Image Loader Solution
15.8. The Image Loader Solution (with File Chooser)

List of Tables

2.1. The C String Functions Reference
2.2. The C++ String Methods
2.3. Character Constants Table
2.4. C++ Operator Precedence Table