ebook img

Object-Oriented Programming (OOP) with Java PDF

1770 Pages·2013·8.59 MB·English
by  
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Object-Oriented Programming (OOP) with Java

Object-Oriented Programming (OOP) with Java By: Richard Baldwin Object-Oriented Programming (OOP) with Java By: Richard Baldwin Online: < http://cnx.org/content/col11441/1.121/ > C O N N E X I O N S RiceUniversity,Houston,Texas ThisselectionandarrangementofcontentasacollectioniscopyrightedbyRichardBaldwin. Itislicensedunderthe Creative Commons Attribution 3.0 license (http://creativecommons.org/licenses/by/3.0/). Collection structure revised: May 7, 2013 PDF generated: May 28, 2013 For copyright and attribution information for the modules contained in this collection, see p. 1742. 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 ............................37 3.4 Ap0030: Self-assessment,RelationalOperators,IncrementOperator,andControl Structures ..................................................................................69 3.5 Ap0040: Self-assessment, Logical Operations, Numeric Casting, String Concate- nation, and the toString Method ............................................................93 3.6 Ap0050: Self-assessment, Escape Character Sequences and Arrays .........................112 3.7 Ap0060: Self-assessment, More on Arrays ..................................................140 3.8 Ap0070: Self-assessment, Method Overloading .............................................167 3.9 Ap0080: Self-assessment, Classes, Constructors, and Accessor Methods ....................183 3.10 Ap0090: Self-assessment, thesuperkeyword, (cid:28)nalkeyword, andstaticmethods............203 3.11 Ap0100: Self-assessment, The this keyword, static (cid:28)nal variables, and initializa- tion of instance variables ...................................................................223 3.12 Ap0110: Self-assessment, Extending classes, overriding methods, and polymor- phic behavior ..............................................................................245 3.13 Ap0120: Self-assessment, Interfaces and polymorphic behavior ............................266 3.14 Ap0130: Self-assessment, Comparing objects, packages, import directives, and some common exceptions ..................................................................300 3.15 Ap0140: Self-assessment, Type conversion, casting, common exceptions, public class (cid:28)les, javadoc comments and directives, and null references ............................320 4 Programming Fundamentals 4.1 Jb0103 Preface to Programming Fundamentals ............................................341 4.2 Jb0105: Java OOP: Similarities and Di(cid:27)erences between Java and C++ ...................342 4.3 Jb0110: Java OOP: Programming Fundamentals, Getting Started .........................346 4.4 Jb0110r Review ...........................................................................352 4.5 Jb0115: Java OOP: First Program .........................................................356 4.6 Jb0120: Java OOP: A Gentle Introduction to Java Programming ..........................361 4.7 Jb0120r Review ...........................................................................368 4.8 Jb0130: Java OOP: A Gentle Introduction to Methods in Java .............................372 4.9 Jb0130r Review ...........................................................................381 4.10 Jb0140: Java OOP: Java comments ......................................................386 4.11 Jb0140r Review ..........................................................................392 4.12 Jb0150: Java OOP: A Gentle Introduction to Java Data Types ...........................396 4.13 Jb0150r Review ..........................................................................410 4.14 Jb0160: Java OOP: Hello World ..........................................................418 4.15 Jb0160r Review ..........................................................................425 4.16 Jb0170: Java OOP: A little more information about classes. ..............................431 4.17 Jb0170r: Review .........................................................................434 4.18 Jb0180: Java OOP: The main method. ...................................................437 4.19 Jb0180r Review ..........................................................................441 4.20 Jb0190: Java OOP: Using the System and PrintStream Classes ...........................445 4.21 Jb0190r: Review .........................................................................449 iv 4.22 Jb0200: Java OOP: Variables ............................................................455 4.23 Jb0200r: Review .........................................................................471 4.24 Jb0210: Java OOP: Operators ............................................................484 4.25 Jb0210r Review ..........................................................................494 4.26 Jb0220: Java OOP: Statements and Expressions ..........................................511 4.27 Jb0220r Review ..........................................................................514 4.28 Jb0230: Java OOP: Flow of Control ......................................................518 4.29 Jb0230r Review ..........................................................................537 4.30 Jb0240: Java OOP: Arrays and Strings ...................................................545 4.31 Jb0240r Review ..........................................................................559 4.32 Jb0250: Java OOP: Brief Introduction to Exceptions .....................................568 4.33 Jb0260: Java OOP: Command-Line Arguments ..........................................571 4.34 Jb0260r Review ..........................................................................575 4.35 Jb0270: Java OOP: Packages .............................................................580 4.36 Jb0280: Java OOP: String and StringBu(cid:27)er ..............................................588 4.37 Jb0280r Review ..........................................................................599 4.38 Jb0290: The end of Programming Fundamentals .........................................608 5 ITSE 2321 Object-Oriented Programming (Java) 5.1 Jy0020: Java OOP: Preface to ITSE 2321 .................................................609 5.2 Essence of OOP ...........................................................................612 5.3 Multimedia ................................................................................815 5.4 The Java Collections Framework ..........................................................1188 5.5 Practice Programs ........................................................................1338 6 ITSE2317 - Java Programming (Intermediate) 6.1 Jy0030: Java OOP: Preface to ITSE 2317 ................................................1377 6.2 Essence of OOP ..........................................................................1378 6.3 Multimedia ...............................................................................1401 6.4 Practice Tests ............................................................................1672 7 GAME 2302 - Mathematical Applications for Game Development 7.1 Jy0040: GAME2302: Mathematical Applications for Game Development ..................1713 8 Anatomy of a Game Engine 8.1 Jy0060: Anatomy of a Game Engine ......................................................1715 9 Principles of Object-Oriented Programming 9.1 Jy0070-Principles of Object-Oriented Programming .......................................1717 10 Programming Oldies But Goodies 10.1 Jy0050: Programming Oldies But Goodies ..............................................1719 11 Appendices 11.1 Java OOP: Java Documentation .........................................................1721 Index ..............................................................................................1728 Attributions ......................................................................................1742 AvailableforfreeatConnexions<http://cnx.org/content/col11441/1.121> 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. 2) · The JDK and the JRE (p. 2) · The Java API documentation (p. 2) · A suitable text editor (p. 3) • Miscellaneous (p. 3) 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 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 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 three courses at Austin Community College in Austin Texas: • ITSE 2321 - Object-Oriented Programming (Java) 2 • ITSE2317 - Java Programming (Intermediate) 3 • GAME 2302 Mathematical Applications for Game Development 4 The collection also includes: • An OOP self-assessment test 5 1Thiscontentisavailableonlineat<http://cnx.org/content/m45136/1.9/>. 2http://cnx.org/content/m45222 3http://cnx.org/content/m45258 4http://cnx.org/content/m45315 5http://cnx.org/content/m45252 AvailableforfreeatConnexions<http://cnx.org/content/col11441/1.121> 1 2 CHAPTER 1. PREFACE • Course materials for a complete course in Programming Fundamentals 6 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 7 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. 1.1.3 Getting started with Java programming As is the case with most 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, youwill(cid:28)ndlinkstothatmaterialonthewebpagetitled JavaPlatformStandardEdition7Documentation 8 at http://docs.oracle.com/javase/7/docs/ (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 9 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 10 available at http://docs.oracle.com/javase/7/docs/webnotes/install/index.html Pay particular atten- tion 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 11 http://docs.oracle.com/javase/tutorial/essential/environment/paths.html 1.1.3.2 The Java API documentation The Java Platform, Standard Edition 7 API Speci(cid:28)cation is available at 12 http://docs.oracle.com/javase/7/docs/api/index.html 13 Also see my Java OOP documentation module for instructions on how to use the documentation. 6http://cnx.org/content/m45179 7http://cnx.org/ 8http://docs.oracle.com/javase/7/docs/ 9http://www.oracle.com/technetwork/java/javase/downloads/index.html 10http://docs.oracle.com/javase/7/docs/webnotes/install/index.html 11http://docs.oracle.com/javase/tutorial/essential/environment/paths.html 12http://docs.oracle.com/javase/7/docs/api/index.html 13http://cnx.org/content/m45117 AvailableforfreeatConnexions<http://cnx.org/content/col11441/1.121> 3 1.1.3.3 A suitable text editor 14 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 15 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.) 16 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 17 18 . 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. note: Housekeeping material • Module name: Jy0010: Preface to Object-Oriented Programming (OOP) with Java • File: Jy0010.htm • Published: 11/16/12 • Revised: 01/02/13 note: Disclaimers: Financial : Although the Connexions site makes it possible for you to download a PDF (cid:28)le for this module at no charge, and also makes it possible for you to purchase a pre-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- 14http://cnx.org/content/m45137 15http://www.jcreator.com/ 16http://drjava.sourceforge.net/ 17http://www.jgrasp.org/ 18http://www.bluej.org/ AvailableforfreeatConnexions<http://cnx.org/content/col11441/1.121> 4 CHAPTER 1. PREFACE AvailableforfreeatConnexions<http://cnx.org/content/col11441/1.121>

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.