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 Beginning Programming with Java For Dummies, 4th Edition

Beginning Programming with Java For Dummies, 4th Edition

Published by E-book Bang SAOTHONG Distric Public library, 2019-04-21 10:35:35

Description: Beginning Programming with Java For Dummies, 4th Edition

Search

Read the Text Version

combined, 194–197 Index 435 defined, 165 in for loops, 315–317 currency object, format method of, 363 mixing different logical operators, currency variable, in NumberFormat 203–205 class, 363 multipart, 201 parentheses, 205–206 •D• while loops and, 294–295 Console view, Eclipse IDE data types. See types disk files and, 256 days in a month, finding the number of, displaying text in, 254 NumberFormatException 227–231 Debug As menu item, 170 message in, 58 Debug perspective, Eclipse IDE, overview, 74 println method and, 84 170–172 const keyword, 82 debugging. See also troubleshooting constructor calls, 400–403 constructors, defined, 402 automated, 170–172 Content panel, Scene Builder code in Listing 18-3, 353–354 decimal numbers window, 413 comparing, 166–167 Controller Class field, 420 defined, 127–128 Controller section, Scene Builder, 414 division and, 139–140 controls, Scene Builder, 413 reading from the keyboard, 128–129 converting types of, 151–152 decisions. See also if statements Celsius to Fahrenheit temperature, overview, 179 166–169 declarations dragging outside of a method, 131–133 converting your code to use import, 106, 129, 192 classes, 336 method, 89–91, 131 variable numbers into strings, 361–363 statements into comments, 87 combining, 133–134 strings into numbers, 359–361 defined, 127 count variable, 287, 290, 291 decrement operators, 144, 148 counting loops, 286–288 default clauses, switch statements, curly braces ( ) acting like a box, 86 220–221, 224 in blocks, 191, 237 default package, Java’s, 61, 62 cascading if statements and, 212 deleteOnExit method, 428 in classes, 95 deleting files missing, in if statements, 182 statements in, 235, 237, 238 response from the user and, 295–298 too many, 114–115 several files, 329–331 use of, 85–86 .txt files, 329–331 currency, formatting numbers as, diagnosing a problem, 108–111 case sensitivity, 108–110 363–365 EchoLine program, 108 omitting punctuation, 110–111 too much punctuation, 111–112

436 Beginning Programming with Java For Dummies, 4th Edition dice game, 234–238 double arrow (chevron), Eclipse IDE, 73 directory, current working, 329 double backslash (\\\\), inside quotation disk access facilities, Java’s, 260–261 disk files marks, 270 double equal sign (==), comparisons reading from, 315 troubleshooting problems with, 264–266 with, 166, 188, 354, 355 disk-oriented programs double keyword, 127 creating an input file, 261–262 double quote marks, for literals, 351 Java’s disk access facilities, 260–261 double type keyboard-reading program and, 254 overview, 253 inaccuracies, 169 reading from disk files, 268 overview, 127, 143 reading input from a disk, 258–259 use in this book, 380 running, 254–256 double values sample program avoiding using for money, 143 casting, 144 listing, 256–257 dividing, 140 running, 261–263 double variables, combined declarations viewing the output file, 263 troubleshooting problems with disk and, 142 downloading files, 264–266 writing a disk-oriented program, API Documentation (API Specification), 21, 30 266–267 writing to a file, 268–271 Eclipse IDE for Java Developers, 24, diskScanner method, 258, 260, 277, 39–40 315–317 programming tools, 22, 26 diskWriter.print method, 259–261 sample programs, 24 display method, 372–377 DriverManager.println method, 93 DISPOSE_ON_CLOSE parameter, 399 division •E• of an int value by another int value, 139 eachKidGetsTen variable, 164, 165, 195 finding remainders, 140–144 EchoLine program forward slash for, 139 do loops (do . . . while loops) code listing, 98–100 execution of statements inside the extra lines of code, 105–107 loop, 300 errors format of, 299 case sensitivity, 108–110 do statements, 294, 299 diagnosing a problem, 108–111 “do this, then do that” approach, 406 expecting, 107–108 Document panel, Scene Builder, 414 omitting punctuation, 110 DO_NOTHING_ON_CLOSE runtime error messages, 117–118 spelling errors, 115–117 parameter, 399 why computers can’t fix, 111–115 dots (.) getting keyboard input, 104–105 calling an object’s methods, 357 how the program works, 103–104 to refer to an object’s parts, 341 Scanner class, 106 in regular expressions, 161 testing the code, 100–103 typing and running, 100–103

