Important Announcement
PubHTML5 Scheduled Server Maintenance on (GMT) Sunday, June 26th, 2:00 am - 8:00 am.
PubHTML5 site will be inoperative during the times indicated!

Home Explore 24 hours clearly learned the English version of Java

24 hours clearly learned the English version of Java

Published by cliamb.li, 2014-07-24 12:00:23

Description: Sams Teach Yourself Java™in 24 Hours,Sixth Edition
Copyright © 2012 by Sams Publishing
All rights reserved. No part of this book shall be reproduced,stored in a retrieval system,
or transmitted by any means,electronic,mechanical,photocopying,recording,or otherwise,without written permission from the publisher. No patent liability is assumed with
respect to the use of the information contained herein. Although every precaution has
been taken in the preparation of this book,the publisher and author assume no responsibility for errors or omissions. Nor is any liability assumed for damages resulting from the
use of the information contained herein.
ISBN-13: 978-0-672-33575-4
ISBN-10: 0-672-33575-1
Library of Congress Cataloging-in-Publication Data:
Cadenhead,Rogers.
Sams teach yourself Java in 24 hours / Rogers Cadenhead.
p. cm.
ISBN-13: 978-0-672-33575-4 (pbk.)
ISBN-10: 0-672-33575-1 (pbk.)
1. Java (Computer program language) I. Title.
QA76.73.J38C335 2012
005.13’3—dc23
2011038994
Printe

Search

Read the Text Version

392 APPENDIX D: Setting Up an Android Development Environment 5. Expand this item by clicking the arrow next to it. You see several subitems for Android-related tools you can add to Eclipse, as shown in Figure D.1. FIGURE D.1 Adding new plug-ins to Eclipse. ptg7068951 6. Select the checkboxes for Android DDMS and Android Development Tools. (You also can add the other tools such as the Android Hierarchy Viewer, but you won’t see them at the start of your Android programming.) 7. Click Next to review the licensing agreement and check whether any- thing else needs to be installed. When you reach the end of the wiz- ard, click Finish. After the plug-in has been installed, close Eclipse and run it again. Your preferences in Eclipse must be checked to make sure the IDE can find the Android SDK. To check this, undertake the following: 1. Choose the menu command Window, Preferences. The Preferences dialog opens with a list of categories running along the left side. 2. Click Android to see the general Android preferences.

Installing the Android Plug-in for Eclipse 393 3. Make sure the SDK Location field contains the folder where the Android SDK was installed. If it doesn’t, click Browse to navigate to that folder and choose it with a file folder dialog. 4. After the SDK has been located, you see a table with a list of SDK targets, as depicted in Figure D.2. FIGURE D.2 Setting Android preferences in Eclipse. ptg7068951 These targets are the versions of Android you can create apps for with the SDK. Android apps must designate the earliest version of Android they are created to work on. Click OK to close the dialog and save your preferences. With the Android plug-in installed and the SDK located properly, you should find new menu commands in Eclipse. One of them is Window, Android SDK, and AVD Manager. If this command is absent, close Eclipse and restart it. You can use the manager to keep the SDK up to date. Choose Window, Android SDK, and AVD Manager. The manager opens. Click Installed Packages in the left pane to see which SDK components are installed on your computer.

394 APPENDIX D: Setting Up an Android Development Environment Click Available Packages to see what else is available that hasn’t been installed yet. Check a box for one of the packages. Eclipse checks the contents of that package and presents check boxes for the specific things you need to install, as shown in Figure D.3. FIGURE D.3 Installing new packages for the Android SDK. ptg7068951 When you have checked the boxes for packages you want and have unchecked the ones you don’t, click Install Selected. You should periodically check for new updates. Android is being devel- oped at a furious pace as new phones and other devices hit the market and the SDK must support them. Setting Up Your Phone The Android SDK includes an emulator that acts like an Android phone and can run the apps you create. This comes in handy as you’re writing an app because you can get it working in a test environment, but at some point you need to see how it works on an actual Android phone (or other device). You can deploy apps you write with the SDK on an Android device over your computer’s USB connection. You can use the same cord you use to transfer files to and from the device. Before connecting the cord, you must enable USB debugging on the phone by following these steps:

Setting Up Your Phone 395 1. On the phone’s Home screen, choose Menu, Settings. The Settings app opens. 2. Choose Applications, Development and check the USB debugging box. Other devices might have this option elsewhere in the settings. It is called something like USB connection mode, USB debugging, or the like. The Android site at http://developer.android.com has documentation for how to set this for different Android devices. Connect the USB cord to your computer and the other end to your phone. A small bug-like Android icon should appear along the top edge of the device alongside the time and icons for connection bars and the battery. Drag the top bar down. You should see the USB Debugging Connected and USB Connected messages (see Figure D.4). This sets up your phone, but your computer also might require configura- tion to be able to connect to the device. If you have never connected to the FIGURE D.4 phone over a USB cord, check your phone’s documentation for how to do Using an Android phone in USB ptg7068951 this. You might need to install a driver from a CD that came with the debugging mode. phone or the manufacturer’s website. On Windows, the Android SDK and AVD Manager run from within Eclipse, and you can use them to download the USB Driver Package, a col- lection of drivers for various phones and other devices, and other packages related to your device. Choose Window, Android SDK, and AVD Manager to see what’s available. During Hour 24, you use the Android development tools to create and run an Android app. If everything’s set up correctly, it should run properly on both the emulator and an Android phone.

This page intentionally left blank ptg7068951

INDEX NUMERICS + (plus sign) accessor methods, 142 addition operator (+), 56 ActionListener interface, 202, 271 2D graphics, 330 concatenation operator, 68-69 actionPerformed() method, 202-203, arcs, 332-333, 341 += operator, 69 212, 276 circles, 332 –– (decrement operator), 56 activities ellipses, 332 - (minus sign), 56 Hour 1, 12 lines, 330 / (division operator), 56 Hour 2, 24 ptg7068951 PiePanel application, 333 / (forward slash) character, 284 Hour 3, 38 PiePanel.java source // (double slashes), 17 Hour 4, 48 code, 338 Hour 5, 64 // (two slash characters), 258 PieSlice class, 335-336 Hour 6, 77 = (equal sign), 52, 54 rectangles, 331 Hour 7, 94 == (equality operator), 81 Hour 8, 106 ? (question mark), 86-87 Hour 9, 119 @Override annotation, 314 Hour 10, 136 @WebMethod annotation, 315 SYMBOLS Hour 11, 154 Hour 12, 168 < > (angle brackets), 238 Hour 13, 186 ; (semicolon), 17, 22, 102 A Hour 14, 200 != (inequality operator), 81 Hour 15, 218 $ (dollar sign), 54 Absolute program, 34 Hour 16, 233 % operator, 56 Abstract Windowing Toolkit. See AWT Hour 17, 248 ’ (single quotation mark), 51, 67 access control Hour 18, 264 / (backslash), 67 definition of, 138 Hour 19, 281 “ (double quotation mark), 51 methods, 142 Hour 20, 297, 311 > (greater than operator), 81 variables, 138 Hour 21, 326, 342 \n (newline character), 180 default, 139 Activity class, 346 (_) (underscore) character, 53 private variables, 139 Add Library dialog box, 303 * (multiplication operator), 56 protected variables, 139

