Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Object-Oriented Programming (OOP) with Java
Nội dung xem thử
Mô tả chi tiết
Object-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.180/ >
OpenStax-CNX
This selection and arrangement of content as a collection is copyrighted by R.G. (Dick) Baldwin. It is licensed under
the Creative Commons Attribution License 4.0 (http://creativecommons.org/licenses/by/4.0/).
Collection structure revised: May 17, 2015
PDF generated: September 3, 2015
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 Objects First
2.1 Gf0100: Objects First with Greenfoot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3 OOP Self-Assessment
3.1 Ap0005: Preface to OOP Self-Assessment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . 11
3.2 Ap0010: Self-assessment, Primitive Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.3 Ap0020: Self-assessment, Assignment and Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.4 Ap0030: Self-assessment, Relational Operators, Increment Operator, and Control
Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
3.5 Ap0040: Self-assessment, Logical Operations, Numeric Casting, String Concatenation, and the toString Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
3.6 Ap0050: Self-assessment, Escape Character Sequences and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . 107
3.7 Ap0060: Self-assessment, More on Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . 135
3.8 Ap0070: Self-assessment, Method Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
3.9 Ap0080: Self-assessment, Classes, Constructors, and Accessor Methods . . . . . . . . . . . . . . . . . . . . 177
3.10 Ap0090: Self-assessment, the super keyword, nal keyword, and static methods . . . . . . . . . . . . 197
3.11 Ap0100: Self-assessment, The this keyword, static nal variables, and initialization of instance variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . 215
3.12 Ap0110: Self-assessment, Extending classes, overriding methods, and polymorphic behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
3.13 Ap0120: Self-assessment, Interfaces and polymorphic behavior . . . . . . . . . . . . . . . .. . . . . . . . . . . . 259
3.14 Ap0130: Self-assessment, Comparing objects, packages, import directives, and
some common exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
3.15 Ap0140: Self-assessment, Type conversion, casting, common exceptions, public
class les, javadoc comments and directives, and null references . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
4 Programming Fundamentals
4.1 Jb0103 Preface to Programming Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
4.2 Jb0105: Java OOP: Similarities and Dierences between Java and C++ . . . . . . . . . . . . . . . . . . . 337
4.3 Jb0110: Java OOP: Programming Fundamentals, Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . 341
4.4 Jb0110r Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
4.5 Jb0115: Java OOP: First Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . 351
4.6 Jb0120: Java OOP: A Gentle Introduction to Java Programming . . . . . . . . . . . . . . . . . . . . . . . . . . 355
4.7 Jb0120r Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
4.8 Jb0130: Java OOP: A Gentle Introduction to Methods in Java . . . . . . . . . . . . . . . . .. . . . . . . . . . . . 366
4.9 Jb0130r Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
4.10 Jb0140: Java OOP: Java comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
4.11 Jb0140r Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
4.12 Jb0150: Java OOP: A Gentle Introduction to Java Data Types . . . . . . . . . . . . . . .. . . . . . . . . . . . 390
4.13 Jb0150r Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
4.14 Jb0160: Java OOP: Hello World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . 411
4.15 Jb0160r Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
4.16 Jb0170: Java OOP: A little more information about classes. . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . 424
4.17 Jb0170r: Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
4.18 Jb0180: Java OOP: The main method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . 430
4.19 Jb0180r Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434
4.20 Jb0190: Java OOP: Using the System and PrintStream Classes . . . . . . . . . . . . . . .. . . . . . . . . . . . 438
4.21 Jb0190r: Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
iv
4.22 Jb0200: Java OOP: Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448
4.23 Jb0200r: Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462
4.24 Jb0210: Java OOP: Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . 474
4.25 Jb0210r Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484
4.26 Jb0220: Java OOP: Statements and Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . 501
4.27 Jb0220r Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504
4.28 Jb0230: Java OOP: Flow of Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . 508
4.29 Jb0230r Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524
4.30 Jb0240: Java OOP: Arrays and Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . 532
4.31 Jb0240r Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
4.32 Jb0250: Java OOP: Brief Introduction to Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553
4.33 Jb0260: Java OOP: Command-Line Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 556
4.34 Jb0260r Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 560
4.35 Jb0270: Java OOP: Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . 564
4.36 Jb0280: Java OOP: String and StringBuer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573
4.37 Jb0280r Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 584
4.38 Jb0290: The end of Programming Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593
5 ITSE 2321 Object-Oriented Programming (Java)
5.1 Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . 595
5.2 Essence of OOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598
5.3 Multimedia . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . 813
5.4 The Java Collections Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . 1398
5.5 Practice Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . 1676
6 ITSE2317 - Java Programming (Intermediate)
6.1 Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . 1759
6.2 Essence of OOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . 1761
6.3 Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . 1888
6.4 Event Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2015
6.5 JavaServer Pages (JSP) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2433
7 INEW 2338 Advanced Java (Web)
7.1 Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . 2977
7.2 Network Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2979
7.3 Search Engines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . 3092
7.4 Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3101
7.5 Frameworks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3274
8 GAME 2302 - Mathematical Applications for Game Development
8.1 Jy0040: GAME2302: Mathematical Applications for Game Development . . . . . . .. . . . . . . . . . . 3277
9 Anatomy of a Game Engine
9.1 Jy0060: Anatomy of a Game Engine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . 3279
10 Principles of Object-Oriented Programming
10.1 Jy0070-Principles of Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3281
11 Programming Oldies But Goodies
11.1 Jy0050: Programming Oldies But Goodies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3283
12 Appendices
12.1 Java OOP: Java Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . 3285
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3292
Attributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3310
Available for free at Connexions <http://cnx.org/content/col11441/1.180>
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. 4)
• Miscellaneous (p. 4)
1.1.2 Welcome
Welcome to my collection titled Object-Oriented Programming (OOP) with Java .
During the past eighteen years, I have published hundreds of Java and OOP programming tutorials on
a variety of dierent topics and websites. I have also developed the teaching materials for several dierent
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 new material that I am developing to create a freely downloadable E-book that covers Java/OOP
programming 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:
1This content is available online at <http://cnx.org/content/m45136/1.17/>.
2http://cnx.org/content/m45222
3http://cnx.org/content/m45258
4http://cnx.org/contents/6e817713-906c-4bc0-9874-3d189e81362a
5http://cnx.org/contents/486c946e-2ad2-4edb-92f7-d21a87063463
Available for free at Connexions <http://cnx.org/content/col11441/1.180>
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 nd what you need
today, come back and take another look in a week or two and you may 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 oer. You can also customize this material for use in your organized courses or for personal self
study.
Feedback is appreciated
And if you nd the material useful, I would like to hear more about how you are using it.
Legacy versus openstax presentation format Early in 2014, cnx.org 9 began a transition
from a legacy presentation format 10 to a new openstax 11 presentation format. As of August,
2015, some of the functionality of the legacy presentation format has not yet been ported to the
openstax presentation format.
If you nd yourself viewing one of my collections or modules in the openstax 12 presentation format
(the format with no menu on the left side of the page) and some of the links appear to be broken,
some of the images fail to display properly, etc., you may need to revert to the legacy presentation
format 13
.
For example, as of August 2015, if you open this collection 14 in the openstax format and attempt
to view the slides, you may nd that they don't display properly. (You may see a le not found
error or something similar. I expect this issue to be resolved in the future but I'm not sure when.)
However, if you open the same collection in the legacy presentation format 15 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 by
selecting a link that normally appears at the beginning of the page and reads something like the
following: "Note: You are viewing an old style version of this document. The new style version is
available here."
Terminology: The folks at cnx.org 16 have adopted some new terminology in conjunction with
this transition. For example, the entity that is referred to as a Collection in the 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 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
Collection in the legacy format as shown here 17 . However, when you open a Book in the
openstax format, you will actually land on the rst page in the book as shown here 18 . In other
words, a Book is not a "stand alone" entity in the openstax format.
6http://cnx.org/contents/e0e3e693-bf58-4deb-a991-bec64b89b355
7http://cnx.org/contents/a431f34d-e41b-4118-8452-c16b0635cdfd
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
Available for free at Connexions <http://cnx.org/content/col11441/1.180>
3
Although it can be tricky to accomplish, it is possible to view Modules and Pages outside the
context of the Collection or Book that contains them in both formats as shown here 19 and here 20
. This makes it possible to include a module in two or more collections or to include a page in two
or more Books. (Note that depending on the state of cookies on your browser, it may be necessary
for you to either clear out some cookies or to view the above links in a Firefox Private Window
or a Chrome Incognito Window to view them outside the context of the Collection or Book.)
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 tools that you 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
The JDK, the JRE, and the API documentation are all freely available from Oracle. As of December 2012,
you will nd links to that material on the web page titled Java Platform Standard Edition 7 Documentation
at http://docs.oracle.com/javase/7/docs/ 21 (The links given in this module may change as new versions of
Java are released, but newer versions shouldn't be too dicult to locate with a web search.)
Download
The JDK and the included JRE can be downloaded from http://www.oracle.com/technetwork/java/javase/downloads/index.html
22 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
You will also probably need to follow the installation instructions for your computer that are
available at http://docs.oracle.com/javase/7/docs/webnotes/install/index.html 23 Pay particular 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
http://docs.oracle.com/javase/tutorial/essential/environment/paths.html 24
1.1.3.2 The Java API documentation
The Java Platform, Standard Edition 7 API Specication is available at
http://docs.oracle.com/javase/7/docs/api/index.html 25 . You should be able to nd the documentation for later versions with a web search.
Also see my Java OOP documentation 26 module for instructions on how to use the documentation.
19https://legacy.cnx.org/content/m45222/?legacy=true
20http://cnx.org/contents/e86af109-f5f1-4de8-a8f0-be0ef2d87567
21http://docs.oracle.com/javase/7/docs/
22http://www.oracle.com/technetwork/java/javase/downloads/index.html
23http://docs.oracle.com/javase/7/docs/webnotes/install/index.html
24http://docs.oracle.com/javase/tutorial/essential/environment/paths.html
25http://docs.oracle.com/javase/7/docs/api/index.html
26http://cnx.org/contents/ba4f0233-f146-4057-86ea-3d442d898707
Available for free at Connexions <http://cnx.org/content/col11441/1.180>
4 CHAPTER 1. PREFACE
1.1.3.3 A suitable text editor
The module titled Jb0110: Java OOP: Programming Fundamentals, Getting Started 27 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 le name extension is .java. Something as simple as Windows Notepad or Windows WordPad would
probably be best for your rst few simple programs.
Soon, however, you will probably want to upgrade to an editor that uses dierent colors to identify the
dierent parts of your program. My favorite color-coded editor is the free version of JCreator 28
. (The
free version seems to have disappeared from their web page so you may have trouble nding it.)
Another free editor is DrJava 29 . 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
30 . Numerous other Java color-coded editors, including BlueJ 31 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: 08/20/15
Disclaimers: Financial : Although the Connexions site makes it possible for you to download
a PDF le for this module at no charge, and also makes it possible for you to purchase a pre-printed
version of the PDF le, you should be aware that some of the HTML elements in this module may
not translate well into PDF.
I also want you to know that I receive no nancial compensation from the Connexions website even
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.
Aliation : I am a professor of Computer Information Technology at Austin Community College
in Austin, TX.
-end27http://cnx.org/contents/920d091d-ea72-478f-986f-eab814c2d992
28http://www.jcreator.com/
29http://drjava.sourceforge.net/
30http://www.jgrasp.org/
31http://www.bluej.org/
Available for free at Connexions <http://cnx.org/content/col11441/1.180>
Chapter 2
Objects First
2.1 Gf0100: Objects First with Greenfoot1
2.1.1 Table of Contents
• Preface (p. 5)
• Overview (p. 6)
· A visual interactive programming environment (p. 6)
· Greenfoot is not a toy (p. 6)
· The Greenfoot gallery (p. 7)
• Software (p. 7)
· Software to download and install (p. 7)
· Stand alone software (p. 7)
• Running Greenfoot (p. 8)
· The installed version (p. 8)
· The stand alone version (p. 8)
• Textbook and tutorials (p. 8)
· The textbook (p. 8)
· The tutorials (p. 9)
* Written tutorials (p. 9)
* Video tutorials (p. 9)
* Other possibilities (p. 9)
• What comes next after Greenfoot (p. 10)
• Miscellaneous (p. 10)
2.1.2 Preface
If you are new to programming and want to learn how to program, or if you have programming experience
but are new to object-oriented programming (OOP) and want to learn OOP, I can't think of a better way
to take that rst step than through the use of Greenfoot 2
.
Greenfoot provides a set of interactive visual tools coupled with the Java programming language that
makes learning to program both interesting and productive.
(Note that descriptions of the Greenfoot website contained herein are current as of 02/19/13.)
1This content is available online at <http://cnx.org/content/m45790/1.2/>.
2http://www.greenfoot.org/door
Available for free at Connexions <http://cnx.org/content/col11441/1.180>
5
6 CHAPTER 2. OBJECTS FIRST
2.1.3 Overview
2.1.3.1 A visual interactive programming environment
The greenfoot overview 3 page describes Greenfoot as an Interactive Visual World that "...teaches object
orientation with Java."
When programming with Greenfoot, you "Create 'actors' which live in 'worlds' to build games, simulations, and other graphical programs."
Greenfoot is both instructive and enjoyable because it is visual and interactive. Visualization and interaction tools are built into the programming environment.
Unlike Scratch 4 and Alice 5
(two programming environments for beginners that use a drag-and-drop
approach to programming) , Greenfoot actors "are programmed in standard textual Java code, providing
a combination of programming experience in a traditional text-based language with visual execution."
According to Wikipedia 6
,
"Greenfoot is an interactive Java development environment designed primarily for educational purposes at the high school and undergraduate level. It allows easy development of two-dimensional graphical
applications, such as simulations and interactive games.
Greenfoot is being developed and maintained at the University of Kent 7 and La Trobe University 8
, with support from Oracle 9
. It is free software, released under the GPL license. Greenfoot is available
for Microsoft Windows 10 , Mac OS X 11 , Linux 12 , Sun Solaris 13 , and any recent JVM 14
."
Continuing with Wikipedia 15
,
"Greenfoot aims to motivate learners quickly by providing easy access to animated graphics, sound
and interaction. The environment is highly interactive and encourages exploration and experimentation.
Pedagogically, the design is based on constructivist and apprenticeship approaches.
Secondly, the environment is designed to illustrate and emphasize important abstractions and concepts
of object-oriented programming. Concepts such as the class/object relationship, methods, parameters, and
object interaction are conveyed through visualizations and guided interactions. The goal is to build and
support a mental model that correctly represents modern object-oriented programming systems."
2.1.3.2 Greenfoot is not a toy
Your rst impression when you enter the Greenfoot home page 16 may be that Greenfoot is a toy, but that
denitely is not the case.
While it is true that the rst impulse of many beginning programmers is to create computer games (as
evidenced on the home page 17 and the Greenfoot Gallery 18 ), Greenfoot can also be used to create
serious interactive simulations. As an example, I will point you to the following simulations:
• Ants 19 by mik
3http://www.greenfoot.org/overview
4http://scratch.mit.edu/
5http://www.alice.org/
6http://en.wikipedia.org/wiki/Greenfoot
7http://en.wikipedia.org/wiki/University_of_Kent
8http://en.wikipedia.org/wiki/La_Trobe_University
9http://en.wikipedia.org/wiki/Oracle
10http://en.wikipedia.org/wiki/Microsoft_Windows
11http://en.wikipedia.org/wiki/Mac_OS_X
12http://en.wikipedia.org/wiki/Linux
13http://en.wikipedia.org/wiki/Sun_Solaris
14http://en.wikipedia.org/wiki/Java_Virtual_Machine
15http://en.wikipedia.org/wiki/Greenfoot
16http://www.greenfoot.org/home
17http://www.greenfoot.org/home
18http://www.greenfoot.org/scenarios
19http://www.greenfoot.org/scenarios/1016
Available for free at Connexions <http://cnx.org/content/col11441/1.180>
7
• Wave-Lab 20 by delmar
• Birds and Trees 21 by polle
(Click a link given above to download and run a simulation. When the simulation window appears, click
the Run button on the simulation window to start the simulation running.)
You can view more than 7500 scenarios (Greenfoot programs are commonly referred to as scenarios) by
clicking the Scenarios link at the top of the home page 22 . Some of the scenarios are impressive. Some
are not so impressive. Many, possibly most, were written by beginning programmers.
(Note that you must have Java applets enabled on your computer to run these simulations. Alternatively,
if you have Greenfoot installed on your computer, you can click the "Open in Greenfoot" button to cause
the program to be downloaded for compilation and execution locally on your computer.)
2.1.3.3 The Greenfoot Gallery
What you see when you click the Scenarios link at the top of the home page 23 has been called the
Greenfoot Gallery 24 in times past. This is a place where the authors of Greenfoot scenarios can publish
their scenarios if they so choose.
This is a social network or virtual programming community where Greenfoot programmers gather to
encourage one another and to critique the work being done by themselves and others. Note however that
active participation in the gallery is completely voluntary.
2.1.4 Software
The Greenfoot software and the Java Development Kit (JDK), both of which run on Windows, Mac, and
Linux, are available for free download 25
.
2.1.4.1 Software to download and install
When you visit the download 26 page, you will nd download links for Windows, Mac OS X, and Ubuntu.
Click one of those links to download and install the appropriate version of Greenfoot on your computer.
You will also need to download and install the Java Development Kit (JDK). There is a download button
labeled Download JDK for that purpose.
2.1.4.2 Stand alone software
As an alternative to the version that can be installed on your computer, there is a self-contained Stand
Alone version that you can download, unzip, copy to, and run either from a disk folder or from a USB
memory stick with no other installation required. This version contains both the Greenfoot software and
the Java JDK.
Once you extract the software from the zip le, you will need about 200 Mbytes to store it in a disk
folder. Depending on the formatting, you may need as much as 275 Mbytes of available space to copy the
software to a USB memory stick.
The stand alone version is particularly useful in several situations including the following:
• You want to run Greenfoot on a computer for which you don't have installation privileges, such as in
a public library or a college computer lab.
20http://www.greenfoot.org/scenarios/597
21http://www.greenfoot.org/scenarios/267
22http://www.greenfoot.org/home
23http://www.greenfoot.org/home
24http://www.greenfoot.org/scenarios
25http://www.greenfoot.org/download
26http://www.greenfoot.org/download
Available for free at Connexions <http://cnx.org/content/col11441/1.180>
8 CHAPTER 2. OBJECTS FIRST
• You don't already have the Java JDK installed on your computer and would prefer not to go through
the eort to download and install it.
• You want to run Greenfoot, but you don't want to install anything on your computer.
2.1.5 Running Greenfoot
2.1.5.1 The installed version
If you download and install Greenfoot and the Java JDK on your computer, you will have an opportunity to
place a Greenfoot icon on your desktop. Just double-click the icon to start the Greenfoot program running.
(Although not necessary, it is probably best to install the JDK before installing Greenfoot.)
2.1.5.2 The stand alone version
If you elect to use the stand alone version, you will extract the following folders from the zip le:
• BlueJ 27 (a more advanced Java programming environment)
• Greenfoot (the programming environment of interest in this module)
• jdk (the Java Development Kit)
• userhome (not sure the purpose of this folder)
You will nd a le named Greenfoot.exe inside the Greenfoot folder. It will have an icon of a small
green footprint. Double click this le to start the Greenfoot program running.
Once the program is running, you can select the Greenfoot Tutorial item on the Help menu to open
a webpage containing a variety of educational tutorials. (I will have more to say about this later.) The
Help menu also provides access to several other useful items, such as documentation for both Greenfoot
and the Java JDK.
2.1.6 Textbook and tutorials
There is a textbook titled Introduction to Programming with Greenfoot - Object-Oriented Programming in
Java with Games and Simulations 28 that you can purchase on Amazon for about $75.00. (As usual, you
can rent it for less or purchase a used copy for less.)
There are also many free online tutorials 29 available to help you get started programming with Java and
Greenfoot.
Whether you need the textbook, or whether the tutorials will suce will depend on your background.
My advice is to begin with the tutorials alone, and then purchase the textbook if needed.
2.1.6.1 The textbook
That having been said, I will comment on the textbook. If I had the luxury of designing a computer
programming curriculum from scratch, I would choose a model very similar to the Greenfoot textbook.
In particular, unlike the programming curricula in many colleges and universities, I would begin with
the big picture (classes, interfaces, objects, methods, variables, etc.) and work my way down to the more
detailed aspects of programming (selection, loops, operators, types, etc.) .
(The programming curricula at many colleges and universities begin at the bottom and work their way
up instead of beginning at the top and working their way down. As a result, many students become bogged
down in details and either quit or fail and never get a chance to see the big picture of object-oriented
programming.)
27http://www.bluej.org/
28http://www.greenfoot.org/book
29http://www.greenfoot.org/doc
Available for free at Connexions <http://cnx.org/content/col11441/1.180>
9
Because I believe in the top down model on which the textbook is based, I can recommend this textbook as
a very good way to get started learning computer programming in general and object-oriented programming
in particular. (I am not aliated with the author or the publisher and receive no compensation from the
sale of this textbook.)
2.1.6.2 The tutorials
2.1.6.2.1 Written tutorials
As of 02/19/13, the Greenfoot website provides the following written tutorials to help you learn how to use
Greenfoot and begin programming:
• Interacting with Greenfoot 30
• Movement and Key Control 31
• Detecting and Removing Actors, and Making Methods 32
• Saving the World, Making and Playing Sound 33
• Adding a Randomly Moving Enemy 34
• How to Access One Object From Another 35
2.1.6.2.2 Video tutorials
If you prefer video tutorials, the Joy Of Code 36 is a thorough introduction to Greenfoot broken down into
a large number of videos. A wide range of other short videos 37 are also available.
2.1.6.2.3 Other possibilities
Once you become an accomplished Greenfoot programmer, you might be interested in some of the following
possibilities, which generally require other resources or more advanced knowledge:
• Kinect with Greenfoot 38
.
• PicoBoard with Greenfoot 39
.
• Sense Board with Greenfoot 40
.
• Finch with Greenfoot 41
.
• Loading native libraries with Greenfoot 42
.
• Gamepads with Greenfoot 43
.
• CS Unplugged with Greenfoot 44
.
• AP Computer Science with Greenfoot 45
.
• Learn Maths with Greenfoot 46
30http://www.greenfoot.org/doc/tut-1
31http://www.greenfoot.org/doc/tut-2
32http://www.greenfoot.org/doc/tut-3
33http://www.greenfoot.org/doc/tut-4
34http://www.greenfoot.org/doc/tut-5
35http://www.greenfoot.org/doc/howto-1
36http://www.joyofcode.org/
37http://www.youtube.com/user/18km?ob=5#g/u
38http://www.greenfoot.org/doc/kinect
39http://www.greenfoot.org/doc/pico
40http://www.greenfoot.org/doc/sense
41http://www.greenfoot.org/doc/nch
42http://www.greenfoot.org/doc/native_loader
43http://www.greenfoot.org/doc/gamepad
44http://www.greenfoot.org/doc/csunplugged
45http://www.greenfoot.org/doc/ap
46http://sinepost.wordpress.com/
Available for free at Connexions <http://cnx.org/content/col11441/1.180>