Index 437 EchoLine.java editor, 101 Package Explorer. See Package Eclipse editor Explorer, Eclipse logic errors, 67–68 perspective, 74, 170 unchecked runtime exceptions, 67 plug-ins, 409 Eclipse folder, extracting the contents of Preferences dialog, 44, 46, 47, 259 running for the first time, 42–43 downloaded, 24 separate installations of, 409 Eclipse IDE setting up, 38–39 Show View dialog box, 54, 74 32-bit and 64-bit versions of, 32, 40 Standard Widget Toolkit (SWT), 395 advantages of, 22 switching to a different workspace, 59 author’s disclaimers, 69 toolbar, 72 Breakpoints view, 170 Variables view, 170 chevron (double arrow), 73 Welcome screen, 25, 42, 43, 56 close button, 73 workbench compile-time errors in, 65 configuring Java in, 44–50 Console view. See Console view Console view display of this book’s Java projects, 50 launching Eclipse and, 25 disk files and, 256 overview, 69 displaying text in, 254 running Eclipse for the first time, NumberFormatException 42, 43 message in, 58 Workspace Launcher dialog, 42, 54, overview, 74 println method and, 84 55, 59 creating a new Java project, 60–62 yellow markers in, 68, 403 Debug perspective, 170–172 Eclipse IDE for Java Developers, definitions of terms, 69 deleting files, 297–298 downloading, 24 displaying text in Console view, 254 e(fx)clipse plug-in, 409–412, 420 downloading, 39–40 Eclipse website e(fx)clipse plug-in, 409–412, 420 editor. See editor, Eclipse advice to check often, 23 editor area, 74 downloading Eclipse from, 39–40 error markers in, 65 editor, Eclipse importing BeginProgJavaDummies4.zip active, 72 changes or corrections to the file into, 26 importing this book’s sample programs, code in, 66 compile-time warnings, 68 48–50 errors and, 107–108 indenting automatically, 86 formatting in, 64 installing, 40–41 need for, 21 JRE vs. JDK choice and, 29 overview, 71–72 launching, 25 quick fixes, 65, 109, 112, 113, 403 layout, 74, 170 red marks in, 65 marker bar, 73 syntax highlighting in, 21, 64 menu button, 72–73 editor area, Eclipse IDE, 74 New File dialog box, 262, 297 electronic version of this book, 102

438 Beginning Programming with Java For Dummies, 4th Edition else clause, 181, 182, 187, 188, 190, 191, unchecked runtime exceptions, 67 209 why computers can’t fix, 111–115 escape sequence, 245 e-mail address examples, program, downloading, 24 author’s, 7 exclamation point (not, negative, or no trying to get a username from an, 246–250, 274–275 way), 194, 195 exists method, 428 empty string, 378 EXIT_ON_CLOSE parameter, 398, 399 end of a file expressions checking for, 277–279 conditions and, 165 why the computer accidentally defined, 148 regular, 161 pushes past, 280–281 statements and, 148 ending statements, with a extends JFrame, 403–405 extensions, filename, 25 semicolon, 93 end-of-line comments, 87 •F• enhanced for loops Facebook, author’s page on, 7, 23 with arrays, 327–329 Fahrenheit temperature, program for creating, 301–303 defined, 301 converting from Celsius to, 166–169 format of, 301–302 fall-through, default clauses and, 224–229 nesting, 303–307 false (reserved word), 82 Enterprise Edition, Java, 29 February, 228–230 enum type File(“rawData.txt”), 260, 264 creating, 212–213 File class, uses of, 428 listing using, 213–215 filename extensions, 25, 268 for slot machine symbols, 301–303 filenames, troubleshooting problems equal signs accidentally typing two, 116 with disk files, 264 double (==), comparisons with, 166, FileNotFoundException, 260 files 188, 354, 355 equal to (==) operator, 166 deleting equals method, 355–357, 359, 371, response from the user and, 295–298 several files, 329–331 429 .txt files, 329–331 error markers, 65 errors (error messages) end of checking for, 277–279 compiler (compile-time), 65 why the computer accidentally EchoLine program pushes past, 280–281 case sensitivity, 108–110 input and output, 257 diagnosing a problem, 108–111 listFiles method, 330 expecting, 107–108 troubleshooting problems with, omitting punctuation, 110 runtime error messages, 117–118 264–266 spelling errors, 115–117 findWithinHorizon method, 105, 161–163, why computers can’t fix, 111–115 expecting, 107–108 222, 250, 268, 277, 281 if statement, 181–182 Firefox, 32 logic, 67–68

Index 439 FixTheAlternator method, 88–92 GetUserName program, 251 float type, 152, 169 Giroux, Phillip, 370 flow of control, 376–377 glyphs, 173 folder names, 270 goto keyword, 82 fonts used in this book, 2 goToTheSupermarketAndBuySome for loops method, 382, 383, 387 arrays and, 322–324 greater than (>) operator, 166 conditions in, 315–317 greater than or equal to (>=) deciding on limits at runtime, 313–315 enhanced operator, 166 GridLayout, 406 with arrays, 327–329 grouping separators, 129, 168, 257 creating, 301–303 guestsIn array, 321, 322, defined, 301 format of, 301–302 325, 328 nesting, 303–307 GUI (Graphical User Interface) initializing, 289–291 while loops and, 291 button control and other widgets, for statements, 286–288. See also for 412–422 loops displaying a window on the screen, example listing, 286–288 395–398 structure of, 288–289 formatting, in Java programs, 64 frames forward slash, for division, 139 creating an instance of, four-letter word, reversing the letters 400–401 described, 398–400 in a, 159–160, 233 extending, 403–405 frame.pack method, 398, 399 frames in general, 78 Java classes for creating GUI JFrame class creating an instance of, 400–401 applications, 394–395 described, 398–400 JavaFX classes, 395, 407–413, 418 extending, 403–405 JFrame class JLabel class, 397 creating an instance of, 400–401 shrink-wrapping, 399, 405 described, 398–400 frame.setDefaultCloseOperation method, extending, 403–405 mixing XML with Java, 406–408 398, 399 overview, 393 fullName variable, 350 Scene Builder, 408–409, 413, 414, fully qualified name, Scanner class, 106, 416–420 365 Swing classes, 394–395, 403 FXML File, 412–413, 416, 420 windows •G• declaratively describing, 406–407 getCurrencyInstance method, 362, 429 getInterest method, 386, 390, 391 displaying on computer screen, getProperty method, 430 395–398 Eclipse, 69 jumping among, 21 gumball problem, 135–138