398 Add Repository dialog box Add Repository dialog box, 391 Annotations, applying, 314-315 receiving, 243 add() method, 157 apostrophes (‘), 51 ShowWeight applet add(Component) method, 228 Apple iPhones, 343 example, 244 addActionListener() method, 202 APPLET tag (HTML), 238-239 WeightScale applet example, 243-245 addChangeListener() method, 223 ALIGN attribute, 239 real-word examples, Visible adding CODE attribute, 239 Human Project website, 27 emulators, 350 CODEBASE attribute, 239, 247 Revolve, 270 plug-ins, Eclipse, 392 HEIGHT attribute, 239 class declaration, 271 addItemListener() method, 204, 206 WIDTH attribute, 239 error handling, 272 addition operator (+), 56 applets, 25, 42, 235 event handling, 276 addKeyListener() method, 204 class files, 236 initializing, 272 addOneToField() method, 212 compared to applications, 236 screen updates, 273 addSlice() method, 335 definition of, 25, 39, 235 threads, 274-275 Aggregator application, 307, 309 displaying variables, 271 Agile Java Development with Spring, drawString() method, 240 RootApplet, 43-44 Hibernate and Eclipse, 381 paint() method, 236-237 ALIGN attribute (APPLET tag), 239 SalutonApplet repaint() method, 236 displaying, 240 Android event handling, 201 applications actionPerformed() method, 202 HTML markup, 241 ptg7068951 configuring AVDs, 350-351 check boxes, 204 source code listing, 240 creating, 345-349 combo boxes, 204 saving, 7 Debug Configurations, event listeners, 201-202 security, digital signatures, 30 351-352 starting, 238 keyboard events, 206 debugging, 366 stopping, 238 events, 236 design, 355-358 structure, 43 HTML markup, 238-239 interface design, 359-362 threaded, 270 initializing, 237-238 manifest files, 358-359 class declarations, 271 Java Boutique, 33-35 navigating, 346-348 error handling, 272 JTicker website, 32-33 running, 352-354 event handling, 276 LinkRotator, 273 writing Java code, 362-368 initializing, 272 methods, 235-236 Java on phones, running, 35 running, 274-275 destroy(), 238 overview, 343-345 screen updates, 273 init(), 237-238 phones, configuring, 394-395 starting, 274 paint(), 236-237 plug-ins, installing, 344, 391-393 stopping, 275 repaint(), 236 programming, 389-390 variables, 271 start(), 238 resources, 358 WeightScale source code, stop(), 238 243-245 SDKs, 390 object tags, applying, 245-246 windows, sizing, 239 Android Virtual Devices. See AVDs parameters appletviewers, 44 AndroidManifest.xml file, 347, 357-360 naming, 243 applications. See also applets passing, 243 Aggregator, 307, 309 Angry Birds application, 35

applications 399 Android ID3Reader, 286-288 compiling, 19 configuring AVDs, 350-351 Java Boutique, 33-35 greeting variable, 17-18 creating, 345-349 KeyViewer.java, 205-206 line-by-line breakdown, 18 Debug Configurations, LeaderActivity, 362-368 main() block, 16 351-352 LottoMadness, 192-193, 196-197 running, 20 debugging, 360 applet version, 216 saving, 18 design, 355-358 event listeners, 208 source code, 15 interface design, 359-362 LottoEvent.java class, 209-211 troubleshooting, 19-20 manifest files, 358-359 methods, 212-213 writing, 14-15 navigating, 346-348 source code listing, 213-215 SalutonApplet, 241-242 overview of, 343-345 multithreading, 31 saving, 7 running, 352-354 Name SpaceRemover, 110 writing code, 362-368 output, 113 SquareRootClient, 320-323 Angry Birds, 35 source code, 112 SquareRootServer, 315 applets, creating, 42-44 NetBeans SquareRootServerImpl, 316 arguments, 46 running, 378 SquareRootServerPublisher, 318 autodialers, 123 troubleshooting, 378-380 stock analysis, 32-33 Benchmark, 103-104 NewCalculator, 252 StringLister.java, 162-163 Calculator, 251 NewRoot, 41, 130 strings, viewing, 66-67 ptg7068951 Clock Nines, 97 Tool, 228, 230 output, 90 NumberDivider, 254-255 troubleshooting, 8 source code, 89-90 Organizing block statements, Variable ClockFrame, 183 81-83 char variables, 51 colors, 313, 327 PageCatalog, 258-261 code listing, 52 RGB values, 329 PieFrame, 338-339 floating-point variables, 51 setting, 329 PiePanel, 333 int statement, 50 compared to applets, 236 PiePanel.java source code, integer variables, 51 compiling, 19, 40 338 string variables, 51 Configurator.java, 294-295 PieSlice class, 335-336 Virus, 148 Console, 289 PlanetWeight, 60-61 class constructor, 143 creating, 39-42 PrimeFinder, 268-269 getSeconds() method, 142 Credits, code listing, 72 properties.xml, 301 setSeconds() method, 142 Crisis, 188-189 PropertyFileCreator.java, 300 showVirusCount(), 144 definition of, 39 ReadConsole, 289 tauntUser() method, 143 deploying, 394 Root VirusLab Fonts, 313, 327 compiling, 40 output, 150 formatting, 192, 196-197 source code, 39 source code, 149-150 Game running, 7 WeatherStation, 304-307 output, 82 Saluton Wheel of Fortune, 113 source code, 82 class declarations, 15 character arrays, 115 class statements, 16 integer arrays, 115

400 applications letterCount array, 115 Wheel of Fortune application, 113 block statements, 49, 81-83 nested loops, 115 output, 114 blocks, 16-17 output, 114 source code, 113 books, Java-related, 381 source code, 113 Arrays class, applying, 112-113 Boole, George, 53 writing, 13 /assets, 347 Boolean variables, 53 applying assigning variables BorderLayout manager, 190-191 annotations, 314-315 types, 50 borders, Insets class, 191-192 arrays, 109 values, 54-55 braces ({}), 16-17, 49, 82, 92 Arrays class, 112-113 asterisk (*), 56 brackets ({}), 82, 92 Color class, 328 attributes, 122, 137 break statement, 84, 92, 100 expressions, 59-60 ALIGN, 239 breaking loops, 100-101 Font class, 327-328 CODE, 239 Browser JAR/Folder dialog box, 303 NetBeans, 373 CODEBASE, 239, 247 browsers creating new projects, 374-375 HEIGHT, 239 Java Plug-in, 28 formatting classes, 376-377 HTML, 238 downloading, 242 installing, 373 inheritance, 125-126 buffered input streams, 288-290 running, 378 SRC, 238 Console application, 289 troubleshooting, 378, 380 WIDTH, 239 creating, 288 objects autoboxing, 131 ReadConsole application, 289 ptg7068951 existing, 159-160 autodialers, 123 reading, 288 tags, 245-246 AVDs (Android Virtual devices), bugs, 8. See also debugging Package Explorer, 348 350-351 Builder class, 304 threads, 270, 272 AWT (Abstract Windows Toolkit), 169 buttons, creating, 174-176 app_name string resource, 349 Insets class, 191-192 bytecode, 284 Arc2D class, 332-333 bytes, 284, 296 arcs, drawing, 332-333, 341 arguments, 46 B applications, 41 C methods, 142-143 backslash (/), escape code, 67 ArrayIndexOutOfBoundsException, 250 backspaces, escape code, 67 C++, 5, 10 arrayoutofbounds errors, 109 BASIC (Beginner’s All Symbolic CableModem class, 133 arrays, 109, 111 Instruction Code), 4, 10 Cadenhead, Rogers, 381 declaring, 108 behavior Cafe au Lait website, 383 definition of, 107 hierarchy, 125-126 Calculator application, 251 elements, 108 inheritance, 125 calling web services, 323 initial values, 108 Bell, Joshua, 6 cannot resolve symbol (error multidimensional, 111 Benchmark application, 103-104 message), 20 sample application, 110 benchmarks, 102 career opportunities, 385 sorting, 111-113 BlackBerrys, 343 carriage returns, escape code, 67 upper limits, checking, 109 blank spaces in source code, 22

