cnx.orgTable of Contents 1 Preface 1.1 Jy0020: Jaav OOP: Preface to ITSE 2321...

992
ITSE 2321 - Object-Oriented Programming (Java) By: R.G. (Dick) Baldwin

Transcript of cnx.orgTable of Contents 1 Preface 1.1 Jy0020: Jaav OOP: Preface to ITSE 2321...

  • ITSE 2321 - Object-Oriented Programming(Java)

    By:R.G. (Dick) Baldwin

  • ITSE 2321 - Object-Oriented Programming(Java)

    By:R.G. (Dick) Baldwin

    Online:< http://cnx.org/content/col11985/1.10/ >

    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: November 26, 2016

    PDF generated: November 27, 2016

    For copyright and attribution information for the modules contained in this collection, see p. 978.

  • Table of Contents

    1 Preface1.1 Jy0020: Java OOP: Preface to ITSE 2321 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.2 Java1554 Completing the First Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51.3 Java 1560: Con�guring Your Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131.4 Java1562: Email Noti�cation of Ebook Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

    2 Essence of OOP2.1 Java1600: Objects and Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . 312.2 Java1602: Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392.3 Java1604: Inheritance, Part 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492.4 Java1606: Inheritance, Part 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 612.5 Java1608: Polymorphism Based on Overloaded Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 702.6 Java1610: Polymorphism, Type Conversion, Casting, etc. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 782.7 Java1612: Runtime Polymorphism through Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 882.8 Java1614: Polymorphism and the Object Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 972.9 Java1616: Polymorphism and Interfaces, Part 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1062.10 Java1618: Polymorphism and Interfaces, Part 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1172.11 Java1620: Static Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1302.12 Java1622: Array Objects, Part 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1472.13 Java1624: Array Objects, Part 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1582.14 Java1626: Array Objects, Part 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1812.15 Java1628: The this and super Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1932.16 Java1630: Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209

    3 Multimedia3.1 Java3000: The Guzdial-Ericson Multimedia Class Library . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . 2353.2 Java3000r Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2443.3 Java3001: An Alternative Graphics Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2513.4 Java3002: Creating and Manipulating Turtles and Pictures in a World Object . . . . . . . . . . . . . 2543.5 Java3002r Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2753.6 Java3003: Drawing Graphs with Turtles and Pixels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2933.7 Java3004: Image Processing Algorithms, Image Inversion, and PictureExplorer

    Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3353.8 Java3004r Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3533.9 Java3006: Implementing a space-wise linear color-modi�cation algorithm. . . . . . . . . . . . . . . . . . . 3613.10 Java3006r Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3733.11 Java3008: Abstract Methods, Abstract Classes, and Overridden Methods . . . . . . . . . . . . . . . . . 3803.12 Java3008r Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3903.13 Java3010: Indirection, Array Objects, and Casting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3973.14 Java3010r Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4073.15 Java3012: Using Nested Loops to Process Pixels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . 4163.16 Java3012r Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4293.17 Java3014: Cropping, Flipping, and Combining Pictures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4363.18 Java3014r Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4533.19 Java3016: Green-Screen Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4603.20 Java3016r Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4773.21 Java3018: Darkening, Brightening, and Tinting the Colors in a Picture . . . . . . . . . . . . . . . . . . . 4873.22 Java3018r Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5063.23 Java3020: Interfaces, Object Arrays, etc. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5153.24 Java3022: Scaling, Rotating, and Translating Images using A�ne Transforms .. . . . . . . . . . . . 530

  • iv

    3.25 Java3022r Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5463.26 Java3024: Mirroring Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5533.27 Java3024r Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5683.28 Java3026: GradientPaint and other Java2D Classes . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . 5753.29 Java3026r Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5923.30 Java3028: Clipping Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5983.31 Java3028r Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6093.32 Java3030: Merging Pictures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6163.33 Java3030r Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 627

    4 The Java Collections Framework4.1 Java4010: Getting Started with Java Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6374.2 Java4010r Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6484.3 Java4020: What is a Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6564.4 Java4020r Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6624.5 Java4030: Purpose of Framework Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . 6674.6 Java4030r Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6734.7 Java4040: Purpose of Framework Implementations and Algorithms . . . . . . . . . . . . . . . . . . . . . . . . 6804.8 Java4040r Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6914.9 Java4050: Core Collection Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7004.10 Java4050r: Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7104.11 Java4060: Duplicate Elements, Ordered Collections, Sorted Collections, and In-

    terface Specialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7184.12 Java4060r: Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7254.13 Java4070: The Comparable Interface, Part 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7334.14 Java4070r: Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7474.15 Java4080: The Comparable Interface, Part 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7584.16 Java4080r: Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7704.17 Java4090: The Comparator Interface, Part 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7774.18 Java4090r: Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7904.19 Java4100: The Comparator Interface, Part 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7964.20 Java4100r: Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8074.21 Java4110: The Comparator Interface, Part 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8124.22 Java4110r: Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8204.23 Java4120: The Comparator Interface, Part 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8254.24 Java4120r: Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8394.25 Java4130: The Comparator Interface, Part 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8464.26 Java4130r: Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8554.27 Java4140: The Comparator Interface, Part 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8614.28 Java4140r: Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8714.29 Java4150: The toArray Method, Part 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . 8784.30 Java4150r: Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8904.31 Java4160: The toArray Method, Part 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . 8964.32 Java4160r: Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 911

    5 Practice Programs

    5.1 Java OOP: ITSE 2321 Practice Group 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9205.2 Java OOP: ITSE 2321 Practice Group 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9345.3 Java OOP: ITSE 2321 Practice Group 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 946

    6 Java Documentation6.1 Java3140 Java OOP Java Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 959

    Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 971

    Available for free at Connexions

  • v

    Attributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .978

    Available for free at Connexions

  • vi

    Available for free at Connexions

  • Chapter 1

    Preface

    1.1 Jy0020: Java OOP: Preface to ITSE 23211

    Revised: Tue Oct 04 15:00:17 CDT 2016This page is included in the following Books:

    • ITSE 2321 - Object-Oriented Programming (Java) 2• Object-Oriented Programming (OOP) with Java 3

    1.1.1 Table of contents

    • Table of contents (p. 1)• Welcome (p. 2)• Course description (p. 2)• Course prerequisite (p. 2)

    · Prerequisite waiver (p. 2)• Prior to enrolling (p. 2)• Course material (p. 3)

    · Preface (p. 3)* Preface to ITSE 2321 (p. 3)* Completing the First Assignment (p. 3)* Con�guring Your Computer (p. 3)

    · Essence of OOP (p. 3)· Multimedia (p. 4)· The Java Collections Framework (p. 4)· Practice Programs (p. 4)· Java Documentation (p. 4)

    • Downloads (p. 4)• Miscellaneous (p. 5)

    1This content is available online at .2http://cnx.org/contents/dzOvxPFw3http://cnx.org/contents/-2RmHFs_

    Available for free at Connexions

    1

  • 2 CHAPTER 1. PREFACE

    1.1.2 Welcome

    Welcome to the course material for ITSE 2321 - Object-Oriented Programming (Java) , which Iteach at Austin Community College 4 in Austin, TX.

    The college website for this course is: http://www.austincc.edu/baldwin/ 5

    1.1.3 Course description

    As of November 2012, the description for this course reads:"ITSE 2321 - Object-Oriented Programming (Java)Introduction to object-oriented programming. Emphasis on the fundamentals of structured design

    with classes, including development, testing, implementation, and documentation. Includes object-orientedprogramming techniques, classes, and objects."

    1.1.4 Course prerequisite

    The prerequisite for the course is COSC 1336 or department approval.As of November 2012, the description for the prerequisite course reads:"COSC 1336 - Programming Fundamentals IIntroduces the fundamental concepts of structured programming. Topics include software development

    methodology, data types, control structures, functions, arrays, and the mechanics of running, testing, anddebugging. This course assumes computer literacy. This course requires the same math skills necessary forCollege Algebra. Students should either have taken or be currently enrolled in College Algebra or a coursethat requires College Algebra."

    1.1.4.1 Prerequisite waiver

    Beginning in August of 2013, you might want to petition the department head for a waiver of the prerequisitecourse if you meet the following requirements:

    • You understand and can answer at least 80-percent of the questions in modules Ap0005: Prefaceto OOP Self-Assessment 6 through Ap0060: Self-assessment, More on Arrays 7 in a "closed-book"setting.

    • You understand and can write at least 80-percent of the programs in the Challenge program questionsin modules Ap0005: Preface to OOP Self-Assessment 8 through Ap0060: Self-assessment, More onArrays 9 in a "closed-book" setting.

    • You understand and can answer at least 80-percent of the questions posed on the Review pages inProgramming Fundamentals 10 in a "closed-book" setting

    1.1.5 Prior to enrolling

    I recommend that you understand and be able to answer at least 80-percent of the questions in modulesAp0005: Preface to OOP Self-Assessment 11 through Ap0060: Self-assessment, More on Arrays 12 in a"closed-book" setting.

    4http://www.austincc.edu/5http://www.austincc.edu/baldwin/6http://cnx.org/contents/1CVBGBJj:4OPmk79Y7http://cnx.org/contents/1CVBGBJj:BeZmA_ea8http://cnx.org/contents/1CVBGBJj:4OPmk79Y9http://cnx.org/contents/1CVBGBJj:BeZmA_ea

    10http://cnx.org/contents/EHRr6hjR11http://cnx.org/contents/1CVBGBJj:4OPmk79Y12http://cnx.org/contents/1CVBGBJj:BeZmA_ea

    Available for free at Connexions

  • 3

    I also recommend that you understand and can write at least 80-percent of the programs in the Chal-lenge program questions in modules Ap0005: Preface to OOP Self-Assessment 13 through Ap0060: Self-assessment, More on Arrays 14 in a "closed-book" setting.

    I also recommend that you read and/or study all of the modules in Programming Fundamentals 15 inwhatever depth is necessary to ensure that you can answer at least 80-percent of the questions posed on theReview pages of that book in a "closed-book" setting.

    1.1.6 Course material

    This course material consists of a more than 30 di�erent modules arranged in the following sections:

    • Preface· Preface to ITSE 2321· Completing the First Assignment· Con�guring Your Computer

    • Essence of OOP• Multimedia• The Java Collections Framework• Practice Programs• Java Documentation

    1.1.6.1 Preface

    1.1.6.1.1 Preface to ITSE 2321

    This is the module 16 that you are reading now.

    1.1.6.1.2 Completing the First Assignment

    This module 17 helps students navigate through the learning process so that they can complete the �rstassignment on or before the deadline.

    1.1.6.1.3 Con�guring Your Computer

    This module 18 provides a step-by-step procedure for con�guring a Windows computer to support the typesof sample programs and assignments involved in this course.

    Perhaps the �rst thing that you should do when considering enrollment in this course is to visit thismodule 19 and con�rm that you are able to understand and to replicate the procedures and the resultsexplained there.

    1.1.6.2 Essence of OOP

    The modules in the Essence of OOP 20 section are more or less theoretical in nature. Sample programs inthis section are intended to illustrate the OOP concepts being discussed with no e�ort being made to causethose programs to have any relation to real-world applications.

    13http://cnx.org/contents/1CVBGBJj:4OPmk79Y14http://cnx.org/contents/1CVBGBJj:BeZmA_ea15http://cnx.org/contents/EHRr6hjR:pDHzTeQb16http://cnx.org/contents/dzOvxPFw:6GrxCfXx17http://cnx.org/contents/dzOvxPFw:5oiUHDPw18http://cnx.org/contents/dzOvxPFw:s3zdocvh19http://cnx.org/contents/dzOvxPFw:s3zdocvh20http://cnx.org/contents/dzOvxPFw:rOlnsVRr

    Available for free at Connexions

  • 4 CHAPTER 1. PREFACE

    1.1.6.3 Multimedia

    The modules in the Multimedia 21 section are intended to illustrate OOP concepts using sample programsthat clearly represent real-world applications. In particular, most of the sample programs in this section useOOP concepts to manipulate digital images of the sort that are produced by your digital camera. (Seesome examples in Java3000: The Guzdial-Ericson Multimedia Class Library 22 .)

    1.1.6.4 The Java Collections Framework

    The modules in this section are intended to help you learn about The Java Collections Framework 23 . Onceyou learn how to use the framework, it is unlikely that you will need to reinvent common data structures,search algorithms, or sorting algorithms again, because those capabilities are neatly packaged within theframework.

    1.1.6.5 Practice Programs

    This section 24 provides the source code for a large number of programs illustrating important aspects ofobject-oriented programming with Java.

    1.1.6.6 Java Documentation

    As the name suggests, the material in this section is intended to show you how to access and how to use thestandard Java documentation.

    1.1.7 Downloads

    Unless the format has changed, you should be able to see a green button labeled Get This Book! nearthe upper-right on this page. (You may need to adjust the zoom factor on your browser to cause it toappear. If you don't see the green button, scroll to the bottom of this page where you should see a tablabeled Downloads+ .) Click the button or the tab to see some of the download options for this book.

    You may �nd additional download options both for the book (collection) and for the individual page(module) by:

    • Clicking the link labeled CNX Author | Legacy Site in the upper right corner of this or any otherpage in the book.

    • Scrolling to the bottom of the page on the legacy site where you may see a DOWNLOAD: label,below which you may see a COLLECTION AS: label and a MODULE AS: label.

    • Selecting the link labeled More downloads... to the right of either of those labels.

    I encourage you to take advantage of all of the download options that OpenStax has to o�er in order tocustomize this material for use in your organized courses or for personal self study. If you elect to downloadthe book in the O�ine ZIP format, see the book titled OpenStax Download Cleaner 25 for instructionson how to make that material more useful than it might otherwise be.

    And if you �nd the material useful, I would like to hear more about how you are using it.

    21http://cnx.org/contents/dzOvxPFw:0xo_9JXz22http://cnx.org/contents/dzOvxPFw:0xo_9JXz23http://cnx.org/contents/dzOvxPFw:BaPSYll824http://cnx.org/contents/dzOvxPFw:nezOEDSQ25http://cnx.org/contents/QqpQSOsH

    Available for free at Connexions

  • 5

    1.1.8 Miscellaneous

    This section contains a variety of miscellaneous information.

    Housekeeping material

    • Module name: Jy0020: Java OOP: Preface to ITSE 2321• File: Jy0020.htm• Published: 11/25/12

    Disclaimers: Financial : Although the Connexions site makes it possible for you to downloada PDF �le for this module at no charge, and also makes it possible for you to purchase a pre-printedversion of the PDF �le, you should be aware that some of the HTML elements in this module maynot translate well into PDF.

    I also want you to know that I receive no �nancial compensation from the Connexions website evenif you purchase the PDF version of the module.

    In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindlebooks, and placed them for sale on Amazon.com showing me as the author. I neither receivecompensation for those sales nor do I know who does receive compensation. If you purchase sucha book, please be aware that it is a copy of a module that is freely available on cnx.org and that itwas made and published without my prior knowledge.

    A�liation : I am a professor of Computer Information Technology at Austin Community Collegein Austin, TX.

    -end-

    1.2 Java1554 Completing the First Assignment26

    Revised: Mon Oct 03 12:22:30 CDT 2016This page is included in the following Books:

    • ITSE 2321 - Object-Oriented Programming (Java) 27• Object-Oriented Programming (OOP) with Java 28

    1.2.1 Table of contents

    • Table of contents (p. 5)• Preface (p. 6)

    · Viewing tip (p. 6)* Figures (p. 6)* Listings (p. 6)

    • General background information (p. 6)· Orientation and the rules of the road (p. 6)· Con�gure your computer (p. 7)· Review programming fundamentals in Java (p. 7)· Learn object-oriented programming fundamentals (p. 7)

    26This content is available online at .27http://cnx.org/contents/dzOvxPFw28http://cnx.org/contents/-2RmHFs_

    Available for free at Connexions

  • 6 CHAPTER 1. PREFACE

    • Discussion and sample code (p. 8)· Working with turtles in general (p. 8)· Drawing a circle with a turtle (p. 9)

    • Run the programs (p. 10)• Complete program listings (p. 10)• Miscellaneous (p. 13)

    1.2.2 Preface

    Many students in this course have di�culty getting started and fail to submit the �rst assignment by theprescribed deadline. Many (but not all) of those students either withdraw too late to get a refund or failthe course altogether.

    The purpose of this lesson is to lay out a set of steps that students can follow in order

    • to get started promptly, and• to submit a correct copy of the �rst assignment before the deadline.

    Note that this lesson won't write the �rst assignment for you. Rather, it will help you navigate the learningprocess so that you can accomplish that on your own.

    1.2.2.1 Viewing tip

    I recommend that you open another copy of this module in a separate browser window and use the followinglinks to easily �nd and view the Figures and Listings while you are reading about them.

    1.2.2.1.1 Figures

    • Figure 1. (p. 9) Graphic output from Turtles01.• Figure 2. (p. 10) Graphic output from Circles01.

    1.2.2.1.2 Listings

    • Listing 1 (p. 10) . The driver class named Turtles01.• Listing 2 (p. 11) . The class named Turtles01Runner.• Listing 3 (p. 12) . The class named Circles01.• Listing 4 (p. 12) . The class named Circles01Runner.

    1.2.3 General background information

    This section will describe the things that you need to do before you begin writing code for the �rst assignment.

    1.2.3.1 Orientation and the rules of the road

    As of September 2016, Austin Community College 29 uses a learning management system (LMS) known asBlackboard (Bb) 30 . When you enroll in this course, you will have access to the course in Bb. The �rstthing that you should do is to open the course in Bb and read all of the announcements, paying particularattention to the requirement for online orientation.

    29http://www.austincc.edu/30http://www.blackboard.com/learning-management-system/blackboard-learn.aspx

    Available for free at Connexions

  • 7

    The next thing that you should do is go to the page 31 on the college website containing links to all ofthe courses that I teach. Locate your course among the various courses listed there. Select the link thatpoints to the syllabus page for my courses. Then select the link to the syllabus for your course and read thesyllabus. Make certain that you understand all of the information provided in the syllabus.

    Once you understand the information in the syllabus, return to the page 32 containing links to all ofthe courses that I teach. Locate your course among the various courses listed there and select the link thatpoints to the Main page for your course. Read that page and all of the pages referenced by links on thatpage.

    By this point, you should fully understand the rules of the road for this course. Go back to your coursein Bb and complete the orientation test.

    If you have questions along the way, be sure to ask those questions.

    1.2.3.2 Con�gure your computer

    The next step is to con�gure your computer to make it compatible with the requirements of this course. Goto Java 1560: Con�guring Your Computer 33 . Study that lesson carefully. Make absolutely certain thatyou can replicate all of the procedures and results shown there with the possible exception of the material inthe section titled Compare the graphic output from each of two Java programs 34 . (I exclude that sectiononly because you don't have the output from two Java programs to compare at this point.)

    Once you have accomplished that, you should probably skip ahead and con�rm that you can replicatethe procedures and results described in the lesson titled Java3000: The Guzdial-Ericson Multimedia ClassLibrary 35 . You could hold o� and do that later if you prefer but you de�nitely need to accomplish thatbefore you begin to write your �rst assignment program.

    1.2.3.3 Review programming fundamentals in Java

    Now that you have your computer con�gured, it is time to begin learning how to use the Java programminglanguage for object-oriented programming. The �rst step is to review the Ebook titled Programming Funda-mentals with Java 36 in whatever depth you need in order to transfer your prior knowledge of programmingfundamentals to the Java programming language. (Note that the previous text refers to programmingfundamentals and not to object-oriented programming fundamentals . That is the topic ofthe next section.)

    1.2.3.4 Learn object-oriented programming fundamentals

    This is where things become more interesting. At this point, you will need to begin studying three portionsof two di�erent Ebooks in parallel and to integrate what you learn in your mind.

    First and foremost, you need to begin studying the material that begins at Java1600: Objects andEncapsulation 37 in order to start understanding the important aspects of object-oriented programming.

    In conjunction with that, you need to start taking the assessment tests in the Ebook titled Java OOPSelf-Assessment 38 to con�rm your knowledge of that material.

    Once you start feeling comfortable that you are gaining an understanding of OOP from a largely theo-retical viewpoint, you need to study the following six lessons for the purpose of applying the OOP conceptsto more meaningful programs .

    31http://www.austincc.edu/baldwin/32http://www.austincc.edu/baldwin/33http://cnx.org/contents/dzOvxPFw:s3zdocvh34http://cnx.org/contents/dzOvxPFw:s3zdocvh#Compare_the_graphic_output_from_each_of_two_Java_programs35http://cnx.org/contents/dzOvxPFw:0xo_9JXz36http://cnx.org/contents/EHRr6hjR37http://cnx.org/contents/dzOvxPFw:rOlnsVRr38http://cnx.org/contents/1CVBGBJj

    Available for free at Connexions

  • 8 CHAPTER 1. PREFACE

    • Java3000: The Guzdial-Ericson Multimedia Class Library 39• Java3000r Review 40• Java3002: Creating and Manipulating Turtles and Pictures in a World Object 41• Java3002r Review 42• Java3003: Drawing Graphs with Turtles and Pixels 43• Program 1 44 in the section titled Java OOP: ITSE 2321 Practice Group 1 45

    1.2.4 Discussion and sample code

    By the time you understand the material in the six lessons listed above (p. 7) , you should be fully capable ofwriting and submitting a correct version of the �rst assignment. However, in case you need more help in thatregard, I am going to provide two sample programs, each of which deals with part of the requirements of the�rst programming assignment. It will be up to you to understand and to integrate the concepts illustratedby these sample programs into a single program that satis�es the requirements of the �rst assignment.

    Note that in addition to these two sample programs and the sample programs in the lessons listed above(p. 7) , there are many other sample programs in the section of the Ebook that begins here 46 .

    1.2.4.1 Working with turtles in general

    Most of the programming assignments in this course are structured as follows. Along with a programspeci�cation document, I provide a short Java source code �le containing a class de�nition that I often referto as the driver class. I also provide any necessary image �les.

    The student is not allowed to make any changes to the driver class. Instead, the student must writeone or more source code �les which, when compiled and executed in conjunction with the driver class, willproduce the required output. In the program that I describe here, the driver class is named Turtles01 andthe code that the student would be required to write would be in the class named Turtles01Runner .

    The student only submits the source code �les for the classes de�ned by the student. In this program,that code would be in a �le named Turtles01Runner.java .

    Listing 1 (p. 10) and Listing 2 (p. 11) near the end of the lesson show the source code for the driverclass and the Turtles01Runner class for this program.

    This program produces the output image shown in Figure 1 (p. 9) .

    39http://cnx.org/contents/dzOvxPFw:0xo_9JXz40http://cnx.org/contents/dzOvxPFw:qJgLGFl541http://cnx.org/contents/dzOvxPFw:WpmhN38H42http://cnx.org/contents/dzOvxPFw:C0a0NBaZ43http://cnx.org/contents/dzOvxPFw:Sc6k8KyF44http://cnx.org/contents/dzOvxPFw:nezOEDSQ#Program_145http://cnx.org/contents/dzOvxPFw:nezOEDSQ46http://cnx.org/contents/dzOvxPFw:nezOEDSQ

    Available for free at Connexions

  • 9

    Figure 1. Graphic output from Turtles01.

    Once you have access to the �rst assignment, you should be able to some similarities between the outputimage in Figure 1 (p. 9) and the required output image for the �rst assignment.

    1.2.4.2 Drawing a circle with a turtle

    Like the previous program, the program presented in this section has a driver class named Circles01 andanother class named Circles01Runner . The source code for those two class de�nitions is shown inListing 3 (p. 12) and Listing 4 (p. 12) .

    The output image produced by this program is shown in Figure 2 (p. 10) .

    Available for free at Connexions

  • 10 CHAPTER 1. PREFACE

    Figure 2. Graphic output from Circles01.

    1.2.5 Run the programs

    I encourage you to copy the code from Listing 1 (p. 10) through Listing 4 (p. 12) . Compile and executethe code for each program and con�rm that you get the same results as those shown in Figure 1 (p. 9) andFigure 2 (p. 10) . (Click here 47 to download a zip �le containing the image �le required by the Turtlesprogram.)

    Experiment with the code, making changes, and observing the results of your changes. Make certain thatyou can explain why your changes behave as they do.

    1.2.6 Complete program listings

    Listing 1 . The driver class named Turtles01.

    /*File Turtles01 Copyright 2010 R.G.Baldwin

    *********************************************************/

    import java.awt.Color;

    public class Turtles01{

    //DO NOT MODIFY THE CODE IN THIS CLASS DEFINITION.

    public static void main(String[] args){

    Turtles01Runner obj = new Turtles01Runner();

    obj.run();

    47http://cnx.org/content/m63203/latest/Turtles01.zip

    Available for free at Connexions

  • 11

    }//end main

    }//end class Turtles01

    //End program specifications.

    Listing 2 . The class named Turtles01Runner.

    /*File Turtles01Runner Copyright 2016 R.G.Baldwin

    *********************************************************/

    import java.awt.Color;

    class Turtles01Runner{

    //Declare the World and Turtle objects.

    private World mars;

    private Turtle aTurtle;

    public void run(){

    //Replace the default all-white picture with another

    // picture.

    Picture picture = new Picture("Turtles01.jpg");

    mars =

    new World(picture.getWidth(),picture.getHeight());

    mars.setPicture(picture);

    aTurtle = new Turtle(mars);

    aTurtle.penDown();

    aTurtle.setPenColor(Color.GREEN);

    aTurtle.setPenWidth(1);

    aTurtle.forward(110);

    aTurtle.turn(-90);

    aTurtle.setPenWidth(2);

    aTurtle.setPenColor(Color.RED);

    aTurtle.forward(110);

    aTurtle.turn(-90);

    aTurtle.setPenWidth(3);

    aTurtle.setPenColor(Color.YELLOW);

    aTurtle.forward(110);

    aTurtle.setPenWidth(4);

    aTurtle.setPenColor(Color.BLUE);

    aTurtle.forward(110);

    aTurtle.turn(-90);

    aTurtle.setPenWidth(5);

    aTurtle.setPenColor(Color.GREEN);

    aTurtle.forward(110);

    }//end run method

    }//end class Turtles01Runner

    Available for free at Connexions

  • 12 CHAPTER 1. PREFACE

    Listing 3 . The class named Circles01.

    /*File Circles01 Copyright 2016 R.G.Baldwin

    *********************************************************/

    import java.awt.Color;

    public class Circles01{

    //DO NOT MODIFY THE CODE IN THIS CLASS DEFINITION.

    public static void main(String[] args){

    Circles01Runner obj = new Circles01Runner();

    obj.run();

    }//end main

    }//end class Circles01

    //End program specifications.

    Listing 4 . The class named Circles01Runner.

    /*File Circles01Runner Copyright 2016 R.G.Baldwin

    *********************************************************/

    import java.awt.Color;

    class Circles01Runner{

    //Declare the World and Turtle objects.

    private World mars;

    private Turtle aTurtle;

    public void run(){

    mars = new World(240,240);

    aTurtle = new Turtle(mars);

    double radius = 110;

    aTurtle.turn(90);

    aTurtle.penUp();

    aTurtle.setPenColor(Color.RED);

    aTurtle.setPenWidth(1);

    aTurtle.forward((int)radius);

    //Draw the semicircle

    aTurtle.penDown();

    aTurtle.setPenWidth(3);

    double dY = 0.0;

    double dX = radius;

    while(dX > -radius){dX-=1;

    dY = Math.sqrt(radius*radius - dX*dX);

    aTurtle.moveTo((int)dX+mars.getWidth()/2,

    (int)dY+mars.getHeight()/2);

    }//end while loop

    }//end run method

    }//end class Circles01Runner

    Available for free at Connexions

  • 13

    1.2.7 Miscellaneous

    This section contains a variety of miscellaneous information.

    Housekeeping material

    • Module name: Java1554 Completing the First Assignment• File: Java1554.htm• Published: 10/02/16

    Disclaimers: Financial : Although the Connexions site makes it possible for you to downloada PDF �le for this module at no charge, and also makes it possible for you to purchase a pre-printedversion of the PDF �le, you should be aware that some of the HTML elements in this module maynot translate well into PDF.

    I also want you to know that, I receive no �nancial compensation from the Connexions website evenif you purchase the PDF version of the module.

    In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindlebooks, and placed them for sale on Amazon.com showing me as the author. I neither receivecompensation for those sales nor do I know who does receive compensation. If you purchase sucha book, please be aware that it is a copy of a module that is freely available on cnx.org and that itwas made and published without my prior knowledge.

    A�liation : I am a professor of Computer Information Technology at Austin Community Collegein Austin, TX.

    -end-

    1.3 Java 1560: Con�guring Your Computer48

    Revised: Tue Sep 20 11:47:25 CDT 2016This page is included in the following Books:

    • ITSE 2321 - Object-Oriented Programming (Java) 49• Object-Oriented Programming (OOP) with Java 50

    1.3.1 Table of contents

    • Table of contents (p. 13)• Preface (p. 14)

    · Viewing tip (p. 15)* Figures (p. 15)* Listings (p. 15)

    • Download, install, and test the Java Development Kit (JDK) (p. 16)· Download and install the JDK (p. 16)· Test the JDK (p. 17)· Compile and run a simple text-based Java program with a single source code �le (p. 18)· Compile and run a simple text-based Java program with two source code �les (p. 19)

    48This content is available online at .49http://cnx.org/contents/dzOvxPFw50http://cnx.org/contents/-2RmHFs_

    Available for free at Connexions

  • 14 CHAPTER 1. PREFACE

    • Download, install, and test the multimedia library (p. 20)· Download the library (p. 20)· Prepare the library for use (p. 20)· Library documentation (p. 20)· Compile and run a graphics program with a single source code �le (p. 20)· Compile and run a graphics program with two source code �les and an image �le (p. 22)

    • Running pre-compiled programs (p. 23)• Compare the graphic output from each of two Java programs (p. 25)• Miscellaneous (p. 26)

    1.3.2 Preface

    I have published several Ebooks dealing with Java programming in general and the courses that I teach atAustin Community College in particular. That list includes the following Ebooks:

    • ITSE 2321 - Object-Oriented Programming (Java) 51 : This Ebook.• Programming Fundamentals with Java 52• Java OOP Self-Assessment 53• ITSE 2317 - Java Programming (Intermediate) 54• INEW 2338 - Advanced Java Programming 55• The json-simple Java Library 56• GAME 2302 - Mathematical Applications for Game Development 57• Object-Oriented Programming (OOP) with Java 58• AP Computer Science A, Clari�cation of the Java Subset 59• Accessible Objected-Oriented Programming Concepts for Blind Students using Java 60• Anatomy of a Game Engine 61• Fun with Java 62• Image Processing using Java 63• Java Graphics 64• Java Sound 65• Java Swing from A to Z 66• Java2D Graphics 67• JavaBeans Components 68• Morse Code and Computer Programs 69

    51http://cnx.org/contents/dzOvxPFw52http://cnx.org/contents/EHRr6hjR:pDHzTeQb53http://cnx.org/contents/1CVBGBJj54http://cnx.org/contents/Rl23r3Lw55http://cnx.org/contents/yWyT-uhM56http://cnx.org/contents/5sRB9gpG57http://cnx.org/contents/Ki_nRUo6:SGyUbirS58http://cnx.org/contents/-2RmHFs_:kFS-maG_59http://cnx.org/contents/2bKkYivW:uCarXUQ060http://cnx.org/contents/27YG7QNc:YTv-wnpo61http://cnx.org/contents/oOLSrHU0:eHQuSS4W62http://cnx.org/contents/3T7WEdtD:wS9aDaUn63http://cnx.org/contents/ycm9ykKB:EXrAm_4l64http://cnx.org/contents/rrRQJ3ZS:DHabim1Z65http://cnx.org/contents/COh26rPj:Kib1Isp566http://cnx.org/contents/JbJvdDy4:C9qrp5I867http://cnx.org/contents/7tjDQie6:HDK3pMT268http://cnx.org/contents/u_b-KzFY:7dn3cpHx69http://cnx.org/contents/zWWYKvGC

    Available for free at Connexions

  • 15

    • Programming Oldies But Goodies 70

    Various lessons in those Ebooks explain how to con�gure your computer for compiling and executing the sam-ple programs in those Ebooks. However, I continue to receive questions from students in this course (ITSE2321 - Object-Oriented Programming (Java)) who are confused about how to con�gure their computers forthis course.

    In this lesson, I will provide a step-by-step procedure for con�guring a Windows computer to support thetypes of sample programs and assignments involved in this course. This won't be the most elegant procedureor necessarily the best procedure. However, it will be a procedure that should make it possible for studentshaving minimal knowledge of such topics as Windows environment variables , Windows batch �les ,Windows command-line operations , etc. to successfully con�gure their computer for use in this course.

    Note: If you are using an operating system other than Windows, you may need to �nd someonewho can translate the steps in this procedure into the corresponding set of steps for your operatingsystem.

    The instructions in this procedure will be current for the 64-bit version of Windows 7 and the Java JDKin September of 2016. As time goes by, some of the links may change and you may need to modify yourresponse to the instructions to accommodate those changes. Also, the user interface for newer versions ofWindows may be di�erent from Windows 7. An e�ort will be made to write the instructions in such a wayas to accommodate such changes.

    You will probably need administrator privileges on your computer in order to install the software.The instructions in this procedure will concentrate on the "what" and not the "why" .. Don't be

    concerned if you don't understand the code used in these instructions. You will learn why the code behavesas it does in future lessons. Just follow the instructions and con�rm that you can produce the results shownin this lesson to con�rm that your computer is properly con�gured.

    1.3.2.1 Viewing tip

    I recommend that you open another copy of this module in a separate browser window and use the followinglinks to easily �nd and view the Figures and Listings while you are reading about them.

    1.3.2.1.1 Figures

    • Figure 1. (p. 17) Screen output for the initial test.• Figure 2. (p. 17) Screen output for the expanded test.• Figure 3. (p. 18) Text output from the program named Hello01.• Figure 4. (p. 19) Text output from the program named Hello02.• Figure 5. (p. 21) Graphic output from the program named Hello03.• Figure 6. (p. 23) Graphic output from the program named Hello04.• Figure 7. (p. 24) Graphic output from the program named Hello05.• Figure 8. (p. 26) Compare the graphic output images from two Java programs.

    1.3.2.1.2 Listings

    • Listing 1 (p. 17) . Initial contents of JDKtest.bat.• Listing 2 (p. 17) . Expanded contents of JDKtest.bat.• Listing 3 (p. 18) . Source code for the class named Hello01.• Listing 4 (p. 18) . Commands for the batch �le named Hello01.bat.• Listing 5 (p. 19) . Source code for the class named Hello02.• Listing 6 (p. 19) . Source code for the class named Hello02Runner.

    70http://cnx.org/contents/1J-75Flv:tQAVqK0b

    Available for free at Connexions

  • 16 CHAPTER 1. PREFACE

    • Listing 7 (p. 19) . Commands for the batch �le named Hello02.bat.• Listing 8 (p. 20) . Source code for the class named Hello03.• Listing 9 (p. 21) . Commands for the batch �le named Hello03.bat.• Listing 10 (p. 22) . Source code for the class named Hello04.• Listing 11 (p. 22) . Source code for the class named Hello04Runner.• Listing 12 (p. 22) . Commands for the batch �le named Hello04.bat.• Listing 13 (p. 24) . Commands for the batch �le named RunHello05.bat.

    1.3.3 Download, install, and test the Java Development Kit (JDK)

    The �rst step is to download, install, and test the Java Development Kit (JDK) from the Oracle website.

    1.3.3.1 Download and install the JDK

    Open the Oracle website in your browser at http://www.oracle.com/technetwork/java/javase/downloads/index.html71 .

    Click the download button labeled Java Platform (JDK) 8u101 / 8u102 . (Note that this labeland the labels and titles mentioned below may change as newer versions of the JDK are released.)

    Click the radio button labeled Accept License Agreement in the section titled Java SE Devel-opment Kit 8u101 .

    Locate the line item labeled Windows x64 in the column titled Product/File Description .Click the link labeled jdk-8u101-windows-x64.exe in the column labeled Download immediately

    to the right of the Windows x64 label..Click the Save File button when the Windows dialog opens asking if you would like to save this �le.

    Depending on the version of the JDK, a �le with a name similar to jdk-8u101-windows-x64.exe will besaved in the folder that your computer and your browser uses to save download �les. Find and double-clickthat �le. Click the Yes button when Windows asks if you want to allow the program to make changes toyour computer.

    A series of installation dialogs will be presented by Windows. Simply click the Next button in eachdialog to cause the full JDK to be installed in the default location on your C-drive. Click the Close buttonin the �nal dialog that appears indicating that the JDK has been successfully installed.

    Open Windows Explorer on the C-drive and navigate to a folder having a path similar to C:\ProgramFiles\Java\jdk1.8.0_101\bin . (The exact name will depend on the version of the JDK that youdownloaded.) Write the path down somewhere and save it because you are going to need it later.

    Examine the contents of the bin folder. It should contain a large number of �les including the followingtwo �les: javac.exe and java.exe . Those are the program �les for the compiler and the Java VirtualMachine respectively. As you will see later, they are used to compile and run a Java program.

    At this point, if you are skilled at such Windows operations as setting environment variables, youcan return to the page at http://www.oracle.com/technetwork/java/javase/downloads/index.html72 , �nd and select a link to Installation instructions , and complete the installation accordingto those instructions. However, many students in this course don't have those skills. If you are ingroup that lacks those skills, you can simply skip this step and continue with the following sectiontitled Test the JDK. The following instructions assume that you have skipped this step.

    71http://www.oracle.com/technetwork/java/javase/downloads/index.html72http://www.oracle.com/technetwork/java/javase/downloads/index.html

    Available for free at Connexions

  • 17

    1.3.3.2 Test the JDK

    Now that you have installed the JDK, you need to test it to con�rm that it is properly installed.Open Windows Explorer and create an empty folder somewhere on your C-drive; it doesn't matter where

    you locate the folder. Name the folder JDKtest .Using Windows Notepad along with the path that you saved earlier, create a �le named JDKtest.bat

    in that folder containing the text shown in Listing 1 (p. 17) . Use your path in place of my path to constructthe command. (Note that the extension must be .bat and not .txt. Note also that you must surround thepath that you saved earlier with matching quotation characters when you construct the command.)

    Listing 1 . Initial contents of JDKtest.bat.

    echo off

    "C:\Program Files\Java\jdk1.8.0_101\bin"\javac -versionpause

    Double click the �le named JDKtest.bat . If everything has been done correctly up to this point, a blackcommand-line window should appear on your screen containing text similar to that shown in Figure 1 (p.17) .

    Figure 1. Screen output for the initial test.

    C:\jnk\6\JDKtest>echo offjavac 1.8.0_101

    Press any key to continue . . .

    The �rst line of text in your output will re�ect the location of the folder named JDKtest on your C-drive.The second line of text will re�ect the version number of the JDK that you have installed.The third line of text should be unchanged unless a future version of Windows changes the format of

    that line of text. (This line of text is produced by the pause command in the batch �le. Withoutthis command, the command-line window would appear momentarily and then disappear before you have achance to examine it.)

    If you are unable to get this result, you need to go back and follow the steps more carefully. If youare certain that you have followed the steps and are still unable to get this result, please let me know. Itis possible that I may have made an error in describing the steps or that I need to update the steps toaccommodate a future version of the JDK or of Windows.

    If you do get this result, update the contents of the �le named JDKtest.bat to match that shown inListing 2 (p. 17) .

    Listing 2 . Expanded contents of JDKtest.bat.

    echo off

    "C:\Program Files\Java\jdk1.8.0_101\bin"\javac -version"C:\Program Files\Java\jdk1.8.0_101\bin"\java -versionpause

    Double click the �le named JDKtest.bat . A black command-line window should appear on your screencontaining text similar to that shown in Figure 2 (p. 17) .

    Figure 2. Screen output for the expanded test.

    Available for free at Connexions

  • 18 CHAPTER 1. PREFACE

    C:\jnk\6\JDKtest>echo offjavac 1.8.0_101

    java version "1.8.0_101"

    Java(TM) SE Runtime Environment (build 1.8.0_101-b13)

    Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)

    Press any key to continue . . .

    The second and third lines of text should re�ect the version of the JDK that you have installed.The fourth and �fth lines of text will be di�erent for future versions of the JDK but they should be

    similar to that shown.Once again, if you are unable to get this result, you need to go back and follow the steps more carefully.If you are able to get this result, this con�rms that the JDK is properly installed on your computer.

    1.3.3.3 Compile and run a simple text-based Java program with a single source code �le

    Now that you have con�rmed that the JDK is properly installed on your computer, it is time to write,compile, and execute a very simple Java program.

    Using Windows Explorer, open a new folder on your C-drive named Hello01 . Using Notepad oranother text editor of your choice, create a �le in that folder named Hello01.java containing the codeshown in Listing 3 (p. 18) .

    Listing 3 . Source code for the class named Hello01.

    class Hello01{

    public static void main(String[] args){

    System.out.println("Hello World from Hello01");

    }//end main

    }//end class

    Now using the path that you saved earlier, create a batch �le named Hello01.bat in that same foldercontaining the commands shown in Listing 4 (p. 18) .

    Listing 4 . Commands for the batch �le named Hello01.bat.

    echo off

    "C:\Program Files\Java\jdk1.8.0_101\bin"\javac Hello01.java"C:\Program Files\Java\jdk1.8.0_101\bin"\java Hello01pause

    Double click the batch �le. A black command-line screen should appear and display the program outputtext shown in Figure 3 (p. 18) .

    Figure 3. Text output from the program named Hello01.

    Hello World from Hello01

    Press any key to continue . . .

    Available for free at Connexions

  • 19

    1.3.3.4 Compile and run a simple text-based Java program with two source code �les

    Assuming that you were able to produce the output shown in the previous section it's time to write, compile,and execute a slightly more complex program consisting of two Java source code �les.

    Create a new folder named Hello02 . Create a Java source code �le in that folder named Hello02.javacontaining the code shown in Listing 5 (p. 19) .

    Listing 5 . Source code for the class named Hello02.

    class Hello02{

    public static void main(String[] args){

    new Hello02Runner().run();

    }//end main

    }//end class

    Create another Java source code �le in that folder named Hello02Runner.java containing the code shownin Listing 6 (p. 19) .

    Listing 6 . Source code for the class named Hello02Runner.

    class Hello02Runner{

    void run(){

    System.out.println("Hello World from Hello02");

    }//end main

    }//end class

    Create a batch �le named Hello02.bat containing the commands shown in Listing 7 (p. 19) .

    Listing 7 . Commands for the batch �le named Hello02.bat.

    echo off

    "C:\Program Files\Java\jdk1.8.0_101\bin"\javac Hello02.java"C:\Program Files\Java\jdk1.8.0_101\bin"\java Hello02pause

    Double click the batch �le. A black command-line screen should appear and display the program outputtext shown in Figure 4 (p. 19) .

    Figure 4. Text output from the program named Hello02.

    Hello World from Hello02

    Press any key to continue . . .

    If you have been successful to this point, you should now be able to compile and execute the code for anytext-based program involved in this course.

    Available for free at Connexions

  • 20 CHAPTER 1. PREFACE

    1.3.4 Download, install, and test the multimedia library

    Many of the sample programs and assignments in this course make use of a multimedia class library developedand made available by Mark Guzdial 73 and Barbara Ericson 74 of the Georgia Institute of Technology.Therefore, it will be necessary for you to download and install that library on your computer.

    The Guzdial-Ericson library was originally published by Guzdial and Ericson in conjunction with theirbook Introduction to Computing and Programming with Java: A Multimedia Approach 75 . While the bookisn't free, the library is freely available and is published under a Creative Commons Attribution 3.0 UnitedStates License 76 .

    1.3.4.1 Download the library

    As of September 2016, the library can be downloaded in a zip �le named bookClasses-3-9-10-with-doc.zip at http://home.cc.gatech.edu/TeaParty/47. 77 Additional information is available athttp://coweb.cc.gatech.edu/mediaComp-plan/101 78 .

    In order to work with the sample programs and assignments that use the library, you will need to downloada copy of the zip �le from the site listed above. To guard against the possibility of that link becoming brokenat some point in the future, I am depositing a backup copy of the zip �le containing the library on thecnx.org site and you can download it here 79 .

    1.3.4.2 Prepare the library for use

    Once you have downloaded the zip �le, you will need to extract the folder named bookClasses from thezip �le and store it as a folder in the root of your C-drive. The path to the library will then be:

    C:\bookClasses

    Once you have stored the bookClasses folder on your C-drive as described above, DO NOT do anythingthat will modify the contents of the folder. For example, don't try to compile the source code in the library.The source code in the library has already been compiled. The library is completely ready for use as receivedin the download.

    1.3.4.3 Library documentation

    When you examine the contents of the bookClasses folder, you will see that in addition to source codeand compiled class �les for the library, the folder also contains javadoc documentation for the library ina folder named doc and some other material as well. Go to the doc folder and open the �le namedindex.html in your browser to view the documentation.

    1.3.4.4 Compile and run a graphics program with a single source code �le

    This section will demonstrate how to use the multimedia library to create and display a simple graphicswindow.

    Create an empty folder named Hello03 on your C-drive.Create a Java source code �le named Hello03.java containing the code shown in Listing 8 (p. 20) and

    store the �le in the folder named Hello03 .

    Listing 8 . Source code for the class named Hello03.

    73http://www.cc.gatech.edu/∼guzdial/74http://coweb.cc.gatech.edu/ice-gt/875http://www.pearsonhighered.com/educator/academic/product/0,3110,0131496980,00.html76http://creativecommons.org/licenses/by/3.0/us/77http://home.cc.gatech.edu/TeaParty/47.78http://coweb.cc.gatech.edu/mediaComp-plan/10179http://cnx.org/content/m63123/latest/bookClasses-3-9-10-with-doc.zip

    Available for free at Connexions

  • 21

    import java.awt.Color;

    class Hello03{

    public static void main(String[] args){

    Picture pix = new Picture(300,100);

    pix.setAllPixelsToAColor(Color.RED);

    pix.addMessage("Hello World from Hello03",50,50);

    pix.explore();

    }// end main

    }//end class

    Create a batch �le named Hello03.bat containing the commands shown in Listing 9 (p. 21) . Store thebatch �le in the folder named Hello03 .

    Listing 9 . Commands for the batch �le named Hello03.bat.

    echo off

    "C:\Program Files\Java\jdk1.8.0_101\bin"\javac -cp .;C:\bookClasses Hello03.java"C:\Program Files\Java\jdk1.8.0_101\bin"\java -cp .;C:\bookClasses Hello03

    Double click the batch �le. As before, a black command-line window will appear on your screen. In thiscase, however, there is nothing interesting in the command-line window. The interesting output will appearon your screen in the form of a graphics window as shown in Figure 5 (p. 21) .

    Figure 5. Graphic output from the program named Hello03.

    If you click the large X in the upper-right corner of the window, both the graphics window and the blackcommand-line window should disappear from your screen.

    Available for free at Connexions

  • 22 CHAPTER 1. PREFACE

    1.3.4.5 Compile and run a graphics program with two source code �les and an image �le

    Now I will show you how to write, compile, and execute a slightly more complex graphics program consistingof two Java source code �les and an image �le.

    Begin by creating an empty folder named Hello04 on your C-drive.Click here 80 to download a zip �le containing an image �le named Hello04-a.jpg . Extract and save

    the image �le in the folder named Hello04 .Create a Java source code �le named Hello04.java containing the code shown in Listing 10 (p. 22) .

    Save the �le in the folder named Hello04 .

    Listing 10 . Source code for the class named Hello04.

    class Hello04{

    public static void main(String[] args){

    new Hello04Runner().run();

    }// end main

    }//end class

    Create another Java source code �le named Hello04Runner.java containing the code shown in Listing11 (p. 22) . Save the �le in the folder named Hello04 .

    Listing 11 . Source code for the class named Hello04Runner.

    class Hello04Runner{

    void run(){

    Picture pix = new Picture("Hello04-a.jpg");

    pix.addMessage("Hello World from Hello04",100,100);

    pix.explore();

    }// end main

    }//end class

    Using your path, create a batch �le named Hello04.bat containing the commands shown in Listing 12 (p.22) . Save the �le in the folder named Hello04 .

    Listing 12 . Commands for the batch �le named Hello04.bat.

    echo off

    "C:\Program Files\Java\jdk1.8.0_101\bin"\javac -cp .;C:\bookClasses Hello04.java"C:\Program Files\Java\jdk1.8.0_101\bin"\java -cp .;C:\bookClasses Hello04

    Double click the batch �le. As before, a black command-line window will appear on your screen. Also asbefore, there is nothing interesting in the command-line window. The interesting output will appear on yourscreen in the form of a graphics window as shown in Figure 6 (p. 23) .

    80http://cnx.org/content/m63123/latest/Hello04-a.zip

    Available for free at Connexions

  • 23

    Figure 6. Graphic output from the program named Hello04.

    That is the end of the con�guration procedure. If you have been successful in carrying out all of theseinstructions, given the source code, you should now be able to compile and execute the code for any programinvolved in this course that uses Ericson's multimedia library. Of course, writing that source code is anothermatter altogether.

    1.3.5 Running pre-compiled programs

    There are a couple more basic skills that students need in order to successfully complete this course.

    • Students need to be able to run pre-compiled programs when provided the class �les and images �les

    Available for free at Connexions

  • 24 CHAPTER 1. PREFACE

    required by the program.• Students need to be able to run two di�erent Java programs and compare the output text and/or

    graphic images produced by the two programs.

    This section will deal with the �rst of those two skills. The second skill will be dealt with in the next section.Begin by creating a new empty folder named Hello05 .Click here 81 to download a zip �le named Hello05.zip . The zip �le contains the following three �les,

    which are the �les required to run a pre-compiled program named Hello05 .

    • Hello05.class• Hello05Runner.class• Hello05-a.jpg

    Extract the contents of the zip �le and store them in the folder named Hello05 ..Create a batch �le named RunHello05.bat containing the commands shown in Listing 13 (p. 24) .

    Store the batch �le in the folder named Hello05 . (Note that unlike the previous batch �les, this batch�le does not execute the compiler program named javac .)

    Listing 13 . Commands for the batch �le named RunHello05.bat.

    echo off

    "C:\Program Files\Java\jdk1.8.0_101\bin"\java -cp .;C:\bookClasses Hello05

    Double click the batch �le to run the program. The output should be as shown in Figure 7 (p. 24) .

    Figure 7. Graphic output from the program named Hello05.

    81http://cnx.org/content/m63123/latest/Hello05.zip

    Available for free at Connexions

  • 25

    1.3.6 Compare the graphic output from each of two Java programs

    Follow the procedure described here (p. 22) to compile and run the program named Hello04 producingthe graphic output shown in Figure 6 (p. 23) . The graphic output will appear in the upper-left corner ofthe screen.

    Using the mouse, grab the output window by the banner at the top and drag it to the right side of thescreen.

    Follow the procedure described here (p. 23) to run the program named Hello05 producing the graphicoutput shown in Figure 7 (p. 24) . The graphic output will appear in the upper-left corner of the screen.

    Available for free at Connexions

  • 26 CHAPTER 1. PREFACE

    Using the mouse, drag the windows and position them side-by-side on your screen as shown in Figure 8(p. 26) . Visually compare the images to see if they match.

    Figure 8. Compare the graphic output images from two Java programs.

    You may also �nd it useful to position the windows one above the other, depending on which aspect of theimages you need to compare.

    1.3.7 Miscellaneous

    This section contains a variety of miscellaneous information.

    Housekeeping material

    • Module name: Java 1560: Con�guring Your Computer• File: Java 1560.htm• Published: 09/18/16

    Disclaimers: Financial : Although the Connexions site makes it possible for you to downloada PDF �le for this module at no charge, and also makes it possible for you to purchase a pre-printedversion of the PDF �le, you should be aware that some of the HTML elements in this module maynot translate well into PDF.

    I also want you to know that, I receive no �nancial compensation from the Connexions website evenif you purchase the PDF version of the module.

    In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindlebooks, and placed them for sale on Amazon.com showing me as the author. I neither receivecompensation for those sales nor do I know who does receive compensation. If you purchase sucha book, please be aware that it is a copy of a module that is freely available on cnx.org and that itwas made and published without my prior knowledge.

    A�liation : I am a professor of Computer Information Technology at Austin Community Collegein Austin, TX.

    Available for free at Connexions

  • 27

    -end-

    1.4 Java1562: Email Noti�cation of Ebook Changes82

    Revised: Thu Oct 06 14:34:55 CDT 2016This page is included in the following Books:

    • ITSE 2321 - Object-Oriented Programming (Java) 83• Object-Oriented Programming (OOP) with Java 84

    1.4.1 Table of contents

    • Table of contents (p. 27)• Preface (p. 27)• Creating an account (p. 27)• Activating the Email noti�cation (p. 27)• Adding Ebooks to the list of your favorite Ebooks (p. 28)• Deactivating Email noti�cation (p. 29)• Miscellaneous (p. 29)

    1.4.2 Preface

    Would you like to receive an Email noti�cation whenever I add a new page to an Ebook, update a page inan Ebook, or remove a page from an Ebook? As of October 2016, OpenStax CNX 85 provides a featurecalled a lens that makes that possible. This page provides instructions for registering to receive Emailnoti�cations of Ebook changes. (A lens provides some other capabilities as well, but I won't discuss thoseother capabilities here.)

    1.4.3 Creating an account

    Viewing content (such as this Ebook) on OpenStax CNX 86 doesn't require a login. However, if you createa free account, that will allow you to activate the Email noti�cation feature and to access some other featuresas well.

    Click here 87 to request a free OpenStax CNX 88 account. (Note that access to some of the featuresgranted by an account require access to the legacy server, which may be slower than you are accustomedto.)

    1.4.4 Activating the Email noti�cation

    When you create your account, a default lens named My Favorites will be automatically created forthe account. Although you can create other lenses with di�erent names, that isn't necessary to activate theEmail noti�cation feature.

    The following instructions assume that you have created an account and you are viewing one of myEbooks such as ITSE 2321 - Object-Oriented Programming (Java) 89 .

    82This content is available online at .83http://cnx.org/contents/dzOvxPFw84http://cnx.org/contents/-2RmHFs_85http://cnx.org/86http://cnx.org/87https://legacy.cnx.org/new_account?came_from=mydashboard88http://cnx.org/89http://cnx.org/contents/dzOvxPFw

    Available for free at Connexions

  • 28 CHAPTER 1. PREFACE

    Begin by selecting the link titled CNX Author | Legacy Site in the upper-right corner of any pagein the Ebook. For the example of the Ebook titled ITSE 2321 - Object-Oriented Programming (Java) 90 ,selecting that link should cause you to land on a page that displays the same page of the same Ebook in thelegacy format. (Note that an Ebook is called a Collection in the legacy format.)

    The page that you land on will have the following tabs at the top of the page:

    • Home• Content• Lenses• About Us• Help• MyCNX

    Select the tab labeled MyCNX . You will land on your home page in the authoring section of the websiteunder the tab labeled MyCNX .

    If you are not already logged in, you will land on a page asking you to log in. Go ahead and log in usingthe account that you created earlier. After logging in, you will land on your home page in the authoringsection of the website under the tab labeled MyCNX .

    Select the link labeled Lenses in the upper-left portion of the page. You will see a list of any lensesthat you have created plus the default lens named My Favorites . Select the edit link next to thelink for the lens named My Favorites . You should land on a page containing a tab labeled Edit lensproperties . Select that tab to edit the properties of the My Favorites lens.

    Check the box labeled Receive e-mails when any content included in this lens is changed.Then select the button labeled Save at the bottom of the page. You are now registered to receive Emailnoti�cations when the contents of your favorite Ebooks change.

    Note that this capability is not restricted to this Ebook. As of October 2016, you can use this procedureto register for Email noti�cations of changes to any Ebook published on OpenStax CNX 91 , including theopen textbooks such as College Physics 92 .

    1.4.5 Adding Ebooks to the list of your favorite Ebooks

    Once you have completed the registration described above, if you are viewing a page in an Ebook and youwant to add the Ebook to your My Favorites lens,

    • Select the CNX Author | Legacy Site link in the upper-right corner of the page.• Select the link labeled Add to Favorites at the top of the page that you land on.

    At that point, you can add the page or the entire Collection (Ebook) to the lens.Having added the Ebook or page to the lens, you will receive an Email message notifying you when

    that material is modi�ed and republished. The Email message identi�es the page and provides some otherinformation as well.

    Please be aware that whenever I modify a page, I sometimes require several cycles of check-out/modify/upload/publish before I am satis�ed with the modi�ed version. In such a situation,you may receive several Email messages in succession.

    90http://cnx.org/contents/dzOvxPFw91http://cnx.org/92http://cnx.org/contents/Ax2o07Ul

    Available for free at Connexions

  • 29

    1.4.6 Deactivating Email noti�cation

    If you decide that you don't want to receive Email noti�cations, you can repeat the process described aboveand remove the check from the box labeled Receive e-mails when any content included in this lensis changed.

    Or, you could go to your MyCNX page, select the link to Lenses , select the edit link for the MyFavorites lens, check the box next to the troublesome item, and select the Remove from lens button.

    1.4.7 Miscellaneous

    This section contains a variety of miscellaneous information.

    Housekeeping material

    • Module name: Java1562: Email Noti�cation of Ebook Changes• File: Java1562.htm• Published: 10/06/16

    Disclaimers: Financial : Although the Connexions site makes it possible for you to downloada PDF �le for this module at no charge, and also makes it possible for you to purchase a pre-printedversion of the PDF �le, you should be aware that some of the HTML elements in this module maynot translate well into PDF.

    I also want you to know that, I receive no �nancial compensation from the Connexions website evenif you purchase the PDF version of the module.

    In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindlebooks, and placed them for sale on Amazon.com showing me as the author. I neither receivecompensation for those sales nor do I know who does receive compensation. If you purchase sucha book, please be aware that it is a copy of a module that is freely available on cnx.org and that itwas made and published without my prior knowledge.

    A�liation : I am a professor of Computer Information Technology at Austin Community Collegein Austin, TX.

    -end-

    Available for free at Connexions

  • 30 CHAPTER 1. PREFACE

    Available for free at Connexions

  • Chapter 2

    Essence of OOP

    2.1 Java1600: Objects and Encapsulation1

    Revised: Wed Mar 30 17:09:41 CDT 2016This page is included in the following Books:

    • ITSE 2321 - Object-Oriented Programming (Java) 2• Object-Oriented Programming (OOP) with Java 3

    2.1.1 Table of Contents

    • Preface (p. 31)· The essence of OOP (p. 31)· Viewing tip (p. 32)

    * Listings (p. 32)

    • Preview (p. 32)• Discussion and sample code (p. 32)• Summary (p. 37)• What's next? (p. 38)• Miscellaneous (p. 38)

    2.1.2 Preface

    This module is the �rst in a series of modules designed to teach you about the essence of Object-OrientedProgramming (OOP) using Java.

    2.1.2.1 The essence of OOP

    My dictionary provides several de�nitions for the word essence. Among those de�nitions are the following:

    • The property necessary to the nature of a thing• The most signi�cant property of a thing1This content is available online at .2http://cnx.org/contents/dzOvxPFw3http://cnx.org/contents/-2RmHFs_

    Available for free at Connexions

    31

  • 32 CHAPTER 2. ESSENCE OF OOP

    Thus, this miniseries will describe and discuss the necessary and most signi�cant aspects of OOP using Java.In other words, I will discuss the essence of OOP using Java. For the �rst few modules, I will provide thatinformation in a high-level format, devoid of any requirement to understand detailed Java syntax. In thosecases where an understanding of Java syntax is required, I will provide the necessary syntax information inthe form of supplementary notes.

    Therefore, if you have a general understanding of computer programming, you should be able to readand understand the modules in this miniseries, even if you don't have a strong background in the Javaprogramming language.

    2.1.2.2 Viewing tip

    I recommend that you open another copy of this document in a separate browser window and use thefollowing links to easily �nd and view the listings while you are reading about them.

    2.1.2.2.1 Listings

    • Listing 1 (p. 36) . Instantiating a new Radio object.• Listing 2 (p. 37) . Calling the playStation method.

    2.1.3 Preview

    In order to understand OOP, you need to understand the following three concepts:

    • Encapsulation• Inheritance• Polymorphism

    This module will concentrate on encapsulation. Encapsulation will be used as a springboard for a discussionof objects.

    A description of an object-oriented program will be provided, along with a description of an object, andhow it relates to encapsulation.

    In order to relate object-oriented programming to the real world, a car radio will be used to illustrate anddiscuss several aspects of software objects. For example, you will learn that car radios, as well as softwareobjects, have the ability to store data, along with the ability to modify or manipulate that data.

    You will learn that car radios, as well as software objects, have the ability to accept messages and toperform an action, modify their state, return a value, or some combination of the above.

    You will learn some of the jargon used in OOP, including persistence, state, messages, methods, andbehaviors.

    You will learn where objects come from, and you will learn that a class is a set of plans that can be usedto construct objects. You will learn that a Java object is an instance of a class .

    You will see a little bit of Java code, used to create an object, and then to send a message to that object(invoke a method on the object).

    You will learn about Java references and reference variables. You will also learn a little about memoryallocation for objects and variables in Java.

    2.1.4 Discussion and sample code

    Purpose of the miniseriesAs mentioned earlier, I will describe and discuss the necessary and most signi�cant aspects of OOP using

    Java.The three pillarsMost books on OOP will tell you that in order to understand OOP, you need to understand the following

    three concepts:

    Available for free at Connexions

  • 33

    • Encapsulation• Inheritance• Polymorphism

    I agree with that assessment.(Some books will also add abstraction and/or late binding to the list. I tend to think of those two topics

    as being included in one or more of the three concepts listed above.)Begin with encapsulationGenerally, speaking, these three concepts increase in di�culty going down the list from top to bottom.

    Therefore, I will begin with Encapsulation and work my way down the list in successive modules.What is an Object-Oriented Program?Many authors would answer this question something like the following:An Object-Oriented Program consists of a group of cooperating objects, exchanging messages, for the

    purpose of achieving a common objective.What is an object?An object is a software construct that encapsulates data, along with the ability to use or modify that

    data, into a software entity.What is encapsulation?An interesting description of encapsulation was provided in an article by Rocky Lhotka regarding

    VB.NET. That description reads as follows:"Encapsulation is the concept that an object should totally separate its interface from its implementation.

    All the data and implementation code for an object should be entirely hidden behind its interface.The idea is that we can create an interface (Public methods in a class) and, as long as that interface

    remains consistent, the application can interact with our objects. This remains true even if we entirelyrewrite the code within a given method thus the interface is independent of the implementation."

    I like this description, so I won't try to improve on it. However, I will try to illustrate it in the paragraphsthat follow.

    A real-world analogyAbstract concepts, such as the concept of an object or encapsulation, can often be best understood by

    comparing them to real-world analogies. One imperfect, but fairly good analogy to a software object is theradio in your car.

    The ability to store dataYour car radio probably has the ability to store data, and to allow you to use and modify that data at

    will. (However, you can only use and modify that data through use of the human interface that is providedby the manufacturer of the radio.)

    The data that can be stored in your car radio probably includes a list of �ve or more frequencies thatcorrespond to your favorite radio stations.

    Using the stored dataThe radio provides a mechanism (human interface) that allows you to use the data stored therein.When you press one of the frequency-selector buttons on the front of the radio, the radio automatically

    tunes itself to the frequency corresponding to that button. (In this case, you, the user, are sending amessage to the radio object asking it to perform a particular action.)

    If you have previously stored a favorite frequency in the storage location corresponding to that button,pressing the button (sending the message) will cause the radio station transmitting at that frequency tobe heard through the radio's speakers.

    If you have not previously stored a favorite frequency in the storage location corresponding to that button,you will probably only hear static. (That doesn't mean that the radio object failed to respond correctly tothe message. It simply means that its response was based on bad data.)

    Modifying the stored dataThe human interface also makes it possible for you to store or modify those �ve or more frequency values.

    This is done in di�erent ways for di�erent radios. On my car radio, the procedure is:

    Available for free at Connexions

  • 34 CHAPTER 2. ESSENCE OF OOP

    • Manually tune the radio to the desired frequency• Press one of the buttons and hold it down for several seconds.

    When the radio beeps, I know that the new frequency value has been stored in a storage location thatcorresponds to that particular button.

    Please change your stateWhat I have done here is to send a message to the radio object asking it to change its state. The beep

    that I hear could be interpreted as the radio object returning a value back to me indicating that the missionhas been accomplished. (Alternately, we might say that the radio object sent a message back to me.)

    We say that an object has changed its state when one or more data values stored in the object have beenmodi�ed.

    We also say that when an object responds to a message, it will usually perform an action, change itsstate, return a value, or some combination of the above.

    Please perform an actionFollowing this, when I press that button (send a message) , the radio object will be automatically tuned

    to that frequency.

    Historical note: While the ability to cause your car radio to remember your list of favoritestations may seem like a miracle of modern digital electronics, the truth is that radios had thiscapability long before they contained digital electronics. My �rst car had a radio that accomplishedthis feat using strings, pulleys, and levers.

    As I recall, in order to set the frequency for a button, I had to manually tune the radio to a stationby turning a knob, pull one of the buttons out about a quarter of an inch, and then push it in again.From that point until I did the same thing again, whenever I pressed that button, some kind of amechanical contraption caused a big rotary capacitor to turn just the right amount to tune for aparticular radio station.

    Also, I remember my grandfather having a table-model radio in the early 1940's that had radiobuttons. He used them to select his favorite stations, as he surfed the airwaves.

    (Interestingly, the term radio button has now become a part of programming jargon, signifyingcertain visual components used in graphical user interfaces.)

    Enough of that, now back to my modern car radioIf I drive to Dallas and press a button that I have associated with a particular radio station in Austin,

    I will probably hear static. In that case, I may want to change the frequency value associated with thatbutton. I can follow the same procedure described earlier to set the frequency value associated with thatbutton to correspond to one of the radio stations in Dallas. (Again, I would be sending a message to theradio object asking it to change its state.)

    JargonAs you can see from the above discussion, the world of OOP is awash with jargon, and the ability to

    translate the jargon is essential to an understanding of the published material on OOP. Therefore, as weprogress through this series of modules, I will introduce you to some of that jargon and try to help youunderstand the meaning of the jargon.

    PersistenceThe ability of your car radio to remember your list of favorite stations is often referred to as persistence.

    An object that has the ability to store and remember values is often said to have persistence.StateIt is often said that the state of an object at a particular point in time is determined by the values

    stored in the object. In our analogy, even if we own identical radios, unless the two of us have the same listof favorite radio stations, associated with the same combination of buttons, the state of your radio object atany particular point in time will be di�erent from the state of my radio object.

    Available for free at Connexions

  • 35

    Identical objects with identical states: It is perfectly OK for the two of us to own identicalradios and to cause the two radio objects to contain the same list of frequencies. Even if twoobjects have the same state at the same time, they are still separate and distinct objects. Whilethis is obvious in the real world of car radios, it may not be quite as obvious in the virtual worldof computer programming.

    Sending a messageA person who speaks in OOP-speak might say that pressing one of the frequency-selector buttons on the

    front of the radio sends a message to the radio object, asking it to perform an action (tune to a particularstation) . That person might also say that storing a new frequency that corresponds to a particular buttonentails sending a message to the radio object asking it to change its state.

    Invoking or calling a methodJava-speak is a little more speci�c than general OOP-speak. In Java-speak, we might say that pressing

    one of the selector buttons on the front of the radio invokes or calls a method on the radio object. Thebehavior of the method is to cause the object to perform an action.

    As a practical matter, the physical manifestation of sending a message to an object in Java is to causethat object to execute one of its methods.

    Similarly, we might say that storing a new frequency that corresponds to a particular button invokes asetter method on the radio object.

    (In an earlier paragraph, I said that I could follow a speci�c procedure to set the frequency valueassociated with a button to correspond to one of the radio stations in Dallas. Note the use of the words setand setter in this jargon.)

    BehaviorIn addition to state, objects are often also said to have behavior . The overall behavior of an object is

    determined by the combined behaviors of its individual methods.For example, one of the behaviors exhibited by our radio object is the ability to play the radio station

    at a particular frequency. When a frequency is selected by pressing