440 Beginning Programming with Java For Dummies, 4th Edition •H• import declarations, 106, 129, 192, 365–366 hard drive, data in. See disk-oriented programs Import Projects dialog, Eclipse, 48–50 indented code, 86 Haskell language, 13 indenting hasNext method, 277, 316, 317 headers, method alternative to, 119 automatically, 86 definition of, 90 if statements, 185–187 examining, 373 punctuation and, 86 example of, 93 indexes, of arrays, 320 working with, 385–386, 391 infinite loops, 243 Herst, David, 120 InfoQ, 426 hexadecimal, 428 inheritance, 405 HIDE_ON_CLOSE parameter, 399 initializing Hierarchy section, Scene Builder, 414, for loops, 289–291 variable values, 131–132, 134, 198 418, 420 inner loops, 277, 279–281 highlighting, syntax, 21, 64 input file, creating an, 261–262 host name, 274, 280 Inspector panel, Scene Builder, 414 hovering the mouse, over error installation, online versus offline, 29 Installed JREs, in Eclipse’s Preferences markers, 110 HTML, 408 dialog, 44–45 installer, Java, 24 •I• instances icons used in this book, 5–6 objects and, 340, 341 IDE (integrated development questions and answers about, 345 int type environment). See also Eclipse IDE for money values, 380 basic description of, 22 for whole numbers, 151, 152 identifiers int values with agreed-upon meanings, 83–84 assigning to a double variable, 380 defined, 83 dividing by another int value, 139 user defined, 83, 119 reading, 138 if clauses, 181, 182, 190, 191 as whole numbers, 136 if statements int variable, 142, 144, 302, 343, 380, 384, 428 blocks, 191 Integer class, 359, 361, 428 cascading, 209–212 Integer.MAX_VALUE, 428 complete program with, 183–185 Integer.MIN_VALUE, 428 error messages, 181–182 Integer.toBinaryString, 428 format of, 179–180 Integer.toHexString, 428 indenting, 185–187 integrated development environment introduction to, 179 nesting, 206–209 (IDE). See also Eclipse IDE sample programs, 179, 183, 188–190 basic description of, 22 without an else clause, 187 interest IHateTxtFiles.java file, 329, 330 adding, 382–383 ImageIcon class, 397 addinterest method, 383–386

Index 441 interestRate parameter, 383–386 Java Motel, occupancy report for, isHidden method, 428 311–317 iterations of loops, 235, 236 Java perspective, Eclipse’s, 74, 170 •J• Java Preferences on Macs, 35, 36 Java programs. See also code; listings Java (Java ecosystem). See also Java language checking out Java code for the first time, 77–79 classes for creating GUI applications, 394–395 different approaches to, 119–120 elements of complexity of, 366 finding on your computer, 32–38 comments, 87–88 numbering of versions of, 28 identifiers, 83–84 version numbers, 28, 32, 34 keywords, 81–82 versions of, 34–38 literals, 84–85 Java API overview, 80 documentation (API Documentation) punctuation, 85–87 getting this book’s sample programs, 26 Applet page, 427 out variable, 366, 367 basic description of, 20–21 sample program, 78–79 downloading, 21, 30 syntax highlighting in, 64 in Java programming toolset, 21 tools for writing, 21–22 introduction to, 20–21 tools needed for writing, 23–25 Java class understanding a simple program, 88–91 New Java Class dialog box, 61, 62, 101 writing a disk-oriented program, overview, 95 Java compiler 266–267 defined, 14, 15 Java Runtime Environment (JRE) Eclipse IDE, 29 Java Control Panel, on Macs, 35–37 choice between JDK and, 28–29 Java Development Kit (JDK) configuring Java in Eclipse and, 44–45 choice between JRE and, 28–29 in general, 27 downloading latest version of, 24 Settings, 37–38 online versus offline installation, 29 Java SDK. See JDK (Java Java EE (Java Enterprise Edition), 29 Java installer, 24 Development Kit) Java JDK, getting newest version of, 27 Java SE (Java Standard Edition) Java language. See also Java as case-sensitive, 81 download page, 28 comparisons between English and, 80 versus Java EE versus Java ME, 29 keywords in, 80–83, 118 versions of, 28 origin of, 20 Java Software Development Kit (Java Java library, 20. See also Application SDK). See JDK (Java Programming Interface (API) Development Kit) Java ME (Java Micro Edition), 29 Java Virtual Machine (JVM) Java methods. See also methods boolean values in, 165 described, 18–19 overview, 89 “Main method not found” message, 118 portability of, 19 random numbers, 186 Write Once, Run Anywhere model of, 19

