Java, Java, Java Object-Oriented Problem Solving Third Edition R. Walde Trinity College Hartford, CT June 25, 2017 This work is licensed under a Creative Commons Attribution 4.0 International License (CC BY 4. This book was previously published by Pearson Education, Inc. Preface to the Open Source Edition Java, Java, Java, 3e was previously published by Pearson Education, Inc.
The first edition (2000) and the second edition (2003) were published by Prentice-Hall. In 2010 Pearson Education, Inc. reassigned the copyright to the authors, and we are happy now to be able to make the book available under an open source license. This PDF edition of the book is available under a Creative Commons Attribution 4.0 International License, which allows the book to be used, modified, and shared with attribution: (https://creativecommons.org/licenses/by/4.
– Ralph Morelli and Ralph Walde – Hartford, CT – December 30, 2016 i ii Preface to the Third Edition We have designed this third edition of Java, Java, Java to be suitable for a typical Introduction to Computer Science (CS1) course or for a slightly more advanced Java as a Second Language course. This edition retains the “objects first” approach to programming and problem solving that was characteristic of the first two editions. Throughout the text we emphasize careful coverage of Java language features, introductory programming concepts, and object-oriented design principles. The third edition retains many of the features of the first two editions, including: • Early Introduction of Objects • Emphasis on Object Oriented Design (OOD) • Unified Modeling Language (UML) Diagrams • Self-study Exercises with Answers • Programming, Debugging, and Design Tips.
• From the Java Library Sections • Object-Oriented Design Sections • End-of-Chapter Exercises • Companion Web Site, with Power Points and other Resources The In the Laboratory sections from the first two editions have been moved onto the book’s Companion Web Site. Table 1 shows the Table of Contents for the third edition. What’s New in the Third Edition The third edition has the following substantive changes: • Although the book retains its emphasis on a “running example” that is revisited in several chapters, the CyberPet examples have been replaced with a collection of games and puzzle examples. The CyberPet examples from earlier editions will be available on the Companion Web Site.
iii iv Table 1: Table of Contents for the Third Edition. Chapter Topic Chapter 0 Computers, Objects, and Java (revised) Chapter 1 Java Program Design and Development Chapter 2 Objects: Defining, Creating, and Using Chapter 3 Methods: Communicating with Objects (revised) Chapter 4 Input/Output: Designing the User Interface (new) Chapter 5 Java Data and Operators Chapter 6 Control Structures Chapter 7 Strings and String Processing Chapter 8 Inheritance and Polymorphism (new) Chapter 9 Arrays and Array Processing Chapter 10 Exceptions: When Things Go Wrong Chapter 11 Files and Streams Chapter 12 Recursive Problem Solving Chapter 13 Graphical User Interfaces Chapter 14 Threads and Concurrent Programming Chapter 15 Sockets and Networking (expanded) Chapter 16 Data Structures: Lists, Stacks, and Queues (revised and expanded) • Chapters 0 (Computers, Objects, and Java) and 1 (Java Program De- sign and Development) have been substantially reorganized and rewritten. The new presentation is designed to reduce the pace with which new concepts are introduced. The treatment of object- oriented (OO) and UML concepts has also been simplified, and some of the more challenging OO topics, such as polymorphism, have been moved to a new Chapter 8.
• The new Java 1.5 Scanner class is introduced in Chapter 2 and is used to perform simple input operations. • Chapter 4 (Input/Output: Designing the User Interface) has been completely written. Rather than relying primarily on applet inter- faces, as in the second edition, this new chapter provides indepen- dent introductions to both a command-line interface and a graphi- cal user interface (GUI). Instructors can choose the type of interface that best suits their teaching style.
The command-line interface is based on the BufferedReader class and is used throughout the rest of the text. The GUI is designed to work with either graphi- cal applications or applets. Both approaches are carefully presented to highlight the fundamentals of user-interface design. The chapter concludes with an optional section that introduces file I/O using the new Scanner class.
• Much of the discussion of inheritance and polymorphism, which was previously woven through the first five chapters in the second edition, has been integrated into a new Chapter 8. • An optional graphics track is woven throughout the text. Beginning with simple examples in Chapters 1 and 2, this track also includes v some of the examples that were previously presented in Chapter 10 of the second edition. • Chapter 15, on Sockets and Networking, is expanded to cover some of the more advanced Java technologies that have emerged, includ- ing servlets and Java Server Pages.
• Chapter 16, on Data Structures, has been refocused on how to use data structures. It makes greater use of Java’s Collection Framework, including the LinkedList and Stack classes and the List inter- face. It has been expanded to cover some advanced data structures, such as sets, maps, and binary search trees. The Essentials Edition An Essentials Edition of the third edition, which will include Chapters 0- 12, will be published as a separate title.
The Essentials Edition will cover those topics (Chapters 0-9) that are covered in almost all introductory (CS1) courses, but it will also include topics (Exceptions, File I/O, and Recursion) that many CS1 instructors have requested. Why Start with Objects? The Third Edition still takes an objects-early approach to teaching Java, with the assumption that teaching beginners the “big picture” early gives them more time to master the principles of object-oriented programming. This approach seems now to have gained in popularity as more and more instructors have begun to appreciate the advantages of the object-oriented perspective. If OO concepts are introduced late, it is much too easy to skip over them when push comes to shove in the course.
The first time I taught Java in our CS1 course I followed the same ap- proach I had been taking in teaching C and C++ — namely, start with the basic language features and structured programming concepts and then, somewhere around midterm, introduce object orientation. This approach was familiar, for it was one taken in most of the textbooks then available in both Java and C++. One problem with this approach was that many students failed to get the big picture. They could understand loops, if-else constructs, and arith- metic expressions, but they had difficulty decomposing a programming problem into a well-organized Java program.
Also, it seemed that this procedural approach failed to take advantage of the strengths of Java’s object orientation. Why teach an object-oriented language if you’re going to treat it like C or Pascal? I was reminded of a similar situation that existed when Pascal was the predominant CS1 language. Back then the main hurdle for beginners was procedural abstraction — learning the basic mechanisms of procedure call vi and parameter passing and learning how to design programs as a collec- tion of procedures. Oh! Pascal!, my favorite introductory text, was typical of a “procedures early” approach.
It covered procedures and parameters in Chapter 2, right after covering the assignment and I/O constructs in Chapter 1. It then covered program design and organization in Chap- ter 3. It didn’t get into loops, if-else, and other structured programming concepts until Chapter 4 and beyond. Today, the main hurdle for beginners is the concept of object abstraction.
Beginning programmers must be able to see a program as a collection of interacting objects and must learn how to decompose programming prob- lems into well-designed objects. Object orientation subsumes both proce- dural abstraction and structured programming concepts from the Pascal days. Teaching objects-early takes a top-down approach to these three im- portant concepts. The sooner you begin to introduce objects and classes, the better the chances that students will master the important principles of object orientation.
Java is a good language for introducing object orientation. Its object model is better organized than C++. In C++ it is easy to “work around” or completely ignore OO features and treat the language like C. In Java there are good opportunities for motivating the discussion of object orien- tation.
For example, it’s almost impossible to discuss GUI-based Java ap- plications without discussing inheritance and polymorphism. Thus rather than using contrived examples of OO concepts, instructors can use some of Java’s basic features — the class library, Swing and GUI components — to motivate these discussions in a natural way. Organization of the Text The book is still organized into three main parts. Part I (Chapters 0-4) in- troduces the basic concepts of object orientation and the basic features of the Java language.
Part II (Chapters 5-9) focuses on remaining language el- ements, including data types, control structures, string and array process- ing, and inheritance and polymorphism. Part III (Chapters 10-16) covers advanced topics, including exceptions, file I/O, recursion, GUIs, threads and concurrent programming, sockets and networking, data structures, servlets, and Java Server Pages. The first two parts make up the topics that are typically covered in an introductory CS1 course. The chapters in Part III are self-contained and can be selectively added to the end of a CS1 course if time permits.
The first part (Chapters 0 through 4) introduces the basic concepts of object orientation, including objects, classes, methods, parameter passing, information hiding, and a little taste of inheritance, and polymorphism. The primary focus in these chapters is on introducing the basic idea that an object-oriented program is a collection of objects that communicate and cooperate with each other to solve problems. Java language elements are introduced as needed to reinforce this idea. Students are given the basic building blocks for constructing Java programs from scratch.
Although the programs in the first few chapters have limited function- ality in terms of control structures and data types, the priority is placed vii Table 2: A one-semester course. Weeks Topics Chapters 1 Object Orientation, UML Chapter 0 Program Design and Development Chapter 1 2-3 Objects and Class Definitions Chapter 2 Methods and Parameters Chapter 3 Selection structure (if-else) 4 User Interfaces and I/O Chapter 4 5 Data Types and Operators Chapter 5 6–7 Control Structures (Loops) Chapter 6 Structured Programming 8 String Processing (loops) Chapter 7 9 Inheritance and Polymorphism Chapter 8 10 Array Processing Chapter 9 11 Recursion Chapter 12 12 Advanced Topic (Exceptions) Chapter 10 13 Advanced Topic (GUIs) Chapter 11 Advanced Topic (Threads) Chapter 15 on how objects are constructed and how they interact with each other through method calls and parameter passing. The second part (Chapters 5 through 9) focuses on the remaining lan- guage elements, including data types and operators (Chapter 5), control structures (Chapter 6), strings (Chapter 7), and arrays (Chapter 9). It also provides thorough coverage of inheritance and polymorphism, the primary mechanisms of object orientation: (Chapter 8).
Topics from these chapters can be used selectively depending on instructor and student interest. Throughout the book, key concepts are introduced through simple, easy-to-grasp examples. Many of the concepts are used to create a set of games, which are used as a running example throughout the text. Our pedagogical approach focuses on design.
Rather than starting of with lan- guage details, programming examples are carefully developed with an emphasis on the principles of object-oriented design. Table2 provides an example syllabus from our one-semester CS1 course. Our semester is 13 weeks (plus one reading week during which classes do not meet). We pick and choose from among the advanced topics during the last two weeks of the course, depending on the interests and skill levels of the students.
Ralph Morelli June 25, 2017 viii Contents 0 Computers, Objects, and Java 1 0.2 What Is a Computer? .3 Networks, the Internet and the World Wide Web .4 Why Study Programming? .7 What Is Object-Oriented Programming?. 11 1 Java Program Design and Development 23 1.2 Designing Good Programs .