classes 401 case choice lists, event handling, 204. See JButton, 174 changing strings, 71, 75 combo boxes JCheckBox, 177-178 sensitivity, variable names, 54 choosing programming languages, JComboBox, 178-179 interpreted languages, 7 statements, 84 JFrame, 171 Chrome browser, 44. See also Google; casting, 127 JLabel, 176-177 interfaces definition of, 127 JPanel, 180 circles, drawing, 332 destinations, 127 JScrollPane, 219 classes, 122 objects, 132 JSlider, 222 Activity, 346 sources, 127 JTextArea, 179 Applet methods, 156-157 variables, 127-128 JTextField, 176-177 Arc2D, 332-333 catch statement, 272, 280 Line2D, 330 ArrayIndexOutOfBounds catching Exception, 250 LottoEvent, 209, 211 Calculator application, 251-252 Arrays, applying, 112-113 methods, 144 DivideNumbers sample CableModem, 133 Modem, 124, 132 application, 254 Color, 329 ModemTester, 133-134 errors, 272 Console, 289-290 nesting, 146 exceptions, 249-255 declaring, 15-16 NetBeans, 376-377 NewCalculator application, 253 documentation, 382 objects NumberDivider sample applica- DslModem, 133 looping, 162-163 ptg7068951 tion, 254-255 Ellipse2D, 332 storing, 160-162 PageCatalog sample application, 258-261 encapsulation, 142 PieSlice, 335-336 SumNumbers sample application, Exception, 250 Point, 164 251, 261 file, 133, 284-285 Point3D, 164 try-catch blocks, 250-255, 261 FileInputStream, 290 code listing, 164 try-catch-finally blocks, 255 FileOutputStream, 290 creating, 164-165 cell phones, 343. See also Android Graphics, 237 testing, 165-166 CENTER tag (HTML), 238 Graphics2D, 330 private, 135 change listeners, 223 arcs, 332-333, 341 R, 363 ColorSlide sample application, 227 circles, 332 ReadConsole, 289 registering objects as, 223-224 ellipses, 332 Rectangle2D, 331 changing string case, 71, 75 lines, 330 Revolve, 271 char variables, declaring, 51, 65 rectangles, 331 statement, 15-16, 124 characters hierarchy, 155, 167 subclasses, 126, 133, 157-159, 164-165 definition of, 51, 65 inheritance, 125-126, 135, special, escape codes, 67-68 155-158 superclasses, 126 strings, counting, 113-115 inner classes, 146-147 testing, 165-166 charts, pie, 121 Insets, 191-192 Thread, 265 check boxes JApplet, 155-156, 235 variables creating, 177-178 inheritance, 156-157 creating, 139-140 event handling, 204 methods, 157 values, 140 checkAuthor() method, 148 subclasses, 157 Virus, 137

402 clearAllFields() method clearAllFields() method, 212 PageCatalog application, 260 combo boxes clients, 320-322 PiePanel.java source code, creating, 178-179 Clock application 336-338 event handling, 204 output, 90 PlanetWeight application, 60 commands, javac, 40. See also source code, 89-90 Point3D class, 164 methods ClockFrame application, 183 PointTester.java program, 165-166 comments, 17, 22, 304 clocks, 87. See also Clock application PrimeFinder application, 268-269 comparing strings, 70 close() method, 291 Root application, 40 equal/not equal comparisons, 81 closing streams, 291 RootApplet application, 43 less/greater than comparisons, 80-81 code Saluton application, 15, 18 annotations, formatting, 314-315 SalutonApplet compiled languages, performance, 10 compilers writing Android applications, HTML file, 241 362-368 source code, 240 definition of, 7 CODE attribute (APPLET tag), 239 ShowWeight applet, 244 javac, error messages, 20 code listings SpaceRemover.java compiling applications, 19, 40 Benchmark application, 103 application, 110 complex for loops, 102 CableMode class, 133 StringLister.java, 162-163 components, 170, 219 Calculator application, 251 Tool application, 229 arranging, 185 Clock application, 88-89 Variable application, 52 buttons, creating, 174-176 ptg7068951 ColorSliders application, 224 Virus application, 149 change listeners, 223 Commodity program, 85-86 VirusLab application, 149-151 ColorSliders sample Console application, 289 WeightScale applet application, 227 registering objects as, Credits application, 72-73 HTML file, 245 223-224 Crisis application, 188-189 Java source code, 244 check boxes DslModem class, 133 Wheel of Fortune application creating, 177-178 Game program, 82 output, 114 event handling, 204 HomePage.java, 259 source code, 114 ClockFrame application, 183 ID3Reader application, 286-288 WriteMail application, 221 combo boxes, 178-179, 204 KeyViewer.java, 205-206 CODEBASE attribute creating, 180-183 LinkRotator applet, 276-279 APPLET tag, 239, 247 disabling, 206-207 LottoEvent.java class, 209, 211 OBJECT tag, 247 enabling, 206-207 LottoMadness application, Color class, 328-329 frames, 170-171 193-195, 213-215 colors, 313, 327 adding components to, 174 MailWriter application, 220 Color class, 328 creating, 171, 174 Modem class, 132 displaying RGB values, 329 sizing, 172 ModemTester class, 133-134 Font class, 327-328 image icons, 227-228 Name application, 112 RGB values, 329 creating, 227 NewCalculator application, 252 setting, 329 Tool sample application, NewRoot application, 41, 130 ColorSliders application, 227 228-230 Nines application, 97-98 com object, creating, 124-125 labels, 176-177 NumberDivider application, 254

defining 403 panels, 180 Configurator.java application, long, 52 294-295 scroll panes, 219 short, 52 configuring adding components to, 220 String, 17 creating, 219-220 AVDs (Android Virtual Devices), date/time, displaying, 183 350-351 MailWriter sample Debug Configurations, creating, Debug Configurations, 351-352 application, 221 351-352 WriteMail sample phones, 394-395 debugging application, 222 ConfigWriter.java application, 291 Android applications, 357, 366 sliders Console application, 289 definition of, 8 creating, 222-223 constants, 55 OOP applications, 123 labels, 223 constructor methods, 143 phones, 395 text arguments, 144 declaring areas, 179 declaring, 143 arrays, 108, 111 fields, 176-177, 198 inheritance, 144 classes TextField, 176 containers, 170, 180 class statement, 15-16 toolbars, 227 continue statement, 100 subclasses, 157-159, 164-165 creating, 228 contracts, WSDL (Web Service methods, 141 dockable toolbars, 228 Description Language), 318 class methods, 144 controlling access. See access control Tool sample application, constructors, 143 228-230 converting ptg7068951 public methods, 142 windows, 170-172, 174 objects, 127 variables, 17, 50 computer speed, testing, 103-104 variables to objects, 129-131 Boolean, 53 concatenating strings, 68 counter variables, 96 char, 65 concatenation operator (+), 68-69 counting characters in strings, char variables, 51 Conder, Shane, 390 113-115 class variables, 139-140 conditionals, 79 Create Activity checkbox, 346 floating-point, 51 Clock application Create New Library dialog box, 303 integers, 50 output, 90 createNewFile() method, 285 long, 52 source code, 89-90 Credits application code listing, 72 object variables, 137-138 if, 79-81, 83, 92 Crisis application, 188-189 Revolve applet, 271 blocks, 81-83 currentThread() method, 275 Revolve program, 271 equal/not equal customizing properties, 361 comparisons, 81 short, 52 strings, 51, 66 less than/greater than comparisons, 81 decrement operator (––), 56 less/greater than D decrementing variables, 56-58 comparisons, 80 default statement, 84 if-else, 83 Darcey, Lauren, 390 default.properties file, 348 switch, 84, 86 data types. See also type values defining ternary operator (?), 86-87 Boolean, 53 classes, inner classes, 146-147 configuration properties, reading/ byte, 52 services, 313 writing, 292-295 char, 51