442 Beginning Programming with Java For Dummies, 4th Edition javac file, 15 reading decimal numbers from, 128–129 javac.exe, 15, 19 reading whole numbers from, 137 java.com, downloading tools from, 22 as reserved word, 106 javadoc comments, 88 System.in, 106 javadoc program, 88 keyboard.nextLine method, 103, 104, Javadocs, 20 java.exe, 19 184, 401 JavaFX classes, 395, 407–413, 418 keyboard-reading program, disk-reading JavaFX projects program and, 254 adding stuff to, 412–418 keyboard/screen program, 256–257 bare-bones keywords. See also specific keywords creating, 410–411 as case-sensitive, 81, 118 running, 411 Java-English comparisons, 80–81 java.lang package, 366 list of, 82 java.lang.System class, 366 with no meaning in a Java program, 82 java.util package, 365 official, predetermined meaning of, 82 java.util.Scanner class, 365 reserved words, 82 JButton, 394, 395 JDK (Java Development Kit) •L• choice between JRE and, 28–29 downloading latest version of, 24 Landscaping For Dummies, 370 online versus offline installation, 29 lastModified method, 428 JFrame class layout creating an instance of, 400–401 described, 398–400 Eclipse IDE, 74 extending, 403–405 GridLayout, 406 JLabel class, 397 leap year, isLeapYear variable, 227, JLabel object, 397, 405–407 job listings, 426 228, 230 JRE (Java Runtime Environment) leftReel variable, 302 choice between JDK and, 28–29 length method, 428, 429 configuring Java in Eclipse and, less than operator (<), 166 less than or equal to operator (<=), 166 44–45 letters. See also case sensitivity; in general, 27 Settings, 37–38 characters JRE Definition dialog, 45, 46 alphabetical ordering of, 172 JRE System Library, 331 Levine, John R., 370 Library panel, Scene Builder window, 413 •K• lineIn variable, 348 Linux keyboard checking Java installation on, 38 getting input from, 104–105, 254, 315 filename extensions in, 25 program to echo keyboard input, installing Eclipse, 41 98–100 JRE Definition dialog, 46–47 reading a String value from, 350 tar.gz file, 30 troubleshooting problems with disk files, 264 listFiles method, 330, 331

Index 443 listings A Java Program, 99 Adding Interest, 382–383 Listing the Combinations, 303–304 An Account Class, 373 A Little Experiment, 271–272 An Answer for Every Occasion, 218–219 Making a Word Go Backward, 159 An Attempt to Debug the Code in Making Change, 141 Listing 18-3, 353 Making Use of the Code in Listing 19-1, An if Statement, 179 Are You Paying Too Much?, 196 375–376 Aren’t You Lucky?, 188 May the Best Team Be Displayed First, Calling an Object’s Method, 354–355 Calling the addInterest Method, 383 189–190 Calling the Method in Listing 19-8, The Mechanical Combining of Two 388–389 In Case of a Tie . . ., 210–211 Loops, 278 A Class with Two Methods, 380–381 A Method Declaration, 89 Creating a Window with an Image in It, A Method That Returns a Value, 388 396–398 More Chips, Please, 359–361 Declaring a Variable Inside the main A More Refined Version of the One- Method, 368 Deleting All .txt Files, 329 Room Code, 316 Do You Have a Coupon?, 200–201 A More Versatile Program for Kids and Doing It the Old Fashioned Way, 334–335 Gumballs, 137 Extending Java’s JFrame Class, 403 Nested if Statements, 207 A Faulty Password Checker, 351–352 Nice Try, But . . ., 249–250 A Few Lines in a Computer Program, 12 No Extra Break for Kids or Seniors, 204 Finding the Number of Days in a Month, From One File to Another, 275 227–228 Please, Gimme a Break!, 225–226 George Boole Would Be Proud, 202–203 Processing Purchases, 341–343 Getting a Double Value from the A Program to Display the Things Keyboard, 128 Hey! Is This a For-by-For?, 294–295 I Like, 63 Horace Fletcher’s Revenge, 286 A Program to Generate an Occupancy How the Display Method Behaves When No One’s Looking, 374 Report, 312 How to Edit the Main.java File, 417, Proud Winners and Sore Losers, 421–422 How to Keep Four Kids from Throwing 213–214 Tantrums, 136 Pulling a Variable Outside of the main How to Prime a Loop, 251–252 I Know Everything, 183 Method, 368 I’m Repeating Myself Again (Again), 348 Putting a Name in a String Variable, Investigating the Behavior of toUpperCase, 156 349–350 It’s Warm and Cozy in Here, 167 Repeat Before You Delete, 296 Report on One Room Only, Please, 313–314 The Right Way to Display a Dollar Amount, 362 Roll 7 or 11, 234–235 A Simple Java Program, 78 A Simplified Version of the Game Twenty-One, 244 Slot Machine Symbols, 301–303 SnitSoft’s Grand Scam, 122

444 Beginning Programming with Java For Dummies, 4th Edition listings (continued) getting a username from an e-mail Storing Occupancy Data in an Array, address, 274–276 326–327 A Template to Read Data from a Disk overview, 273 File, 258–259 reworking existing code, 275 A Template to Write Data to a Disk running your code, 276 File, 259 solving the problem, 282–283 That’s Much Better!, 282–283 using, 292–293 This Is So Cool!, 381 no early bailout from, 238 Traveling Through Data Both Forward priming, 245, 250–252 and Backward, 322–324 trying to get a username from an e-mail Trying to Get a Username from an E-Mail Address, 246–247 address, 246–250, 274–275 Using a Java Locale, 364–365 while (while statements) Using a Variable Declared Outside of a for Loop, 290 blocks and, 237 Using Input and Output Files, 257 condition written first, 294–295 Using the boolean Type, 164 do statements and, 294–295 Using the Keyboard and the overview, 234–237 Screen, 256 priming, 245, 249–252 Using Your Purchase Class, 336 What It Means to Be a Chair, 338 •M• What It Means to Be a Purchase, 335 Mac OS X literals, 84–85, 351 filename extensions in, 25 Lively Browser, 39 installing Eclipse, 41 Locale class, 85, 364, 429 versions, 34 logic errors, 67–68 logical operators Macs (Macintosh computers). See also Mac OS X example of, 195–196 mixing different, 203–205 checking Java installation on, 33–38 overview, 194 JRE Home field, 45–46 long type, 151, 152 main identifier, 84 loops, 233–252 main method(s) assembling the pieces of, 241 declaring a variable inside, 368 counting, 286–288 inside a class, 95 escape sequence and, 245 “Main method not found” message, 118 following the action in, 235–237 overview, 91–92 getting values for variables, 242–243 pulling a variable outside of, 368 infinite, 243 sample code, 94 inner, 277, 279–281 two, in one project, 337 iterations of, 235, 236 variable declaration and, 127, 133 jumping into, 249–250 Main.java file, 416, 417, 420, 421 within loops (nested loops), 273–283 MakeChange class, 142 Managing Your Money Online For checking for the end of a file, 277–279 creating useful code, 276–277 Dummies, 370 defined, 277 marker bar, Eclipse IDE, 73 markers, 73 Math class, 429 menu button, Eclipse IDE, 72–73

