Table Of ContentObject-Oriented Programming (OOP) with
Java
Collection Editor:
R.G. (Dick) Baldwin
Object-Oriented Programming (OOP) with
Java
Collection Editor:
R.G. (Dick) Baldwin
Authors:
R.G. (Dick) Baldwin
R.L. Martinez, PhD
Online:
< http://cnx.org/content/col11441/1.181/ >
OpenStax-CNX
ThisselectionandarrangementofcontentasacollectioniscopyrightedbyR.G.(Dick)Baldwin. Itislicensedunder
the Creative Commons Attribution License 4.0 (http://creativecommons.org/licenses/by/4.0/).
Collection structure revised: September 3, 2015
PDF generated: January 9, 2016
For copyright and attribution information for the modules contained in this collection, see p. 3310.
Table of Contents
1 Preface
1.1 Jy0010: Preface to OOP with Java ..........................................................1
2 Programming Fundamentals
2.1 Jb0103 Preface to Programming Fundamentals ..............................................5
2.2 Jb0105: Java OOP: Similarities and Di(cid:27)erences between Java and C++ .....................7
2.3 Jb0110: Java OOP: Programming Fundamentals, Getting Started ..........................11
2.4 Jb0110r Review ............................................................................17
2.5 Jb0115: Java OOP: First Program ..........................................................21
2.6 Jb0120: Java OOP: A Gentle Introduction to Java Programming ...........................25
2.7 Jb0120r Review ............................................................................32
2.8 Jb0130: Java OOP: A Gentle Introduction to Methods in Java ..............................36
2.9 Jb0130r Review ............................................................................45
2.10 Jb0140: Java OOP: Java comments .......................................................50
2.11 Jb0140r Review ...........................................................................56
2.12 Jb0150: Java OOP: A Gentle Introduction to Java Data Types ............................60
2.13 Jb0150r Review ...........................................................................73
2.14 Jb0160: Java OOP: Hello World ...........................................................81
2.15 Jb0160r Review ...........................................................................88
2.16 Jb0170: Java OOP: A little more information about classes. ...............................94
2.17 Jb0170r: Review ..........................................................................97
2.18 Jb0180: Java OOP: The main method. ...................................................100
2.19 Jb0180r Review ..........................................................................104
2.20 Jb0190: Java OOP: Using the System and PrintStream Classes ...........................108
2.21 Jb0190r: Review .........................................................................112
2.22 Jb0200: Java OOP: Variables ............................................................118
2.23 Jb0200r: Review .........................................................................132
2.24 Jb0210: Java OOP: Operators ............................................................144
2.25 Jb0210r Review ..........................................................................154
2.26 Jb0220: Java OOP: Statements and Expressions ..........................................171
2.27 Jb0220r Review ..........................................................................174
2.28 Jb0230: Java OOP: Flow of Control ......................................................178
2.29 Jb0230r Review ..........................................................................194
2.30 Jb0240: Java OOP: Arrays and Strings ...................................................202
2.31 Jb0240r Review ..........................................................................215
2.32 Jb0250: Java OOP: Brief Introduction to Exceptions .....................................223
2.33 Jb0260: Java OOP: Command-Line Arguments ..........................................226
2.34 Jb0260r Review ..........................................................................230
2.35 Jb0270: Java OOP: Packages .............................................................234
2.36 Jb0280: Java OOP: String and StringBu(cid:27)er ..............................................243
2.37 Jb0280r Review ..........................................................................254
2.38 Jb0290: The end of Programming Fundamentals .........................................263
3 ITSE 2321 Object-Oriented Programming (Java)
3.1 Preface ....................................................................................265
3.2 Essence of OOP ...........................................................................268
3.3 Multimedia ................................................................................482
3.4 The Java Collections Framework ..........................................................1067
3.5 Practice Programs ........................................................................1345
4 ITSE2317 - Java Programming (Intermediate)
iv
4.1 Preface ...................................................................................1427
4.2 Essence of OOP ..........................................................................1429
4.3 Generics ..................................................................................1556
4.4 Event Handling ..........................................................................1683
4.5 JavaServer Pages (JSP) ..................................................................2101
5 INEW 2338 Advanced Java (Web)
5.1 Preface ...................................................................................2645
5.2 Network Programming ...................................................................2647
5.3 Search Engines ...........................................................................2760
5.4 Servlets ..................................................................................2769
5.5 Frameworks ..............................................................................2942
6 GAME 2302 - Mathematical Applications for Game Development
6.1 Jy0040: GAME2302: Mathematical Applications for Game Development ..................2945
7 OOP Self-Assessment
7.1 Ap0005: Preface to OOP Self-Assessment .................................................2947
7.2 Ap0010: Self-assessment, Primitive Types ................................................2948
7.3 Ap0020: Self-assessment, Assignment and Arithmetic Operators ..........................2970
7.4 Ap0030: Self-assessment,RelationalOperators,IncrementOperator,andControl
Structures ................................................................................2998
7.5 Ap0040: Self-assessment, Logical Operations, Numeric Casting, String Concate-
nation, and the toString Method ..........................................................3024
7.6 Ap0050: Self-assessment, Escape Character Sequences and Arrays ........................3044
7.7 Ap0060: Self-assessment, More on Arrays .................................................3072
7.8 Ap0070: Self-assessment, Method Overloading ............................................3097
7.9 Ap0080: Self-assessment, Classes, Constructors, and Accessor Methods ...................3114
7.10 Ap0090: Self-assessment, thesuperkeyword, (cid:28)nalkeyword, andstaticmethods...........3134
7.11 Ap0100: Self-assessment, The this keyword, static (cid:28)nal variables, and initializa-
tion of instance variables ..................................................................3152
7.12 Ap0110: Self-assessment, Extending classes, overriding methods, and polymor-
phic behavior .............................................................................3175
7.13 Ap0120: Self-assessment, Interfaces and polymorphic behavior ...........................3196
7.14 Ap0130: Self-assessment, Comparing objects, packages, import directives, and
some common exceptions .................................................................3230
7.15 Ap0140: Self-assessment, Type conversion, casting, common exceptions, public
class (cid:28)les, javadoc comments and directives, and null references ...........................3250
8 Anatomy of a Game Engine
8.1 Jy0060: Anatomy of a Game Engine ......................................................3273
9 Principles of Object-Oriented Programming
9.1 Jy0070-Principles of Object-Oriented Programming .......................................3275
10 Programming Oldies But Goodies
10.1 Jy0050: Programming Oldies But Goodies ..............................................3277
11 Objects First
11.1 Gf0100: Objects First with Greenfoot ...................................................3279
12 Appendices
12.1 Java OOP: Java Documentation .........................................................3285
Index ..............................................................................................3292
Attributions ......................................................................................3310
AvailableforfreeatConnexions<http://cnx.org/content/col11441/1.181>
Chapter 1
Preface
1.1 Jy0010: Preface to OOP with Java1
1.1.1 Table of Contents
• Welcome (p. 1)
• Getting started with Java programming (p. 3)
· The JDK and the JRE (p. 3)
· The Java API documentation (p. 3)
· A suitable text editor (p. 3)
• Miscellaneous (p. 4)
1.1.2 Welcome
Welcome to my collection titled Object-Oriented Programming (OOP) with Java .
During the past nineteen years, I have published hundreds of Java and OOP programming tutorials on
a variety of di(cid:27)erent topics and websites. I have also developed the teaching materials for several di(cid:27)erent
college-level programming courses in Java/OOP.
A work in progress
This is a work in progress. I am currently combining selected content from those earlier endeavors with
newmaterialthatIamdevelopingtocreateafreelydownloadableBookthatcoversJava/OOPprogramming
from programming fundamentals to very advanced OOP concepts.
Among other things, the collection contains the material that I use to teach the following courses at
Austin Community College in Austin Texas:
• ITSE 2321 - Object-Oriented Programming (Java) 2
• ITSE2317 - Java Programming (Intermediate) 3
• INEW2338 - Advanced Java Programming 4
• GAME 2302 Mathematical Applications for Game Development 5
The collection also includes:
1Thiscontentisavailableonlineat<http://cnx.org/content/m45136/1.18/>.
2http://cnx.org/contents/-2RmHFs_:6GrxCfXx
3http://cnx.org/contents/-2RmHFs_:JVTd7bX1
4http://cnx.org/contents/-2RmHFs_:boF3E5Bs
5http://cnx.org/contents/Ki_nRUo6:SGyUbirS
AvailableforfreeatConnexions<http://cnx.org/content/col11441/1.181>
1
2 CHAPTER 1. PREFACE
• An OOP self-assessment test 6
• Course materials for a complete course in Programming Fundamentals 7
Because it is a work in progress, the collection is growing on a daily basis. If you don’t (cid:28)nd what you need
today, come back and take another look in a week or two and you may (cid:28)nd what you need then.
Download options
I encourage you to take advantage of all of the download options (most of which are free) that cnx.org
8
has to o(cid:27)er. You can also customize this material for use in your organized courses or for personal self
study.
Feedback is appreciated
And if you (cid:28)nd the material useful, I would like to hear more about how you are using it.
9
Legacy versus openstax presentation format Early in 2014, cnx.org began a transition
10 11
from a legacy presentation format to a new openstax presentation format. As of January 8,
2016, some of the functionality of the legacy presentation format has not yet been ported to the
openstax presentation format.
12
Ifyou(cid:28)ndyourselfviewingoneofmycollectionsormodulesintheopenstax presentationformat
and some of the links appear to be broken, some of the images fail to display properly, etc., you
13
may need to revert to the legacy presentation format .
14
For example, as of January 2016, if you open this Book in the openstax format and attempt
to view the slides, you may (cid:28)nd that they don’t display properly. (You may see a (cid:28)le not found
error or something similar. I expect this issue to be resolved in the future but I’m not sure when.)
15
However, if you open the same collection in the legacy presentation format and attempt to view
the slides, you should see that the slides display as expected.
You can switch from openstax format to legacy format by selecting the Legacy Site link
in the upper-right corner of the page when viewing a collection or module in openstax format.
When you are viewing a module in the legacy format, you can switch to the openstax format
byselectingalinkthatnormallyappearsatthebeginningofthepageandreadssomethinglikethe
following: "Note: You are viewing an old style version of this document. The new style version is
available here."
16
Terminology: The folks at cnx.org have adopted some new terminology in conjunction with
thistransition. Forexample,theentitythatisreferredtoasa Collection inthe legacy format
is referred to as a Book in the openstax format. The entity that is referred to as a Module
in the legacy format is referred to as a Page in the openstax format. You will probably (cid:28)nd
that I use these terms somewhat interchangeably in this material.
The landing module or page: It is possible to open and view material at the level of a
17
Collection in the legacy format as shown here . However, (with some exceptions) when
you open a Book in the openstax format, you will actually land on the (cid:28)rst Page in the
18
book as shown here . In other words, a Book is not a "stand alone" entity in the openstax
format.
6http://cnx.org/contents/-2RmHFs_:4OPmk79Y
7http://cnx.org/contents/-2RmHFs_:pDHzTeQb
8http://cnx.org/
9http://cnx.org/
10https://legacy.cnx.org/content/col11441/latest/
11http://cnx.org/contents/fb64661c-5b3f-4ea8-97c6-e48df112438a
12http://cnx.org/contents/fb64661c-5b3f-4ea8-97c6-e48df112438a
13https://legacy.cnx.org/content/col11441/latest/
14http://cnx.org/contents/fb64661c-5b3f-4ea8-97c6-e48df112438a
15https://legacy.cnx.org/content/col11441/latest/
16http://cnx.org/
17https://legacy.cnx.org/content/col11441
18http://cnx.org/contents/fb64661c-5b3f-4ea8-97c6-e48df112438a
AvailableforfreeatConnexions<http://cnx.org/content/col11441/1.181>
3
1.1.3 Getting started with Java programming
As is the case with many worthwhile endeavors, Java programming requires that you have some tools to
begin. Fortunately, all of the software tools that you will need to get started programming in Java are
available for free downloading.
In addition to a computer with web access, you will need:
• The Java Development Kit (JDK) and Java Runtime Engine (JRE)
• The Java API documentation
• A suitable text editor
1.1.3.1 The JDK and the JRE
TheJDK,theJRE,andtheAPIdocumentationareallfreelyavailablefromOracle. AsofJanuary2016,you
will(cid:28)ndlinkstothedocumentationonthewebpagetitled JavaPlatformStandardEdition8Documentation
19
. (The links given in this module may change as new versions of Java are released, but newer versions
shouldn’t be too di(cid:30)cult to locate with a web search.)
Download
TheJDKandtheincludedJREcanbedownloadedfromhttp://www.oracle.com/technetwork/java/javase/downloads/index.html
20
That page provides several download options. Beginners should download the Java Platform (JDK)
for the latest released version. (The JRE is included in the JDK package, so you don’t need to download
both.)
Install
21
You will also probably need to follow the installation instructions for your computer. Pay partic-
ular attention to the instructions for setting the path and classpath environment variables. This
is where many students stumble. Another useful document on the path and classpath is available at
22
http://docs.oracle.com/javase/tutorial/essential/environment/paths.html
1.1.3.2 The Java API documentation
23
The Java Platform, Standard Edition 8 API Speci(cid:28)cation is available here . You should be able to (cid:28)nd
the documentation for later versions with a web search.
24
Also see my Java OOP documentation module for instructions on how to use the documentation.
1.1.3.3 A suitable text editor
25
The module titled Jb0110: Java OOP: Programming Fundamentals, Getting Started explains how to use
a text editor to create Java program code. Just about any text editor will do as long as you can ensure that
the (cid:28)le name extension is .java. Something as simple as Windows Notepad or Windows WordPad would
probably be best for your (cid:28)rst few simple programs.
Soon, however, you will probably want to upgrade to an editor that uses di(cid:27)erent colors to identify the
26
di(cid:27)erent parts of your program. My favorite color-coded editor is the free version of JCreator . (The
free version seems to have disappeared from their web page so you may have trouble (cid:28)nding it.)
27
Another free editor is DrJava . An advantage of this editor is that it can be run from a USB drive
with no installation required. Another possibility, although I have never had occasion to use it, is jGRASP
19http://docs.oracle.com/javase/8/docs/
20http://www.oracle.com/technetwork/java/javase/downloads/index.html
21http://docs.oracle.com/javase/8/docs/technotes/guides/install/install_overview.html
22http://docs.oracle.com/javase/tutorial/essential/environment/paths.html
23http://docs.oracle.com/javase/8/docs/api/index.html
24http://cnx.org/contents/-2RmHFs_:uk8CM_FG
25http://cnx.org/contents/-2RmHFs_:kg0JHepy
26http://www.jcreator.com/
27http://drjava.sourceforge.net/
AvailableforfreeatConnexions<http://cnx.org/content/col11441/1.181>
4 CHAPTER 1. PREFACE
28 29
. Numerous other Java color-coded editors, including BlueJ are available for free downloading on the
web.
1.1.4 Miscellaneous
This section contains a variety of miscellaneous information.
Housekeeping material
• Module name: Jy0010: Preface to Object-Oriented Programming (OOP) with Java
• File: Jy0010.htm
• Published: 11/16/12
• Revised: 01/08/16
Disclaimers: Financial : AlthoughtheConnexionssitemakesitpossibleforyoutodownload
aPDF(cid:28)leforthismoduleatnocharge,andalsomakesitpossibleforyoutopurchaseapre-printed
version of the PDF (cid:28)le, you should be aware that some of the HTML elements in this module may
not translate well into PDF.
IalsowantyoutoknowthatIreceiveno(cid:28)nancialcompensationfromtheConnexionswebsiteeven
if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle
books, and placed them for sale on Amazon.com showing me as the author. I neither receive
compensation for those sales nor do I know who does receive compensation. If you purchase such
a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it
was made and published without my prior knowledge.
A(cid:30)liation : IamaprofessorofComputerInformationTechnologyatAustinCommunityCollege
in Austin, TX.
-end-
28http://www.jgrasp.org/
29http://www.bluej.org/
AvailableforfreeatConnexions<http://cnx.org/content/col11441/1.181>