404 deleting files deleting files, 285 dollar sign ($), 54 else statements, 83 deploying double quotation mark (“), 51 employment opportunities, 385 Android applications, 354 double slashes (//), 17 emulators (Android), configuring, applications, 394 draw() method, 330 350-351 Deployment Target Selection drawing enabling components, 206-207 Mode, 352 encapsulation, 142 arcs, 332-333, 341 design endless loops, 105 circles, 332 Android, 355-358 ellipses, 332 Endpoint Interfaces, 317 interfaces, 359-362 annotations, 314-315 lines/shapes, 329-330 destinations (casting), 127 pie graphs, 333 creating, 313 destroy() method, 238 equal sign (=), 52, 54 PiePanel.java source code, detecting errors in Android 338 equality operator (==), 81 applications, 357 PieSlice class, 335-336 equals() method, 70, 156 determining string lengths, 70-71 rectangles, 331 error handling, 249 development history of Java, 27 drawRoundRect() method, 331-332 catching exceptions, 249-250 Development settings, 354 drawString() method, 141, 240 multiple exceptions, 253-255 dialects, 302 DslModem class, 133 PageCatalog sample applica- dialog boxes, Add Repository, 391 tion, 258-261 digital signatures, 30 try-catch blocks, 250-255, 261 ptg7068951 disabling components, 206-207 try-catch-finally blocks, 255 displaying E creating exceptions, 262 applets ignoring exceptions, 258 EarthWeb’s Java directory, 385 drawString() method, 240 memory errors, 262 Eclipse paint() method, 236-237 stack overflows, 262 Android plug-ins, 344. See also repaint() method, 236 throwing exceptions, 250, Android colors, 329 256-258 installing, 390 pie graphs, 339 PageCatalog sample applica- plug-ins, 392 tion, 258-261 revolving links, 279 projects, creating, 355 throw statements, 256 strings editing try-catch statements, 272 println() method, 66-67 NetBeans, 376-377 errors special characters, 67-68 string resources, 348 Android applications, 357 text areas, 179 XML, 349 arrayoutofbounds, 109 variable contents, 18 editors, text, 13 bugs, 8 web services, 323 educational applications, 27 cannot resolve symbol displaySpeed() method, 124-125 elements, 108 message, 20 division, 59 comment, 304 exceptions, 109, 117 division operator (/), 56 forecastday, 305 handling. See error handling do-while loops, 99-101 initial values, 108 javac error messages, 20 dockable toolbars, 228 Ellipse2D class, 332 logic errors, 8 docking toolbars, 230 ellipses, drawing, 332 NetBeans, 379 documentation, 9, 232, 382

frames 405 Saluton program, troubleshooting, executing. See starting Fisher, Timothy R., 381 19-20 existing objects, 159-160 float statement, 51 syntax errors, 8 exists() method, 284 floating-point variables, declaring, 51 escape codes, 67-68 exiting loops, 100-101 FlowLayout manager, 176, 187 evaluating expressions, operator expressions, 49-50, 55, 59-61. See folders, viewing, 356. See also files precedence, 59 also operators Font class, applying, 327-328 Evans, Ben, 383 advantages, 60 fonts, 327 event handling, 201 operator precedence, 58-59 for loops, 95-97 actionPerformed() method, extends statement, 132, 157 complex for loops, 102 202, 276 extensions (file), .class, 22 counter variables, 96 check boxes, 204 empty sections, 102 combo boxes, 204 exiting, 100-101 event listeners, 201-202 sample application, 97 ActionListener interface, 202 F LottoMadness application, syntax, 96-97 208-211 File class, 284-285 vectors, 162-163 keyboard events, 206 File.pathSeparator, 284 forecastday element, 305 event listeners, 201-202 FileInputStream class, 290-292 formatting. See also configuring; design ActionListener interface, 202 FileOutputStream class, 290 annotations, 314-315 actionPerformed() method, 202 files ptg7068951 applications, 39, 192, 196-197 adding, 201 checking existence of, 284 Android, 345-352 LottoMadness application, creating, 284 208-209, 211 creating applets, 42-44 deleting, 285 EventListener interfaces, 201-202 sending arguments to, 41-42 File class, 284-285 Everlong.mp3 file, 287-288 classes, NetBeans, 376-377 file extensions, .class, 22 Exception class, 250 Color class, 328 finding size of, 285 exceptions, 109, 117 components, 180-183 manifest, Android applications, ArrayIndexOutOfBounds 358-359 Font class, 327-328 Exception, 250 interfaces reading catching, 249-250 annotations, 314-315 ID3Reader application, multiple exceptions, 253-255 286-288 Endpoint Interfaces, 313 PageCatalog sample applica- streams, 285-286 threads, 266 tion, 258-261 renaming, 285 variables, 137-140 try-catch blocks, 250-255, 261 writing to, 290-291 web service clients, 320-322 try-catch-finally blocks, 255 XML XML files, 299-302 creating, 262 creating, 299-302 formfeeds, escape codes, 67 ignoring, 258 reading, 302-307 forward slash (/) character, 284 NumberFormatException, 253-254 RSS syndication feeds, 307-309 frames, 170 throwing, 250, 256-258 fill() method, 330 adding components to, 174 PageCatalog sample fillRect() method, 329-331 creating, 170-171 application, 258-261 fillRoundRect() method, 331 SalutonFrame.java example, 174 throw statements, 256 finding strings within strings, 71-72 sizing, 172

406 Game application G Gosling, James, 4, 26, 303, 344, 373 ColorSliders sample graphics, 330 application, 227 Game application arcs, 332-333, 341 registering objects as, 223-224 output, 82 circles, 332 check boxes source code, 82 color, 313, 327 creating, 177-178 Gamelan website, 385 RGB values, 329 event handling, 204 games setting, 329 ClockFrame application, 183 lotto. See LottoMadness ellipses, 332 application combo boxes fonts, 313, 327 running on phones, 35 Graphics class, 237 creating, 178-179 /gen folder, 347 event handling, 204 icons, 227-228 /gen/org.cadenhead.android/ creating, 227 enabling/disabling components, R.java, 347 206-207 Tool sample application, get(int) method, 304 event handling, 201 228-230 getActionCommand() method, lines, drawing, 330 event listeners, 201-202 203, 212 ActionListener interface, 202 PiePanel application, 333 getAttribute() method, 304-305 actionPerformed() method, 202 PiePanel.java source code, 338 getChildElements() method, 304 adding, 201 PieSlice class, 335-336 getFirstChildElement() method, 304 frames, 170 rectangles, drawing, 331 getId() method, 364 adding components to, 174 ptg7068951 Graphics class, 237 getInsets() method, 192 creating, 170-171 Graphics2D class, 330 getKeyChar() method, 205 SalutonFrame.java arcs, 332-333, 341 getKeyCode() method, 205 example, 174 circles, 332 getKeyText() method, 205 sizing, 172 ellipses, 332 getName() method, 284 image icons, 227-228 lines, 330 getParameter() method, 243 creating, 227 rectangles, 331 getPort() method, 322 Tool sample application, graphs, pie, 333, 339 228, 230 getProperty() method, 293 PiePanel.java source code, 338 Insets, 191-192 getSeconds() method, 142 PieSlice class, 335-336 labels, creating, 176-177 getSource() method, 203, 223 greater than operator, 81 layout managers, 187 getSquareRoot() method, 315, 320 greeting variables BorderLayout, 190-191 getStateChange() method, 204 declaring, 17 FlowLayout, 187 getTime() method, 315 displaying contents of, 18 GridLayout, 189-190 getURL() method, 272 GridLayout manager, 189-190 LottoMadness sample applica- getValue() method, 304-305 GridLayout() method, 197 tion, 192-197 getValueIsAdjusting() method, 224 GUIs (graphical user interfaces), panels, creating, 180 getVirusCount() method, 149 170, 219 scroll panes, 219 GNU Lesser General Public License AWT (Abstract Windowing adding components to, 220 (LGPL), 303 Toolkit), 169 Google buttons, creating, 174-176 creating, 219-220 Android. See Android change listeners, 223 MailWriter sample application, 221 Chrome browser, 44

interfaces 407 sliders, 222-223 I init() block statements, 43 WriteMail sample init() method, 237-238, 272 application, 222 I/O (input/output) initializing Swing, 169 streams, 283-284, 299 applets, 237-238, 272 text buffered input streams, definition of, 105 areas, 179 288-290 inner classes, 146-147 fields, 176-177 byte streams, 284 input/output. See I/O write-protecting, 198 closing, 291 Insets class, 191-192 toolbars, 227 defined, 283-284 installing creating, 228 reading data from, 285-288 Android dockable toolbars, 228 writing data to, 290-291 plug-ins, 391-393 Tool sample application, IceRocket, 383 SDKs, 390 228, 230 icons, 227-228 Eclipse, 390 windows, 170-172, 174 creating, 227 NetBeans, 373 Tool sample application, 228, 230 programming tools, 9 ID3Reader application, 286-288 int statement, 50 IDEs (integrated development environ- H ments), 344, 373 integers arrays, creating, 108 if statements, 79-81, 83, 92 handling errors. See error handling variable types, 50 ptg7068951 blocks, 81-83 Harold, Elliote, 303, 383 integrated development environ- equal/not equal comparisons, 81 HEIGHT attribute (APPLET tag), 239 ments. See IDEs less than/greater than compar- “Hello world!”, 20 Intel, 343 isons, 80-81 Hemrajani, Anil, 381 Intent() method, 365 if-else statements, 83 hierarchies, Java classes, 155 interfaces, 227. See also GUIs ignoring exceptions, 258 history of Java, 26-27 ActionListener, 202, 271 ImageIcon constructor, 227 HomePage.java listing, 259 AWT (Abstract Windowing ImageIcon() method, 227 Toolkit), 169 horizontal sliders implementing Service buttons, 174, 176 creating, 222 Implementation Beans, 316-317 ChangeListener, 223 labels, 223 import statement, 237 check boxes, 177-178 HTML (Hypertext Markup incrementing variables, 56-58 Language), 238 combo boxes, 178-179 indexOf() method, 71-72 angle brackets (< >), 238 components, 170, 180-183 inequality operator (!=), 81 APPLET, 238-239 defined, 201 infinite loops, 105 CENTER, 238 design, Android applications, InformIT, 384 359-362 P, 238 website, 382 Endpoint Interfaces hyphen (-), subtraction operator, 56 inheritance, 125, 135, 155-157 annotations, 314-315 classes, 155-158 creating, 313 constructors, 144 EventListener, 201-202 hierarchy, 125-126 frames, 170-173

408 interfaces GUIs (graphical user interfaces). overriding, 157 JLabel class, 176-177 See GUIs setBackground(), 157 JME (Java Mobile Edition), 373 ItemListener, 204 setLayout(), 157 job opportunities, 385 KeyListener, 204, 206 subclasses, 157 Joy, Bill, 26 labels, 176-177 JAR (Java Applet Ratings Service), 34 JPanel class, 180 layout managers, 187-189 JARS (Java Review Service), 384 JScrollPane class, 219 BorderLayout manager, Java 7 Developer Blog, 383 JScrollPane() method, 219 190-191 Java Applet Ratings Service. See JAR JSE (Java Standard Edition), 373 BoxLayout manager, 191 Java Boutique website, 33-35 JSlider class, 222 GridLayout manager, 189 Java Development Kits. See JDKs JSlider() method, 222 separating components, 191 Java Development Tools. See JDTs JTextArea class, 179 NetBeans, 374 Java EE 6 Tutorial, The Basic JTextField class, 176-177 panels, 180 Concepts, 381 JTicker website, 32-33 Runnable, 265 Java Enterprise Edition. See JEE JToolBar() method, 228 scroll panes, 219, 222 Java Mobile Edition. See JME JVMs (Java Virtual Machines), 20, 28 Service Implementation Bean, Java Phrasebook, 381 316-317 Java Plug-in, 28, 242 text areas, 179-180 Java Review Service, 384 text fields, 176-177 Java Standard Edition. See JSE K windows, 170-173 ptg7068951 Java Virtual Machines. See JVMs Internet Explorer, 242 keyboards, event handling, 206 Java website, 382 interpreted languages, 7, 10 KeyListener interface, 204-206 Javac interpreters, 28 KeyViewer.java application, 205-206 commands, 40 definition of, 7 keywords, this, 147-148 compilers, error messages, 20 Java Plug-in, 28 JavaWorld website, 29-30 ItemListener interface, 204 javax.xml.ws, 317 itemStateChanged() method, 204, 212 JAX-WS library packages, 322 L JButton objects, 174 iteration, 97. See also loops JCheckBox class, 177-178 iterators, 97 Label() method, 176 JComboBox class, 178-179 labels JDKs (Java Development Kits), 8, 320 creating, 176-177 applications sliders, 223 J Saluton program, 14-15 languages writing, 13 OOP. See OOP JApplet class, 155-156, 235 installing, 9 selecting, 4-5 inheritance, 156-157 JDTs (Java Development Tools), 390 layout managers, 187 methods JEE (Java Enterprise Edition), 373 FlowLayout, 187 add(), 157 Jendrock, Eric, 381 GridLayout, 189-190 equals(), 156 JFrame class, 171 LottoMadness sample application, 192-197

methods 409 LeaderActivity application, 362-368 PropertyFileCreator.java applica- LottoEvent.java class, 209, 211 length variable, 109, 117 tion, 300 methods length() method, 70, 285 SalutonFrame.java application, 173 actionPerformed(), 212 lengths of strings, determining, 70-71 SquareRootClient application, addOneToField(), 212 320-323 LGPL (GNU Lesser General Public clearAllFields(), 212 License), 303 SquareRootServer application, 315 getActionCommand(), 212 libraries, XOM, 303. See also XOM SquareRootServerImpl. itemStateChanged(), 212 application, 316 Line2D class, 330 matchedOne(), 212 SquareRootServerPublisher appli- lines, drawing, 329-330 cation, 318 numberGone(), 212 LinkRotator applet, 273 WeatherStation.java application, source code listing, 213, 215 links 305-307 LottoMadness() method, 197 revolving, displaying, 279 Web Service Description lowercase, changing strings to, 71 variables with strings, 68-69 Language Contract application, 319 listeners, 201-202 lists, choice lists, 204 ActionListener interface, 202 load() method, 292 actionPerformed() method, 202 M loading applets, 43 adding, 201 Log.i() methods, 364 magazines, JavaWorld, 29-30 change listeners, 223 logic errors, 8 MailWriter application, 221 ColorSliders sample applica- ptg7068951 tion, 227 long variable type, 52 main() blocks, Saluton program, 16 registering objects as, loops MalformedURLException errors, 223-224 Benchmark application, 103-104 258, 273 LottoMadness application, definition of, 95 managers. See layout managers 208-211 managing resources, 356-358 do-while, 99 listFiles() method, 285 manifest files, Android applications, exiting, 100-101 listings. See also code listings 358-359 for, 95-97 Aggregator application, 307-309 matchedOne() method, 212 complex for loops, 102 ClockFrame application, 183 memory errors, 262 counter variables, 96 ClockPanel application, 181 messages, SOAP, 322 empty sections, 102 Configurator.java application, methods, 137, 140, 236 sample application, 97 294-295 accessor, 142 syntax, 96-97 ConfigWriter.java application, 291 actionPerformed(), 202-203, vectors, 162-163 HomePage.java application, 259 212, 276 infinite loops, 105 LeaderActivity application, add(), 157 362-368 naming, 101 add(Component), 228 NumberDivider application, nesting, 101 addActionListener(), 202 254-255 while, 98-99 addChangeListener(), 223 PageCatalog application, 260 LottoEvent.java class, 209-211 addItemListener(), 204 PieFrame application, 338-339 LottoMadness application, 192-193, addKeyListener(), 204 Playback application, 175 196-197 addOneToField(), 212 properties.xml application, 301 applet versions, 216 addSlice(), 335 event listeners, 208

410 methods applets, 235 getStateChange(), 204 setEditable(), 179, 198 arguments, 142-143 getTime(), 315 setEnabled(), 206 checkAuthor(), 148 getURL(), 272 setLayout(), 157, 188 class methods, declaring, 144 getValue(), 304-305 setLayoutManager(), 175 clearAllFields(), 212 getValueIsAdjusting(), 224 setProperty(), 293 close(), 291 getVirusCount(), 149 setSeconds(), 142 constructors, 143 GridLayout(), 197 setSize(), 172 arguments, 144 ImageIcon(), 227 setText(), 217 declaring, 143 indexOf(), 71-72 setTitle(), 171 inheritance, 144 init(), 237-238, 272 showDocument(), 276 createNewFile(), 285 init() blocks, 43 showVirusCount(), 144 currentThread(), 275 Intent(), 365 skip(), 286 declaring, 141 itemStateChanged(), 204, 212 sleep(), 266 definition of, 70 JScrollPane(), 219 sort(), 112 destroy(), 238 JSlider(), 222 start(), 238, 274 displaySpeed(), 124-125 JToolBar(), 228 stateChanged(), 223 draw(), 330 Label(), 176 stop(), 238, 270, 275 drawRoundRect(), 332 length(), 70, 285 storeToXML(), 300 drawString(), 141, 240 listFiles(), 285 substring(), 287 ptg7068951 equals(), 70, 156 load(), 292 System.out.println(), 127, 376 exists(), 284 Log.i(), 364 tauntUser(), 143 fill(), 330 LottoMadness(), 197 TextArea(), 180 fillRect(), 329, 331 main() blocks, 16 toCharArray(), 110 fillRoundRect(), 331 matchedOne(), 212 toLowerCase(), 71 get(int), 304 numberGone(), 212 toUpperCase(), 71, 75 getActionCommand(), 203, 212 overriding, 157-158 variable scope, 145-146 getAttribute(), 304-305 pack(), 172 void keyPressed(), 204 getChildElements(), 304 paint(), 43, 157-158, 236-237 void keyReleased(), 204 getFirstChildElement(), 304 parseInt(), 130, 152 void keyTyped(), 205 getId(), 364 println(), 61, 66-67, 141 write(), 290 getInsets(), 192 public, 142 mfl arrays, 111 getKeyChar(), 205 read(), 285 minus sign (-) getKeyCode(), 205 readLine(), 290 decrement operator (––), 56 getKeyText(), 205 renameTo(), 285 subtraction operator, 56 getName(), 284 repaint(), 236, 273 Modem class, 124, 132 getParameter(), 243 return values, 75, 141 Modem objects, 123 getPort(), 322 run(), 267, 274-275 modems getProperty(), 293 setBackground(), 157 CableModem class, 133 getSeconds(), 142 setColor(), 273 DslModem class, 133 getSource(), 203, 223 setContentView(), 363 Modem class, 132 getSquareRoot(), 315, 320 setDefaultCloseOperation(), 172 ModemTester class, 133-134

operators 411 ModemTester class, 133-134 NetBeans Field Guide, 373 creating, 124-125, 132-134 modifying strings, case, 71 NetBeansProjects, 375 existing, 159-160 modulus operator (%), 56 Netscape Navigator, downloading Java inheritance, 125-126, 155-157 Plug-ins, 242 Monitor objects, 123 Modem, 123 New Android Project Wizard, 345, Motorola, 343 Monitor, 123 349, 355 mouse clicks, handling, 276 PieChart, 122 New File Wizard, 14 multidimensional arrays, 111 referencing, 147-148 New Project button, 375 multiplication, 56, 59 storing, 160-163 New Project Wizard, 375 multitasking, 265 tags, 245-246 new statements, 108, 143 multithreading, 31, 265 variables, 137-139 NewCalculator application, 252 My Documents, 375 private, 139 newline characters, 180 protected, 139 escape codes, 67 onCreate() method, 363 NewRoot application, 130 online communities, Stack N source code, 41 Overflow, 384 news aggregators, 307. See also RSS OOP (object-oriented programming), Name application syndication feeds 33, 121-122, 170. See also classes output, 113 newSuffix variable, 129 advantages of, 122-123 source code, 112 Nines application, 97 applications, debugging, 123 ptg7068951 names nu.xom package, 304 autoboxing/unboxing, 131 file extensions, .class, 22 NumberDivider application, 254-255 encapsulation, 142 naming conventions NumberFormatException, 253, 256 inheritance, 125-126, 135, loops, 101 numberGone() method, 212 155-157 parameters, 243 numbers, displaying sequence of objects prime numbers, 268-269 variables, 54, 62 casting, 132 numeric variable types, 52 resources, 349 creating, 124-125, 132, 134 Nvidia, 343 navigating Android applications, objects. See objects 346-348 overview, 33, 121 Navigator, downloading Java Open Handset Alliance, 343 Plug-ins, 242 operators O nesting addition (+), 56 classes, 146-147 Oak language, 27 concatenation (+), 68-69 loops, 101 OBJECT tag (HTML), CODEBASE attrib- decrement (– –), 56 NetBeans, 8. See also IDEs (integrat- ute, 247 division (/), 56 ed development environments) object-oriented programming, See equality (==), 81 applying, 373 OOP greater than (>), 81 classes, creating, 376-377 objects, 137. See also classes inequality (!=), 81 errors, Saluton program, 19-20 attributes, 122, 137 modulus (%), 56 installing, 9, 373 behavior, 122 multiplication (*), 56 projects, creating, 374-375 casting, 132 precedence, 58-59 running, 378 classes, 122 subtraction (-), 56 troubleshooting, 378, 380 converting, 127-131 ternary (?), 86-87

412 Oracle Oracle, 25 parseInt() method, 130, 152 Point3D class, 164 Oracle Technology Network for Java passing creating, 164-165 Developers, 382 arguments testing, 165-166 order of precedence, operators, 58-59 to applications, 41 postfixing, 57 organizing to methods, 142-143 precedence, operators, 58-59 applications, block statements, parameters to applets, 243 preferences, configuring Android, 393 81-83 pasting prefixing, 57 resources, 356-358 into strings, 69 prime numbers, displaying sequence output. See I/O (input/output) of, 268-269 strings together, 68 @Override annotation, 314 PrimeFinder application, 268-269 percent sign (%), modulus operator, 56 overriding methods, 157-158 printing strings performance, interpreted languages, 10 phones. See also Android println() method, 66-67 special character, 67-68 configuring, 394-395 P running Java on, 35 println() method, 61, 66-67, 141 pie charts, 121 private classes, 135 P tag (HTML), 238 pie graphs, creating, 333 private variables, 139 program listings. See code listings pack() method, 172 PiePanel.java source code, 338 Package Explorer, applying, 348 PieSlice class, 335-336 programming packages, 139 viewing, 339 Android, 389 ptg7068951 Android SDKs, installing, 394 PieChart object, 122 configuring phones, 394-395 javax.xml.ws package, 317 PieFrame application, 338-339 Eclipse, 390 JAX-WS library, 322 PiePanel application, 333 plug-ins, 391-393 PageCatalog application, 258-261 PiePanel.java source code, 338 SDKs, 390 pageTitle array, 271 PieSlice class, 335-336 languages, selecting, 4-5 paint() method, 236-237, 273 PieSlice class, 335-336 OOP (object-oriented program- ming). See also OOP block statements, 43 pipe (|) characters, 254 advantages of, 122-123 overriding, 157-158 PlanetWeight application code listing, panels, creating, 180 60-61 casting, 129 platform independence, 29 creating objects, 124, 132-134 PARAM tag (HTML), 242 NAME attribute, 243 Playback.java, 175 overview of, 121 plug-ins Saluton program VALUE attribute, 243 parameters Android, 344, 391-393 creating, 14-15 handling definition of, 242 running, 20 ShowWeight applet, 244 Java Plug-in, 242 tools WeightScale applet, 243-245 plus signs (+) installing, 9 naming, 243 addition operator, 56 selecting, 8-9 passing to applets, 243 concatenation operator, 68-69 programs. See applications; software receiving in applets, 243 increment operator (++), 56 proguard.cfg file, 348 values, assigning, 243 Point class, 164 Project Location text field, 375 Project Properties dialog box, 303 Project Selection dialog box, 352

rounded rectangles, drawing 413 projects Hour 17, 247 referencing objects, this statement, Android applications, navigating, Hour 18, 263 147-148 346-348 registering objects as change listen- Hour 19, 280 ers, 223-224 creating, 355 Hour 20, 296-297, 310-311 NetBeans, 374-375 renameTo() method, 285 Hour 21, 341-342 renaming files, 285 properties quotation marks configuration, reading/writing, repaint() method, 236, 273 double (“), 51 292-295 /res folder, 347, 357 escape codes, 67 customizing, 361 resources, 381. See also websites single (‘), 51 Properties object, 293, 299 Android, 358 properties.xml application, 301 folders, viewing, 356 PropertyFileCreator.java Java-related books, 381 application, 300 R job opportunities, 385 protected variables, 139 managing, 356-358 public methods, 142 R class, 363 naming, 349 public statements, 124 R.java file, 363 strings, editing, 348 publishing web services, 317-318 read() method, 285 restricting access, 138. See also ReadConsole application, 289 access control reading return values (methods), 75, 141 configuration properties, 292-295 Revolve applet, 270 ptg7068951 Q files, 285 class declaration, 271 ID3Reader application, error handling, 272 QName, 321 286-288 event handling, 276 question mark (?), 86-87 read() method, 285 methods quizzes skip() method, 286 actionPerformed(), 276 Hour 1, 11 RSS syndication feeds, 307, 309 init(), 272 Hour 2, 23 streams, buffered input run(), 274-275 Hour 3, 37 streams, 288 start(), 274 Hour 4, 47 XML files, 302-307 stop(), 275 Hour 5, 63 readLine() method, 290 screen updates, 273 Hour 6, 76 real-word Java projects Hour 7, 93-94 threads JavaWorld website, 29-30 running, 274-275 Hour 8, 105-106 Visible Human Project website, Hour 9, 118 27, 29 starting, 274 stopping, 275 Hour 10, 135-136 receiving parameters to applets, 243 variables, 271 Hour 11, 153 recommended reading, 381 Revolve class, creating, 271 Hour 12, 167-168 Rectangle2D class, 331 revolving links, displaying, 279 Hour 13, 185-186 rectangles, drawing, 331 RGB values (red, green, blue), 329 Hour 14, 199 Red, Green Blue (RGB) color Root application, 40 Hour 15, 217-218 system, 329 Hour 16, 232-233 Reference Chooser dialog box, 361 RootApplet applet, 43-44 rounded rectangles, drawing, 331

414 RSS syndication feeds, reading RSS syndication feeds, reading, Sams Teach Yourself Java 2 in 21 setLayout() method, 157, 188 307-309 Days, 381 setLayoutManager() method, 175 run() method, 267, 274-275 Sams Teach Yourself Java 2 in 24 setProperty() method, 293 RuneScape, 26 Hours website, 387-388 setSeconds() method, 142 Runnable interface, 265 Sams Teach Yourself Java in 24 Hours setSize() method, 172 website, 383 running setText() method, 217 Samsung, 343 Android, 352-354 setTitle() method, 171 saving applications, 7 shapes applications, 7 Java on phones, 35 arcs, 332-333, 341 Saluton programs, 18 NetBeans, 374-375, 378 circles, 332 scope (variables), 145-146 Saluton program, 20 drawing, 329-330 screens, updating, 273 threads, 274-275 ellipses, 332 scroll panes, 219 lines, 330 adding components to, 220 PiePanel application, 333 creating, 219-220 PiePanel.java source code, 338 S MailWriter sample application, 221 PieSlice class, 335-336 WriteMail sample application, 222 rectangles, 331 Saluton application SDKs (Software Development Kits), classes 343, 390 short variable type, 52 declarations, 15 searching strings, 71-72 showDocument() method, 276 ptg7068951 statements, 16 searchKeywords variable, 69 showVirusCount() method, 144 code listings, 18 security, 30 signatures (digital), 30 compiling, 19 digital signatures, 30 single quotation marks (‘), escape code, 67 creating, 14-15 trusted developers, 30 sizing applet windows, 239 main() block, 16 selecting skip() method, 286 running, 20 languages, 4-5 SkyWatch, 31-32 saving, 18 programming tools, 8-9 slashes (//), 17 troubleshooting, 19-20 semicolon (;), 17, 22, 102 sleep() method, 266 variables Service Implementation Bean, 316-317 sliders declaring, 17 services creating, 222-223 displaying, 18 clients, creating, 320-322 labels, 223 SalutonApplet applet defining, 313 slowing down threads, 266 displaying, 240 publishing, 317-318 SOAP messages, 322 HTML markup, APPLET tag, 241 SquareRootServer, 313 software source code listing, 240 setBackground() method, 157 Absolute program, 34 testing, 241-242 setColor() method, 273 overview, 5-6 SalutonFrame.java, 174 setContentView() method, 363 strings, viewing, 66-67 Sams Publishing website, 382 setDefaultCloseOperation() troubleshooting, 8 Sams Teach Yourself Android method, 172 Software Development Kits. See SDKs Application Development in 24 Hours, 390 setEditable() method, 179, 198 sort() method, 112 setEnabled() method, 206

strings 415 sorting arrays, 111-113 continue, 100 stop() method, 238, 270, 275 source code default, 84 stopping black spaces, 22 definition of, 5 applets, 238 code listings. See code listings example, 6 threads, 275 editors, 13 expressions, 50, 59-61 storeToXML() method, 300 sources (casting), 127 extends, 132, 157 storing SpaceRemover application, 110 float, 51 looping, 162-163 spacing in source code, 22 if, 79-80, 83, 92 objects, 160-162 Spartacus.java class, 377 blocks, 81-83 variables, 54-55 special characters, escape codes, equal/not equal comparisons, streams, 283-284, 299 67-68 81 buffered input streams, 288-290 speed, testing computer, 103-104 less/greater than compar- Console application, 289 square brackets ([]), 108 isons, 80-81 creating, 288 SquareRootClient application, if-else, 83 ReadConsole application, 289 320-323 import, 237 reading, 288 SquareRootServer application, init(), 43 313-315 byte streams, 284 int, 50 SquareRootServerImpl loops closing, 291 application, 316 defined, 283-284 definition of, 95 SquareRootServerPublisher reading data from, 285 ptg7068951 application, 317-318 do-while, 99 ID3Reader application, exiting, 100-101 /src folder, 347 286-288 for, 95-97, 102 /src/org.cadenhead.android/ read() method, 285 SalutonActivity.java, 347 infinite loops, 105 skip() method, 286 sRGB, 329 naming, 101 writing to, 290-291 stack overflows, 262, 384 nesting, 101 String data type, 17 standard applet methods, 235 while, 98-99 StringLister.java source code, Standard RGB, 329 new, 108, 143 162-163 start() method, 238, 274 paint(), 43 strings, 65-66 starting public, 124 adding to, 69 applets, 238 static, 140, 144 arrays, 108. See also arrays threads, 274 super, 158-159, 165 changing case of, 71, 75 variables, 55 switch, 84, 86 characters, counting, 113-115 stateChanged() method, 223 this, 158, 165 comparing, 70 statements, 49, 79. See also condi- throw, 256 equal/not equal tionals try-catch, 250-255, 261, 272 comparisons, 81 blocks, 16-17, 49, 81-83 try-catch-finally blocks, 255 less/greater than comparisons, 80-81 break, 84, 92, 100 void, 141 concatenating, 68 case, 84 static statement, 140, 144 definition of, 51, 66 catch, 280 stock analysis applications, 32-33 determining length of, 70-71 class, 15-16, 124

416 strings displaying LottoMadness application, T println() method, 66-67 208-211 image icons, 227-228 special characters, 67-68 T-Mobile G1s, 343 finding within other strings, 71-72 creating, 227 tabs, escape code, 67 Tool sample application, resources, editing, 348 tags 228, 230 variables, 51 angle brackets (< >), 238 JApplet class, 235 declaring, 66 APPLET, 238-239 labels, creating, 176-177 linking, 68-69 ALIGN attribute, 239 layout managers, 187 strings.xml file, 349 CODE attribute, 239 BorderLayout, 190-191 Stroustrop, Bjarne, 5 CODEBASE attribute, 239, 247 FlowLayout, 187 subclasses, 126 HEIGHT attribute, 239 GridLayout, 189-190 creating, 133, 157-159, 164-165 WIDTH attribute, 239 LottoMadness sample applica- substring() method, 287 CENTER, 238 tion, 192-197 subtraction operator (-), 56 HTML, 238, 242-243 panels, creating, 180 Sun website, 25-26, 382 objects scroll panes, 219 super statement, 165 applying, 245-246 adding components to, 220 class declarations, 158-159 CODEBASE attribute, 247 creating, 219-220 superclasses, 126 P, 238 MailWriter sample Swing, 169, 219 application, 221 PARAM, 242 ptg7068951 buttons, creating, 174-176 WriteMail sample NAME attribute, 243 change listeners, 223 application, 222 VALUE attribute, 243 ColorSliders sample applica- sliders tauntUser() method, 143 tion, 224-227 creating, 222-223 ternary operator (?), 86-87 registering objects as, labels, 223 testing 223-224 text computer speed, 103-104 check boxes areas, 179 Points3D class, 165-166 creating, 177-178 fields, 176-177 SalutonApplet program, 241-242 event handling, 204 write protecting, 198 SquareRootServerPublisher appli- combo boxes toolbars, 227 cation, 318 creating, 178-179 creating, 228 text event handling, 204 dockable toolbars, 228 areas, 179 documentation, 232 Tool sample application, Color class, 328 enabling/disabling components, 228-230 editors, 13 206-207 switch statements, 84-86 fields event listeners, 201-202 syndication feeds, reading RSS, creating, 176-177 ActionListener interface, 202 307-309 write-protecting, 198 actionPerformed() method, 202 syntax errors, 8 Font class, 327-328 adding, 201 System.out.println() method, 127, 376 pasting into strings, 69 TextArea() constructor method, 180 this keyword, 147-148

variables 417 this statements, 165 tools U class declarations, 158 appletviewer, 44 Thread class, 265 programming Udovydchenko, Aleksey, 34 threaded applets, 270 installing, 9 unboxing, 131 class declarations, 271 selecting, 8-9 underscore (_) characters, 53, 54 error handling, 272 toUpperCase() method, 71, 75 University of British Columbia, 28 event handling, 276 travel Java Boutique, 33-35 updating screens, 273 initializing, 272 troubleshooting upper limits of arrays, checking, 109 screen updates, 273 Android applications, 357 uppercase, changing strings to, threads exceptions, 249-253. See also 71, 75 running, 274-275 exceptions user events, 201 starting, 274 NetBeans, 378, 380 ActionListener interface, 202 stopping, 275 Saluton program, 19-20 combo boxes, 204 variables, 271 software, 8 components, 206 threaded classes, 266-270 trusted developers, 30 handling, 202-203 threads, 265. See also threaded try-catch blocks, 250-255, 261, 273 keyboard events, 204-206 applets Calculator application, 251-252 LottoMadness application, creating, 266-270 DivideNumbers sample applica- 207-208, 212-213 multithreading, 31 tion, 254 ptg7068951 Runnable interface, 265 NewCalculator application, 253 running, 274-275 NumberDivider sample applica- tion, 254-255 V slowing down, 266 SumNumbers sample application, starting, 274 251, 261 validity, 302 stopping, 275 try-catch statement, 272 van de Panne, Michiel, 28 Thread class, 265 try-catch-finally blocks, 255 Variable application throw statements, 256 TryPoints.java listing, 165 code listing, 52 throwing exceptions, 250, 256-258 Twitter, 385 int statement, 50 PageCatalog sample application, two slash characters (//), 258 variables 258-261 type values (variables), casting, 127 characters, 51 throw statements, 256 types floating-point, 51 time, displaying, 183 Boolean, 53 integers, 51 titles, frames, 171 byte, 52 strings, 51 toCharArray() method, 110 char, 51 variables toLowerCase() method, 71 long, 52 access control, 138 Tool application, 228-230 short, 52 arrays, 109, 111 toolbars, 227 variables, 50 declaring, 108 creating, 228 definition of, 107 dockable toolbars, 228 elements, 108 docking, 230 initial values, 108 Tool sample application, 228-230 multidimensional, 111

418 variables sample application, 110 integers, 50 W sorting, 111-113 long, 52 assigning values, 54-55 short, 52 WeatherStation.application, 304-307 casting, 127-128 strings, 51 Web Service Description Language, converting to objects, 129-131 values See WSDL counter variables assigning, 55 web services definition of, 96 decrementing, 56-58 clients, creating, 320-322 initializing, 96 incrementing, 56-58 publishing, 317-318 data types, String, 17 starting values, 55 SquareRootServer, 313 declaring, 17, 50 vectors, objects Web Tools Platform. See WTP class variables, 139-140 looping, 162-163 weblogs, 383 object variables, 137-138 storing, 160-162 @WebMethod annotation, 315 definition of, 49 Verburg, Martijn, 383 websites displaying contents of, 18 VeriSign website, 30 Cafe au Lait, 383 initializing, definition of, 105 vertical sliders, creating, 223 Gamelan, 385 length, 117 viewing InformIT, 382 naming conventions, 54, 62 Android projects, 347 JARS (Java Review Service), 384 newSuffix, 129 appletviewers, 44 Java Boutique, 33-35 private, 139 pie graphs, 339 JTicker, 32-33 ptg7068951 protected, 139 resources, 356 JavaWorld, 29-30 referencing, this statement, revolving links, 279 Liberty BASIC interpreter, 6 147-148 strings, 66-67 Sams Publishing, 382 Revolve applet, 271 text areas, 179 Sams Teach Yourself Java 2 in 24 Revolve program, 271 web services, 323 Hours, 387-388 scope, 145-146 Virus application, 148 Sams Teach Yourself Java in 24 searchKeywords, 69 class constructor, 143 Hours, 383 strings, 66 methods Sun, 25-26, 382 changing case, 71, 75 getSeconds(), 142 VeriSign, 30 comparing, 70 setSeconds(), 142 Workbench, 383 concatenating, 68 tauntUser(), 143 WeightScale applets, source code, 243-245 declaring, 66 showVirusCount(), 144 well-formed data (XML determining length, 70-71 Virus class, 137 formatting), 302 displaying, 66-67 VirusLab application Wheel of Fortune application, 113 escape codes, 67-68 output, 150 character arrays, 115 linking, 68-69 source code, 149-150 integer arrays, 115 types Visual Basic, 4 letterCount array, 115 assigning, 50 void keyPressed() method, 204 nested loops, 115 Boolean, 53 void keyReleased() method, 204 output, 114 char, 51, 65 void keyTyped() method, 205 source code, 113 floating-point, 51 void statement, 141 while loops, 98-101 widgets, customizing properties, 361

Zamenhof, Ludwig 419 WIDTH attribute (APPLET tag), 239 X-Y windows, 170-172, 174 Debug Configurations, 351 XML (Extensible Markup Language) wizards editing, 349 New Android Project Wizard, files 345, 349 creating, 299-302 New File Wizard, 14 reading, 302-307 New Project Wizard, 375 RSS syndication feeds, 307-309 Workbench website, 383 XOM (XML Object Model), 303 write protecting text fields, 198 write() method, 290 WriteMail application, 222 writing Z applications, 13, 39 Zamenhof, Ludwig, 20 creating applets, 42-44 Saluton programs, 14-15 sending arguments to, 41-42 code, Android applications, 362-368 ptg7068951 Color class, 328 configuration properties, 292-295 Font class, 327-328 streams, 290-291 WSDL (Web Service Description Language), 318-320 WTP (Web Tools Platform), 390


Like this book? You can publish your book online for free in a few minutes!
Create your own flipbook