messages, error Index 445 compiler (compile-time), 65 EchoLine program header, 90 case sensitivity, 108–110 inheriting, 405 diagnosing a problem, 108–111 main expecting, 107–108 omitting punctuation, 110 declaring a variable inside, 368 runtime error messages, 117–118 inside a class, 95 spelling errors, 115–117 “Main method not found” why computers can’t fix, 111–115 expecting, 107–108 message, 118 if statement, 181–182 overview, 91–92 logic, 67–68 pulling a variable outside of, 368 unchecked runtime exceptions, 67 sample code, 94 why computers can’t fix, 111–115 two, in one project, 337 variable declaration and, 127, 133 method body object’s values as used by, 386–387 definition of, 90 parameters and, 382 examining, 374–375 passing values to, 382–384 punctuation, 377 method calls Scanner, 105, 138, 258, 268 definition of, 90, 91 static, 133, 357–359, 365, 366, 368 described, 91 terminology describing, 90 example of, 92 using an object’s, 351–355 getting values from, 387 Microsoft Windows, 19 semicolon at the end of, 93 Microsoft Word, formatting in, 64 minus sign (-), 139 method declaration mkdir method, 428 definition of, 90 modifier, abstract, 113 described, 89–91 modulus operator, 140 month, finding the number of days in a, method headers definition of, 90 227–231 examining, 373 mortgage program, sample, 53–58 example of, 93 multiplication, asterisk for, 139 working with, 385–386, 391 MyFirstJavaClass.java file, 62, 63, 71, 72 MyFirstProject, 60–62, 66 method name, described, 89 MyFrame class, 403–405 methods. See also specific methods myRandom.nextInt method, 185–188, abstract, 113 217, 220, 379, 383, 384 assignments and, 129–130 mySalary, 85 body, 90 myScanner.useLocale method, 429 calling, 89–92, 375–377 classes with two, 380–381 •N• constructors and, 402 creating, 372–373 names defined, 88, 89 defined, 83 defining within classes, 371 filenames, troubleshooting problems dragging declarations outside of, with disk files, 264 folder, 270 131–133 of variables, 124 flow of control, 376–377

446 Beginning Programming with Java For Dummies, 4th Edition nested loops (loops within loops) comparison operators, 166 checking for the end of a file, 277–279 decimal creating useful code, 276–277 defined, 277 comparing, 166–167 getting a username from an e-mail defined, 127–128 address, 274–276 division and, 139–140 overview, 273 reading from the keyboard, reworking existing code, 275 running your code, 276 128–129 solving the problem, 282–283 types of, 151–152 using, 292–293 double type, 127 formatting as currency, 363–365 nesting grouping separators, 129, 168, 257 enhanced for loops, 303–307 pseudorandom, 186 if statements, 206–209 random, 184–186, 189, 235 turning into strings, 361–363 NetBeans IDE, downloading, 29 turning strings into, 359–361 new File expression, 260, 270, 298, 329 with underscores, as literals, 85 New File statement, in Eclipse, 262, whole reading from the keyboard, 137–138 297, 298 remainder and, 143 New Java Class dialog, Eclipse, types of, 151 using, 135–136 61–62, 101 New Java Project dialog, 60, 101, 410–411 •O• next method, 105, 350 nextBoolean method, 228 object code. See also code nextDouble method, 105, 129, 130, 162, defined, 14 in general, 17 168, 257 relationship between source code nextInt method, 104, 105, 138, 162, 163, and, 15 185–190, 193 object-oriented programming (OOP) nextLine method, 103–105, 184, 350, 401 defined, 95, 333 nextWord method, 104 equals method and, 356 niceTotal variable, in NumberFormat FAQ, 345 class, 363 objects non-static methods, 358–359 from classes, 338–340 not (!), 194, 195 creating several, 341–343 null (reserved word), 82 differences between classes and, 338 NullPointerException error message, 281 explained, 344 number formats, 85 instances and, 340, 341 number separators, 85 questions and answers about, 345 NumberFormat class, 85, 362–364 references to parts of, 341 NumberFormatException message, 57, 58 self-filling, 380 NumberFormat.getCurrencyInstance terminology, 340–341 using methods of, 351–355 method, 429 numberOfDays, 228, 231 numbers adding, 378 comparing, 165–172

