ebook img

Sams Teach Yourself Java in 21 Days (Covers Java 11/12) PDF

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

Preview Sams Teach Yourself Java in 21 Days (Covers Java 11/12)

ptg28406312 d e n m LESSON 7 Exceptions aThreads LESSON 14 Developing Swing Applications LESSON 21 Writing a Gawith Java d 2D LESSON 6 Packages, Interfaces, anOther Class Features LESSON 13 Creating JavaGraphics LESSON 20 Making Web Service Requests LESSON 5 Creating Classes and Methods LESSON 12 Responding to User Input LESSON 19 Reading and Writing RSS Feeds APPENDIX E Programming with the Java Development Kit on ce a Kit 1 Days LESSON 4 Lists, Logic, and Loops LESSON 11 Arranging Components a User Interfa LESSON 18 Accessing Databases with JDBC and Derby APPENDIX D Using the JavDevelopment ptg28406312 2 n g Java i™ LESSON 3 Working with Objects LESSON 10 Building an Interface LESSON 17 CommunicatinOver HTTP APPENDIX C This Book’s Website hYourself a Language LESSON 2 The ABCs of Programming a Class Library LESSON 9 Creating aGraphical UserInterface ogramming LESSON 16 Working with Input and Output APPENDIX B Fixing Package Not Visible Errors in NetBeans v v r c a a P TeaSams PART 1: The J LESSON 1 Getting Started with Java PART 2: The J LESSON 8 Data Structures PART 3: Java LESSON 15 Using Inner Classes and Lambda Expressions Appendixes APPENDIX A Using the NetBeans IDE This page intentionally left blank ptg28406312 Rogers Cadenhead SamsTeachYourself Java ptg28406312 in 21 Days Eighth Edition 221 River Street Hoboken, NJ 07030 USA Sams Teach Yourself Java in 21 Days, Eighth Edition Editor-in-Chief Mark Taub Copyright © 2020 by Pearson Education, Inc. Acquisitions Editor All rights reserved. This publication is protected by copyright, and permission must be Greg Doench obtained from the publisher prior to any prohibited reproduction, storage in a retrieval sys- Development Editor tem, or transmission in any form or by any means, electronic, mechanical, photocopying, Mark Taber recording, or likewise. For information regarding permissions, request forms, and the appro- priate contacts within the Pearson Education Global Rights & Permissions Department, Project Editor please visit www.pearson.com/permissions. Mandie Frank ISBN-13: 978-0-672-33795-6 Copy Editor ISBN-10: 0-672-33795-9 Kitty Wilson Technical Editor Library of Congress Control Number: 2019912868 Boris Minkin ScoutAutomatedPrintCode Design Chuti Prasertsith Trademarks Proofreading All terms mentioned in this book that are known to be trademarks or service marks have Donna Mulder been appropriately capitalized. Sams Publishing cannot attest to the accuracy of this infor- mation. Use of a term in this book should not be regarded as affecting the validity of any Indexer trademark or service mark. Cheryl Lenser Composition Microsoft and/or its respective suppliers make no representations about the suitability of codeMantra the information contained in the documents and related graphics published as part of the services for any purpose all such documents and related graphics are provided “as is” without warranty of any kind. Microsoft and/or its respective suppliers hereby disclaim all warranties and conditions with regard to this information, including all warranties and conditions of merchantability, whether express, implied or statutory, fitness for a particular ptg28406312 purpose, title and non-infringement. In no event shall Microsoft and/or its respective suppliers be liable for any special, indirect or consequential damages or any damages whatsoever resulting from loss of use, data or profits, whether in an action of contract, negligence or other tortious action, arising out of or in connection with the use or perfor- mance of information available from the services. The documents and related graphics contained herein could include technical inaccuracies or typographical errors. Changes are periodically added to the information herein. Microsoft and/or its respective suppliers may make improvements and/or changes in the product(s) and/or the program(s) described herein at any time. Partial screen shots may be viewed in full within the software version specified. Microsoft® Windows®, and Microsoft Office® are registered trademarks of the Microsoft Corporation in the U.S.A. and other countries. This book is not sponsored or endorsed by or affiliated with the Microsoft Corporation. Warning and Disclaimer Every effort has been made to make this book as complete and as accurate as possible, but no warranty or fitness is implied. The information provided is on an “as is” basis. The author and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this book. Special Sales For information about buying this title in bulk quantities, or for special sales opportunities (which may include electronic versions; custom cover designs; and content particular to your business, training goals, marketing focus, or branding interests), please contact our corporate sales department at [email protected] or (800) 382-3419. For government sales inquiries, please contact [email protected]. For questions about sales outside the U.S., please contact [email protected]. Figure Credits Figure 1-1 Screenshot of Apache NetBeans IDE 11.0 ©2019 Oracle Figure 2-1 Screenshot of Apache NetBeans IDE 11.0 ©2019 Oracle Figure 2-2 Screenshot of Apache NetBeans IDE 11.0 ©2019 Oracle Figure 3-1 Screenshot of Apache NetBeans IDE 11.0 ©2019 Oracle Figure 3-2 Screenshot of Apache NetBeans IDE 11.0 ©2019 Oracle Figure 3-3 Screenshot of Apache NetBeans IDE 11.0 ©2019 Oracle Figure 3-4 Screenshot of Apache NetBeans IDE 11.0 ©2019 Oracle Figure 3-5 Screenshot of Apache NetBeans IDE 11.0 ©2019 Oracle Figure 4-1 Screenshot of Apache NetBeans IDE 11.0 ©2019 Oracle Figure 4-2 Screenshot of Apache NetBeans IDE 11.0 ©2019 Oracle Figure 4-3 Screenshot of Apache NetBeans IDE 11.0 ©2019 Oracle Figure 4-4 Screenshot of Apache NetBeans IDE 11.0 ©2019 Oracle Figure 4-5 Screenshot of Apache NetBeans IDE 11.0 ©2019 Oracle Figure 5-1 Screenshot of Apache NetBeans IDE 11.0 ©2019 Oracle Figure 5-2 Screenshot of Apache NetBeans IDE 11.0 ©2019 Oracle Figure 5-3 Screenshot of Apache NetBeans IDE 11.0 ©2019 Oracle Figure 5-4 Screenshot of Apache NetBeans IDE 11.0 ©2019 Oracle Figure 5-5 Screenshot of Apache NetBeans IDE 11.0 ©2019 Oracle ptg28406312 Figure 5-6 Screenshot of Apache NetBeans IDE 11.0 ©2019 Oracle Figure 5-7 Screenshot of Apache NetBeans IDE 11.0 ©2019 Oracle Figure 6-1 Screenshot of Apache NetBeans IDE 11.0 ©2019 Oracle Figure 6-2 Screenshot of Apache NetBeans IDE 11.0 ©2019 Oracle Figure 6-3 Screenshot of Apache NetBeans IDE 11.0 ©2019 Oracle Figure 6-4 Screenshot of Apache NetBeans IDE 11.0 ©2019 Oracle Figure 7-1 Screenshot of Apache NetBeans IDE 11.0 ©2019 Oracle Figure 7-2 Screenshot of Apache NetBeans IDE 11.0 ©2019 Oracle Figure 8-2 Screenshot of Apache NetBeans IDE 11.0 ©2019 Oracle Figure 8-3 Screenshot of Apache NetBeans IDE 11.0 ©2019 Oracle Figure 8-6 Screenshot of Apache NetBeans IDE 11.0 ©2019 Oracle Figure 9-1 Screenshot of Displaying a frame ©2019 Oracle Figure 9-2 Screenshot of ButtonFrame ©2019 Oracle Figure 9-3 Screenshot of Icon frame ©2019 Oracle Figure 9-4 Screenshot of NetBeans Files pane ©2019 Oracle Figure 9-5 Screenshot of Authenticator application ©2019 Oracle Figure 9-6 Screenshot of FormatFrame ©2019 Oracle Figure 9-7 Screenshot of FormatFrame2 ©2019 Oracle Figure 9-8 Screenshot of Subscriptions ©2019 Oracle Figure 9-9 Screenshot of Java Class Library©2019 Oracle Figure 10-1 Screenshot of dialog box ©2019 Oracle Figure 10-2 Screenshot of resulting dialog ©2019 Oracle vi Sams Teach Yourself Java in 21 Days Figure 10-3 Screenshot of input dialog ©2019 Oracle Figure 10-4 Screenshot of message dialog ©2019 Oracle Figure 10-5 Screenshot of option dialog ©2019 Oracle Figure 10-6 Screenshot of window of the FeedInfo ©2019 Oracle Figure 10-7 Screenshot of JSlider ©2019 Oracle Figure 10-8 Screenshot of dockable toolbar ©2019 Oracle Figure 10-9 Screenshot of progress bar ©2019 Oracle Figure 10-10 Screenshot of Tabbed panes ©2019 Oracle Figure 11-1 Screenshot of Alphabet application ©2019 Oracle Figure 11-2 Screenshot of user interface ©2019 Oracle Figure 11-3 Screenshot of Bunch ©2019 Oracle Figure 11-4 Screenshot of Border ©2019 Oracle Figure 11-5 Screenshot of Survey ©2019 Oracle Figure 12-1 Screenshot of Titlebar ©2019 Oracle Figure 12-2 Screenshot of Calculator application . ©2019 Oracle Figure 12-3 Screenshot of Syndication format ©2019 Oracle Figure 12-4 Screenshot of MousePrank application ©2019 Oracle Figure 12-5 Screenshot of KeyChecker application ©2019 Oracle ptg28406312 Figure 13-2 Screenshot of Font metrics ©2019 Oracle Figure 13-4 Screenshot of GradientPaint ©2019 Oracle Figure 13-9 Screenshot of Map Application ©2019 Oracle Figure 14-1 Screenshot of Java 21 ©2019 Oracle Figure 14-4 Screenshot of Java 21 ©2019 Oracle Figure 15-1 Screenshot of Java 21 ©2019 Oracle Figure 15-2 Screenshot of Java 21 ©2019 Oracle Figure 15-3 Screenshot of Java 21 ©2019 Oracle Figure 16-1 Screenshot of Java 21 ©2019 Oracle Figure 16-2 Screenshot of NetBeans java ©2019 Oracle Figure 16-3 Screenshot of Java 21 ©2019 Oracle Figure 16-4 Screenshot of Java 21 ©2019 Oracle Figure 16-5 Screenshot of Java 21 ©2019 Oracle Figure 17-1 Screenshot of Java 21 ©2019 Oracle Figure 17-2 Screenshot of Java 21 ©2019 Oracle Figure 17-3 Screenshot of Java 21 ©2019 Oracle Figure 17-4 Screenshot of Command Prompt ©Microsoft 2019 Figure 17-5 Screenshot of Java 21 ©2019 Oracle Figure 17-6 Screenshot of Java 21 ©2019 Oracle Figure 18-1 Screenshot of Java DB database ©2019 Oracle Figure 18-2 Screenshot of Java DB database ©2019 Oracle Figure 18-3 Screenshot of Java DB database ©2019 Oracle Figure Credits vii Figure 18-4 Screenshot of Java DB database ©2019 Oracle Figure 18-5 Screenshot of Java code ©2019 Oracle Figure 18-6 Screenshot of Java DB database ©2019 Oracle Figure 18-7 Screenshot of Apache NetBeans IDE 11.0 ©Netbeans Figure 18-8 Screenshot of Apache NetBeans IDE 11.0 ©Netbeans Figure 19-1 Screenshot of Java 21 ©2019 Oracle Figure 19-2 Screenshot of Java 21 ©2019 Oracle Figure 19-3 Screenshot of Java 21 ©2019 Oracle Figure 20-1 Screenshot of Java 21 ©2019 Oracle Figure 21-1 Screenshot of Java 21 ©2019 Oracle Figure A-1 Screenshot of Apache NetBeans IDE 11.0 ©2019 Oracle Figure A-2 Screenshot of Apache NetBeans IDE 11.0 ©2019 Oracle Figure A-3 Screenshot of Apache NetBeans IDE 11.0 ©2019 Oracle Figure A-4 Screenshot of Apache NetBeans IDE 11.0 ©2019 Oracle Figure A-5 Screenshot of Apache NetBeans IDE 11.0 ©2019 Oracle Figure A-6 Screenshot of Apache NetBeans IDE 11.0 ©2019 Oracle Figure D-1 Screenshot of Command Prompt ©2019 Microsoft Figure D-2 Screenshot of Command Prompt ©2019 Microsoft ptg28406312 Figure D-3 Screenshot of Command Prompt ©2019 Microsoft Figure D-4 Screenshot of Command Prompt ©2019 Microsoft Figure D-5 Screenshot of Environment Variables ©2019 Microsoft Figure D-6 Screenshot of Environment variable ©2019 Microsoft Figure D-7 Screenshot of Command Prompt ©2019 Microsoft Figure E-1 Screenshot of NetBeans Project ©2019 Oracle Cover xtock/Shutterstock Contents at a Glance Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 PART I The Java Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7 1 Getting Started with Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9 2 The ABCs of Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37 3 Working with Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .67 4 Lists, Logic, and Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .93 5 Creating Classes and Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .123 6 Packages, Interfaces, and Other Class Features . . . . . . . . . . . . . . . . . . . . . . . . . .155 7 Exceptions and Threads. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .189 PART II The Java Class Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .221 8 Data Structures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .223 9 Creating a Graphical User Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .253 10 Building an Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .283 11 Arranging Components on a User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .309 12 Responding to User Input. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .333 ptg28406312 13 Creating Java2D Graphics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .359 14 Developing Swing Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .383 PART III Java Programming. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .401 15 Using Inner Classes and Lambda Expressions. . . . . . . . . . . . . . . . . . . . . . . . . . . .403 16 Working with Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .423 17 Communicating Over HTTP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .451 18 Accessing Databases with JDBC and Derby. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .483 19 Reading and Writing RSS Feeds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .505 20 Making Web Service Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .529 21 Writing a Game with Java. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .549 APPENDIXES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .577 A Using the NetBeans Integrated Development Environment . . . . . . . . . . . . . . . . . . .579 B Fixing Package Not Visible Errors in NetBeans. . . . . . . . . . . . . . . . . . . . . . . . . . . .589 C This Book’s Website . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .593 D Using the Java Development Kit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .595 E Programming with the Java Development Kit . . . . . . . . . . . . . . . . . . . . . . . . . . . . .611 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .633 Table of Contents Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 How This Book Is Organized ....................................................................................................2 Who Should Read This Book ...................................................................................................4 Conventions Used in This Book ...............................................................................................5 PART I The Java Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7 LESSON 1: Getting Started with Java 9 The Java Language ..................................................................................................................10 History of the Language ...............................................................................................10 Introduction to Java ......................................................................................................11 Selecting a Development Tool ......................................................................................12 Object-Oriented Programming ................................................................................................13 Objects and Classes .................................................................................................................14 ptg28406312 Attributes and Behavior ..........................................................................................................16 Attributes of a Class of Objects ...................................................................................17 Behavior of a Class of Objects .....................................................................................18 Creating a Class ............................................................................................................18 Running the Program ....................................................................................................22 Organizing Classes and Class Behavior ..................................................................................25 Inheritance ....................................................................................................................25 Creating a Class Hierarchy ...........................................................................................27 Inheritance in Action ....................................................................................................29 Packages .......................................................................................................................31 Summary .................................................................................................................................32 Q&A ........................................................................................................................................33 Quiz .........................................................................................................................................34 Questions ......................................................................................................................34 Answers ........................................................................................................................34 Certification Practice ...............................................................................................................35 Exercises ..................................................................................................................................35 LESSON 2: The ABCs of Programming 37 Statements and Expressions ....................................................................................................38 Variables and Data Types ........................................................................................................38

See more

The list of books you might like

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