Page i
John Dean
Park University
Raymond Dean
University of Kansas
Page ii
INTRODUCTION TO PROGRAMMING WITH JAVA
Published by McGraw Hill LLC, 1325 Avenue of the Americas, New York, NY 10121. Copyright 2021 by McGraw Hill LLC. All rights reserved. Printed in the United States of America. No part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written consent of McGraw Hill LLC, including, but not limited to, in any network or other electronic storage or transmission, or broadcast for distance learning.
Some ancillaries, including electronic and print components, may not be available to customers outside the United States.
This book is printed on acid-free paper.
1 2 3 4 5 6 7 8 9 LCR 24 23 22 21 20
ISBN 978-1-26057524-8
MHID 1-260-57524-1
Cover Image: Shutterstock/Brian Lasenby
All credits appearing on page or at the end of the book are considered to be an extension of the copyright page.
The Internet addresses listed in the text were accurate at the time of publication. The inclusion of a website does not indicate an endorsement by the authors or McGraw Hill LLC, and McGraw Hill LLC does not guarantee the accuracy of the information presented at these sites.
mheducation.com/highered
Page iii
edication
To Stan and Kate
Page iv
About the Authors
John Dean is an Associate Professor in the Computer Science and Information Systems Department at Park University. He earned a Ph.D. degree in computer science from Nova Southeastern University and an M.S. degree in computer science from the University of Kansas. He is Java certified and has worked in industry as a software engineer and project manager, specializing in Java and various web technologiesJavaScript, JavaServer Pages, and servlets. He has taught a full range of computer science courses, including Java programming and Java-based web programming. He has authored a web programming textbook with a focus on client-side technologies HTML5, CSS, and JavaScript.
Raymond Dean is a Professor Emeritus, Electrical Engineering and Computer Science, University of Kansas. He earned an M.S. degree from MIT and a Ph.D. degree from Princeton University. As a professional engineer in the HVAC industry, he wrote computer programs that design air distribution systems and analyze energy consumption and sound propagation in buildings. At the University of Kansas, he taught microprocessor programming, data structures, and other courses in electrical engineering and computer science.
Page v
Contents
Page vi
Page vii
Page viii
Page ix
Page x
Preface
In this book, we lead you on a journey into the fun and exciting world of computer programming. Throughout your journey, well provide you with lots of problem-solving practice. After all, good programmers need to be good problem solvers. Well show you how to implement your problem solutions with Java programs. We provide a plethora of examples, some short and focused on a single concept, some longer and more real world. We present the material in a conversational, easy-to-follow manner aimed at making your journey a pleasant one. When youre done with the book, you should be a proficient Java programmer.
Our textbook targets a wide range of readers. Primarily, it targets students in a standard college-level Introduction to Programming course or course sequence where no prerequisite programming experience is assumed. We have included the topics recommended by the College Board for high school students studying for advanced placement (AP) in computer science. So this text should be good for those students as well.
In addition to targeting students with no prerequisite programming experience, our textbook targets industry practitioners and college-level students who have some programming experience and want to learn Java. This second set of readers can skip the early chapters on general programming concepts and focus on the features of Java that differ from the languages that they already know. In particular, because C++ and Java are similar, readers with a C++ background should be able to cover the textbook in a single three-credit-hour course. (But we should reiterate for those of you with no programming experience: No prerequisite programming experience is required in order to use this text.)
Finally, our textbook targets those who are learning Java on their own, outside of a classroom environment. This third set of readers should read the entire textbook at a pace determined on a case-by-case basis.
Whats New in This Edition?
The changes in this edition are big and small. Big changes include new chapters, reorganized chapter sections, new programming constructs, new program examples, and new exercises. Smaller changes include updating explanations and anecdotes. Weve combed the entire book for opportunities to improve the books clarity and readability. The following list highlights the more significant changes that weve made to this edition.
Introductory Chapter
To keep up with the computer industrys growth, weve made quite a few changes to , such as updating the information in the computer hardware and Java history sections.
Switching Constructs
Java 12 and Java 13 made improvements to the venerable switch statement, and this edition describes those improvements. We use the new switching techniques (multiple comma-separated case constants and no break statements) for programs throughout the book. And we use switch statements versus switch expressions according to what the problem calls for. If youre a fan of the old-style switch statement, no worries, we provide a description early on which will help you with legacy code.
Local Variable Type Inferencing
Java 10 introduced the ability to use var as a type (rather than int, double, etc.) for a local variable declaration where the declaration is part of an initialization. We describe the new syntax, but for self-documentation reasons, we stick with traditional explicit type declarations for the most part.
Page xi
Name Change for Static Variables and Static Methods
The powers that be (the Oracle documentation folks) now use the terms static variable and static method for what used to be known as class variable and class method, so weve updated accordingly.
Miscellaneous Java API Library Updates
With the new Java releases since the second edition, there have been quite a few updates to the Java API library. Weve updated our discussions and programs with new API method and constructor calls when appropriate. Most of our new API content can be found in our GUI coverage, but there are other API changes sprinkled throughout the book. For example, with Javas deprecation of the wrapper class constructors, weve refactored our programs to rely on the wrapper classes valueOf methods.