Index 447 occupancy report for Java Motel, parameters (parameter lists), 382 311–317 parentheses onePurchase variable, 339, 341, 357, calling a method, 377 397, 400 calling an object’s method, 377 casting, 144 online articles, 6 conditions using, 205–206 operating systems, 64-bit vs 32-bit in if statements, 181 methods and, 161 versions, 31 parseInt method, 359, 361, 367, 428 operators. See also specific operators password-checking program, 351–357 peers, in AWT components, 394 assignment, 147–150 percent sign (%), as remainder comparison, 166 conditional, 229–231 operator, 140 creating new values by applying, plus sign (+) 139–140 for addition, 139, 378 decrement, 144, 148 concatenating strings with, 378 logical, 194 portability, 19 modulus, 140 postdecrement operator, 147 postincrement, 145–148 postincrement operator, 145–148 predecrement and postdecrement, 147 predecrement operator, 147 preincrement, 144–145 predetermined values, repeating with, or operator (||), 194, 195 Oracle, Java’s keywords created by, 82 300–301 Oracle website Preferences dialog, Eclipse IDE, 44, 46, advice to check often, 23 downloading tools from, 22 47, 259 overview, 425 preincrement operator (++), 144–145 OS X, Java installation, 33–34 price variable, 198 OtherStatements place-holder, 180 priming loops, 245, 250–252 out identifier, 84 primitive non-numeric types, 173 out variable, 366, 367 primitive numeric types (simple types), •P• 151, 152 primitive types, reference types and, 335 pack method, 399 println identifier, 84 Package Explorer, Eclipse println method, 102, 109, 269, 272. displaying all files and folders, 331 See also System.out.println method menu button, 73 in Console view, 84 running a Canned Java Program, 55–56 PrintStream type, 109, 116, 260, 261, 267, toolbar, 72 writing and running your program, 268, 272 procedural programming, 406–407 60–61 ProcessNiceAccounts class, 385, 386 packages programmer, defined, 12, 130 programming, object-oriented default, 61 Eclipse IDE, 61 defined, 95, 333 import declarations and, 365–366 equals method and, 356 FAQ, 345

448 Beginning Programming with Java For Dummies, 4th Edition programming languages, 13. See also examples, 85–86 Java language exclamation point (not, negative, or no programs (code). See also Java way), 194, 195 programs; listings if statements, 181 omitting, 110 to Display the Things I Like, 63 parentheses importing this book’s, 48–50 object calling a method, 377 calling an object’s method, 377 defined, 14 casting, 144 in general, 17 conditions using, 205–206 relationship between source code in if statements, 181 methods and, 161 and, 15 quotation marks overview, 11–13 curly (“”), 64 process of creating, 14 double, for literals, 351 reusing, 20 reading characters and, 162–163 running straight (“”), 64 semicolons on almost any computer, 3 after for statements, 289 Canned Java Program, 53–58 ending statements with, 93 overview, 15–19 errors, 112 projects that contain two Java source in if statements, 181, 182 too much, 111 files, 337 Purchase class, 336, 340, 341, 343–345, separating your programs from this 397, 400, 401 book’s, 59 purchase program, 333–345 sample, 11–12 translating into zeros and ones, •Q• 14–17, 126 question mark colon (? :) conditional prompt operator, 230 defined, 130 quick fixes, Eclipse editor, 65, 109, 110, user’s input and, 130–131 112, 113, 403 pseudorandom numbers, 186 public, as keyword, 81 quotation marks punctuation curly (“”), 64 commas, adding extra, 111 double, for literals, 351 for comments, 87 reading characters and, 162–163 curly braces straight (“”), 64 acting like a box, 86 •R• in blocks, 191, 237 cascading if statements and, 212 Random class, 185–187, 193 in classes, 95 random generation missing, in if statements, 182 statements in, 235, 237, 238 myRandom.nextInt method, 185–188, too many, 114–115 217, 220, 379, 383, 384 use of, 85–86 equal signs accidentally typing two, 116 double (==), comparisons with, 166, 188, 354, 355

Index 449 of numbers, 184–186, 189, 235 typing and running your own code, 58 of three-letter names, 372, 378–379 writing and running your program, RangeOfValues array, 302, 303, 328, 330 .rar files, 26 60–66 rawData.txt file, 257, 260–262, 264–266 runtime error messages, 117–118 reading assignment statements from right to •S• left, 124 sample programs, this book’s. See also characters, 162–163 listings decimal numbers from the keyboard, importing, 26, 48–50 128–129 Scanner class from disk files, 254–256, 268 input from a disk, 258–259 described, 106 int values, 138 findWithinHorizon method, 105 whole numbers from the keyboard, 137 fully qualified name of, 106, 365 reference types, 335, 354 java.util package, 365 regular expressions, 161 nextDouble method, 105, 130 remainder operator, 140, 143, 169 nextLine method, 105 remainders, finding, 140–144 NumberFormat class and, 429 RepeatAfterMe class, 119 Scanner methods, 105, 138, 258, 268 repeating instructions, 234–235 Scene Builder, 408–409, 413, 414, 416–420 repeating statements (for statements), SDK, Java (Java Software Development 286–288 Kit). See JDK (Java Development Kit) replaceAll string, 430 semicolon (;) reserved words, 82 response from the user, deleting files after for statements, 289 ending statements with, 93 and, 295–299 errors, 112 return types, 387–390 in if statements, 181, 182 return values, 387–390 serialVersionUID, 403 reversing, four-letter words, 159–160, 233 setDefaultCloseOperation method, 398, RoboJeeves, 97–98 role-playing the computer, 279, 283 399, 404 root directory, 329, 330 setReadOnly method, 428 Root.fxml file, 412, 413, 416, 420 setVisible method, 398, 400, 404 Ross, Rick, 187 short type, 152 .rtf extension, 262, 313 Show Line Numbers check box, 65 running code (programs). See also Java Show View dialog box, Eclipse IDE, 54, 74 simple types (primitive numeric types), programs; listings on almost any computer, 3 151, 152 Canned Java Program, 53–58 Simula language, 95 overview, 15–19 Sindell, Kathleen, 370 projects that contain two Java source Slashdot, websites, websites, 426 slot machines, Java program about, files, 337 separating your programs from this 301–303 smallLetter variable, 154, 156–158 book’s, 59 Smalltalk language, 95 SnitSoft, 121–123

450 Beginning Programming with Java For Dummies, 4th Edition SnitSoft CD-ROM, 128 switch SnitSoft class, 122 case clauses, 219–220, 223 SomeStatements place-holder, 180, 208 default clause, 220–221 sort method, 358, 368, 369 defined, 218 sorting, checks, 368–370 fall-through and, 224–229 source code form of, 221 Java versions, 222 defined, 14 listing, 218–219 relationship between object code overview, 217–218 and, 15 turning into comments, 87 SourceForge, 426 while spaces, alternative to, 119 spelling errors, 109, 115–117 blocks and, 237 Spotlight, Java Preferences, 35 condition written first, 294–295 square brackets, in arrays, 320 do statements and, 294–295 square brackets ([]), 85, 320 overview, 234–237 stacked views, 71 priming, 245, 249–252 Standard Widget Toolkit (SWT), 395 static import declarations, 192, 212, 367 statements static methods, 133, 357–359, 365, for. See also for loops 366, 368 example listing, 286–288 static out variable, 367 structure of, 288–289 String class assignment example, 348 initialization and, 198 overview, 347–348, 429 order of, 138 String type overview, 129–130 comparisons with, 172 reading from right to left, 124 use of, 157 understanding, 124 String values, displaying, 351 string variables, 349–350 blocks of, 237 String.java file, 347–348, 371 compound, 181, 182, 237 strings for counting loops, 286–288 comparing, 354–355 definition of, 92 concatenating, 378 do, 294, 299 empty, 378 ending with a semicolon, 93 in general, 177 expressions and, 148 reading and writing, 350–351 if turning into numbers, 359–361 turning numbers into, 361–363 blocks, 191 strings identifier, 84 cascading, 209–212 Swing classes, 394, 395, 403 complete program with, 183–185 switch statements error messages, 181–182 case clauses, 219–220, 223 format of, 179–180 default clause, 220–221 indenting, 185–187 defined, 218 introduction to, 179 fall-through and, 224–229 nesting, 206–209 form of, 221 sample programs, 179, 183, 188–190 without an else clause, 187

Java versions, 222 Index 451 listing, 218–219 overview, 217–218 three-letter names, random generation Symbol type, 302 of, 372, 378, 379 Symbol.values() expression, 302 syntax coloring (syntax highlighting), throws clause, 260, 266 TicketPrice program, 196, 198–200 21, 64 toolbar, 72 System class total variable, in NumberFormat java.lang package, 366 class, 363 static out variable, 367 toUpperCase method, 154–156 system identifier, 84 traditional comments, 87 System Preferences, on Macs, 35–36 tree variable, 369 System.in, defined, 106 troubleshooting problems with disk files, System.out method, 367 System.out variable, static, 366 264–266 System.out.println method true (reserved word), 82 calling an object’s methods, 357 Twenty-One card game, 238, 239, in complete program, 183–184 described, 92, 93 243, 244 EchoLine program, 103 Twitter error messages, 115 in general, 63, 92 author’s handle, 7 with nothing in the parentheses, 142 author’s page, 23 postincrement operator and, 146 .txt extension, 313 sample code, 94 .txt files, deleting all, 329–331 wrapping displayed lines, 125 typefaces used in this book, 2 types (of variables) •T• defined, 123, 127 difference between a type’s name and tab groups, 71–72 tab stops, inserting, 245 the type’s values, 303 tabs, Eclipse IDE, 71 primitive, 151 tar.gz files, 26 return, 387–390 template, code, 258, 259 Terminal window, on Linux •U• computers, 38 unchecked runtime exceptions, 67 “test, then print, then input” strategy, uncompressing a file, 26 underscore character (_) 249–250 text-based programs, 53, 393 in class names, 62 TextEdit program, 262, 313 numbers with, as literals, 85 TextField control, 412, 413, 417–421 Unicode, 173 ThingsILike class, 78, 79, 83, 85, 87, 95 UNIX For Dummies: Quick ThingsILike program, 77, 78, 85, 86, Reference, 370 92, 94 unzipping a file, 26 upper case. See capitalization uppercase letters, 154–156, 158 userInput variable (string), 352, 353, 356, 359

452 Beginning Programming with Java For Dummies, 4th Edition username views, Eclipse reaching the end of the, 279–280 active, 72 trying to get a, from an e-mail address, bar of buttons at the top of, 72 246–250, 274–275 close button, 73 Console view users, defined, 130 disk files and, 256 user’s input, prompt and, 130–131 displaying text in, 254 NumberFormatException message •V• in, 58 overview, 74 values println method and, 84 in arrays, 320–321 described, 70–71 assigning to variables, 131, 165, 380 Package Explorer, 73 defined, 123 stacked, 71 double avoiding using for money, 143 virtual machine, defined, 14 casting, 144 Visual Basic, sample code, 13 dividing, 140 void keyword, 81 handing off, 384–385 method calls for, 387 •W• passing to methods, 382 return, 387–390 Walheim, Lance, 370 warnings variable declarations combining, 133–134 compile-time, 68 defined, 127 paying attention to, 68 websites variable names, defined, 124 InfoQ, 426 variables. See also specific variables job listings, 426 Oracle assigning values to, 131, 165, 380 boolean advice to check often, 23 downloading tools from, 22 defined, 164 overview, 425 examples of using, 164–165, 201–203 SourceForge, 426 overview, 164–165 support, 7 with combined declarations, 142 this book’s, 23, 102, 425 defined, 122 unintuitive names for software initialization of, 131–132, 198 moving from place to place, 131–133 downloads, 32 reusing, 157–160 Welcome screen, Eclipse IDE, 25, 42, types of defined, 123, 127 43, 56 difference between a type’s name and whatsLeft variable, 141, 142 while statements (while loops) the type’s values, 303 primitive, 151 blocks and, 237 return, 387–390 condition written first, 294–295 using, 121–124 do statements and, 294–295 Variables view, Eclipse IDE, 170 overview, 234–237 versions, Java, 28, 34 priming, 245, 249–252

whole numbers Index 453 reading from the keyboard, 137–138 remainder and, 143 NumberFormatException types of, 151 message in, 58 using, 135–136 overview, 74 WhoWins type, 212–216 println method and, 84 Wilson, Alan, 187 display of this book’s Java projects, 50 windows launching Eclipse and, 25 overview, 69 declaratively describing, 406–407 running Eclipse for the first time, 42, 43 displaying on computer screen, working directory, 329 workspace, Eclipse 395–398 place on your hard drive for, 410 Eclipse, 69 switching to a different, 59 jumping among, 21 Workspace Launcher dialog, Eclipse, 42, Windows, Microsoft installing Eclipse, 40–41 54, 55, 59 JRE Home field, 45 wrapping displayed lines, 125 Windows (XP and newer) Write Once, Run Anywhere model of checking Java installation on, 33 filename extensions in, 25 computing, 19 Windows 7 writing, to disk files, 268–271 checking Java installation on, 33 filename extensions in, 25 •X• Windows 8 checking Java installation on, 32 XML (eXtensible Markup Language), filename extensions in, 25 406–408 Windows environment, 19 Windows Notepad, 262, 313 •Y• Windows XP checking Java installation on, 33 yearlyInterest variable, 390 filename extensions in, 25 yellow marker, Eclipse editor, 68, 403 word lengths, 30–32 Young, Margaret Levine, 370 word processing, 20 word-processing programs, editor •Z• and, 21 zeros and ones, 14–17, 126 workbench, Eclipse. See also specific .zip files, 26, 41, 49 parts Console view disk files and, 256 displaying text in, 254

Notes ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________

Notes ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________

Notes ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________ ______________________________________

About the Author Dr. Barry Burd has an M.S. in Computer Science from Rutgers University and a Ph.D. in Mathematics from the University of Illinois. As a teaching assistant in Champaign-Urbana, Illinois, he was elected five times to the university-wide List of Teachers Ranked as Excellent by their Students. Since 1980, Dr. Burd has been a professor in the Department of Mathematics and Computer Science at Drew University in Madison, New Jersey. When he’s not lecturing at Drew University, Dr. Burd leads training courses for professional programmers in business and industry. He has lectured at conferences in the United States, Europe, Australia, and Asia. He is the author of several articles and books, including Java For Dummies and Android Application Development All-in-One For Dummies, both published by John Wiley & Sons, Inc. Dr. Burd lives in Madison, New Jersey with his wife and two kids (both in their twenties, and mostly on their own). As an avid indoor enthusiast, Dr. Burd enjoys sleeping, talking, and eating.

Dedication For Harriet, Sam and Jennie, Sam and Ruth, Abram and Katie, Benjamin and Jennie.

Author’s Acknowledgments Author’s To-Do List, February 13, 2014: Item: Send chocolates to Paul Levesque (the book’s project editor) and Melba Hopper (the book’s copy editor). As anyone who reads Chapter 4 learns, chocolate is one of the most precious commodities on earth. So when I give chocolate, I give it thoughtfully and intentionally. Item: Have a plaque erected in honor of Connie Santisteban, your acquisi- tions editor at Wiley. While you worked on other projects, Connie kept on insisting that you write this book’s fourth edition. (Sure, you wanted a long vacation instead of another book project, but who cares? She was right; you were wrong.) Item: Send a thank-you note to tech editor Russ Mullen who helped polish your original work and, miraculously, didn’t make a lot of extra work for you. Item: Visit Frank Thornton, Bonnie Averbach, and Herbert Putz at Temple University. Thank them for steering you to a career as a professor. In any other career, you’d have no time left to write. (And by the way, while you’re in Philly, don’t forget to stop for a cheesesteak.) Item: Send e-mail to Gaisi Takeuti at the University of Illinois, and to William Wisdom and Hughes LeBlanc at Temple University. Thank them for teaching you about Symbolic Logic. It’s made your life as a computer scientist and mathematician much richer. Item: Spend more time with your family. (Remind them that you’re the guy who wandered around the house before you started writing books.) Renew your pledge to clean up after yourself. Don’t be so high-strung and finish each sentence that you start. Remember that you can never fully return the love they’ve given you, but you should always keep trying.

Publisher’s Acknowledgments Project Coordinator: Erin Zeltner Cover Image: Spanic/iStockphoto Senior Acquisitions Editor: Constance Santisteban Senior Project Editor: Paul Levesque Copy Editor: Melba Hopper Technical Editor: Russ Mullen Editorial Assistant: Annie Sullivan Sr. Editorial Assistant: Cherie Case













At home, at work, or on the go, Dummies is here to help you go digital! From eLearning to e-books, test prep to test banks, language learning to video training, mobile apps, and more, Dummies makes learning easier. www.facebook.com/fordummies www.twitter.com/fordummies


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