Download Beginning Programming with Java for Dummies (ISBN - 0764588745) PDF

TitleBeginning Programming with Java for Dummies (ISBN - 0764588745)
TagsFor Dummies
LanguageEnglish
File Size5.0 MB
Total Pages410
Table of Contents
                            Beginning Programming with Java For Dummies, 2nd Edition
	About the Author
	Dedication
	Author’s Acknowledgments
	Contents at a Glance
	Table of Contents
	Introduction
		How to Use This Book
		Conventions Used in This Book
		What You Don’t Have to Read
		Foolish Assumptions
		How This Book Is Organized
		Icons Used in This Book
		Where to Go from Here
	Part I: Revving Up
		Chapter 1: Getting Started
			What’s It All About?
			From Your Mind to the Computer’s Processor
			Your Java Programming Toolset
		Chapter 2: Setting Up Your Computer
			Downloading and Installing the Software You Need
			Running JCreator for the First Time
			Is That All There Is to It?
		Chapter 3: Running Programs
			Running a Canned Java Program
			Typing and Running Your Own Code
	Part II: Writing Your Own Java Programs
		Chapter 4: Exploring the Parts of a Program
			Checking Out Java Code for the First Time
			The Elements in a Java Program
			Understanding a Simple Java Program
		Chapter 5: Composing a Program
			A Program to Echo Keyboard Input
			Expecting the Unexpected
		Chapter 6: Using the Building Blocks: Variables, Values, and Types
			Using Variables
			What Do All Those Zeros and Ones Mean?
			Reading Decimal Numbers from the Keyboard
			Variations on a Theme
		Chapter 7: Numbers and Types
			Using Whole Numbers
			Creating New Values by Applying Operators
			Size Matters
		Chapter 8: Numbers? Who Needs Numbers?
			Characters
			The boolean Type
			The Remaining Primitive Types
	Part III: Controlling the Flow
		Chapter 9: Forks in the Road
			Making Decisions (Java if Statements)
			Variations on the Theme
		Chapter 10: Which Way Did He Go?
			Forming Bigger and Better Conditions
			Building a Nest
			Enumerating the Possibilities
		Chapter 11: How to Flick a Virtual Switch
			Meet the switch Statement
			Using Fall-through to Your Advantage
			Using a Conditional Operator
		Chapter 12: Around and Around It Goes
			Repeating Instructions Over and Over Again (Java while Statements)
			Thinking about Loops (What Statements Go Where)
			Thinking About Loops (Priming)
		Chapter 13: Piles of Files: Dealing with Information Overload
			Running a Disk-Oriented Program
			Writing a Disk-Oriented Program
			Writing, Rewriting, and Re-rewriting
		Chapter 14: Creating Loops within Loops
			Paying Your Old Code a Little Visit
			Creating Useful Code
		Chapter 15: The Old Runaround
			Repeating Statements a Certain Number Times (Java for Statements)
			Using Nested for Loops
			Repeating Until You Get What You Need (Java do Statements)
			Repeating with Predetermined Values (Java’s Enhanced for Statement)
	Part IV: Using Program Units
		Chapter 16: Using Loops and Arrays
			Some for Loops in Action
			Reader, Meet Arrays; Arrays, Meet the Reader
			Working with Arrays
		Chapter 17: Programming with Objects and Classes
			Creating a Class
			From Classes Come Objects
			Another Way to Think About Classes
		Chapter 18: Using Methods and Variables from a Java Class
			The String Class
			Using an Object’s Methods
			Static Methods
			Understanding the Big Picture
		Chapter 19: Creating New Java Methods
			Defining a Method within a Class
			Let the Objects Do the Work
			Passing Values to Methods
			Getting a Value from a Method
		Chapter 20: Oooey GUI Was a Worm
			The Java Swing Classes
			Keeping the User Busy (Working with Buttons and Text Fields)
			Taking Action
	Part V: The Part of Tens
		Chapter 21: Ten Sets of Web Links
			The Horse’s Mouth
			Finding News, Reviews, and Sample Code
			Improving Your Code with Tutorials
			Finding Help on Newsgroups
			Reading Documentation with Additional Commentary
			Checking the FAQs for Useful Info
			Opinions and Advocacy
			Looking for Java Jobs
			Finding Out More about Other Programming Languages
			Everyone’s Favorite Sites
		Chapter 22: Ten Useful Classes in the Java API
			Applet
			ArrayList
			File
			Integer
			Math
			NumberFormat
			Scanner
			String
			StringTokenizer
			System
	Index
                        
Document Text Contents
Page 1

by Barry Burd

Beginning
Programming

with Java™
FOR

DUMmIES


2ND EDITION

01_588745 ffirs.qxd 3/16/05 9:31 PM Page i


C1.jpg

Page 2

01_588745 ffirs.qxd 3/16/05 9:31 PM Page iv

Page 205

switch (randomNumber) {
case 1:

System.out.println(“Yes. Isn’t it obvious?”);
break;

case 5:
System.out.println(“No chance, Lance.”);
break;

case 7:
System.out.print(“Yes, but only if”);
System.out.println(“ you’re nice to me.”);
break;

case 10:
System.out.print(“No, not until”);
System.out.println(“ Nell squeezes Rover.”);
break;

default:
System.out.print(“Sorry,”);
System.out.println(“ I just can’t decide.”);
break;

}

� The default clause is optional.

switch (randomNumber) {
case 1:

System.out.println(“Yes. Isn’t it obvious?”);
break;

case 2:
System.out.println(“No, and don’t ask again.”);
break;

case 3:
System.out.print(“I’m too tired.”);
System.out.println(“ Go ask somebody else.”);

}
System.out.println(“Goodbye”);

If you have no default clause, and a value that’s not covered by any of
the cases comes up, then the switch statement does nothing. For exam-
ple, if randomNumber is 4, then the code shown above displays Goodbye,
and nothing else.

� In some ways, if statements are more versatile than switch statements.
For example, you can’t use a condition in a switch statement’s
Expression:

//You can’t do this:
switch (age >= 12 && age < 65)

187Chapter 11: How to Flick a Virtual Switch

17_588745 ch11.qxd 3/16/05 9:29 PM Page 187

Page 206

You can’t use a condition as a case value either:

//You can’t do this:
switch (age) {
case age <= 12: //...etc.

To break or not to break
In every Java programmer’s life, a time comes when he or she forgets to use
break statements. At first, the resulting output is confusing, but then the pro-
grammer remembers fall-through. The term fall-through describes what hap-
pens when you end a case without a break statement. What happens is that
execution of the code falls right through to the next case in line. Execution
keeps falling through until you eventually reach a break statement or the end
of the entire switch statement.

If you don’t believe me, just look at Listing 11-2. This listing’s code has a
switch statement gone bad:

Listing 11-2: Please, Gimme a Break!

/*
* This isn’t good code. The programmer forgot some
* of the break statements.
*/
import java.util.Scanner;
import java.util.Random;
import static java.lang.System.out;

class BadBreaks {

public static void main(String args[]) {
Scanner myScanner = new Scanner(System.in);
Random myRandom = new Random();
int randomNumber;

out.print(“Type your question, my child: “);
myScanner.nextLine();

randomNumber = myRandom.nextInt(10) + 1;

switch (randomNumber) {
case 1:

out.println(“Yes. Isn’t it obvious?”);

case 2:
out.println(“No, and don’t ask again.”);

188 Part III: Controlling the Flow

17_588745 ch11.qxd 3/16/05 9:29 PM Page 188

Page 409

BUSINESS, CAREERS & PERSONAL FINANCE

Also available:
�Accounting For Dummies †

0-7645-5314-3
�Business Plans Kit For Dummies †

0-7645-5365-8
�Cover Letters For Dummies

0-7645-5224-4
�Frugal Living For Dummies

0-7645-5403-4
�Leadership For Dummies

0-7645-5176-0
�Managing For Dummies

0-7645-1771-6

�Marketing For Dummies
0-7645-5600-2

�Personal Finance For Dummies *
0-7645-2590-5

�Project Management For Dummies
0-7645-5283-X

�Resumes For Dummies †
0-7645-5471-9

�Selling For Dummies
0-7645-5363-1

�Small Business Kit For Dummies *†
0-7645-5093-4

Also available:
�Bass Guitar For Dummies

0-7645-2487-9
�Diabetes Cookbook For Dummies

0-7645-5230-9
�Gardening For Dummies *

0-7645-5130-2
�Guitar For Dummies

0-7645-5106-X
�Holiday Decorating For Dummies

0-7645-2570-0
�Home Improvement All-in-One

For Dummies
0-7645-5680-0

�Knitting For Dummies
0-7645-5395-X

�Piano For Dummies
0-7645-5105-1

�Puppies For Dummies
0-7645-5255-4

�Scrapbooking For Dummies
0-7645-7208-3

�Senior Dogs For Dummies
0-7645-5818-8

�Singing For Dummies
0-7645-2475-5

�30-Minute Meals For Dummies
0-7645-2589-1

FOOD, HOME, GARDEN, HOBBIES, MUSIC & PETS

0-7645-5307-0 0-7645-5331-3 *†

0-7645-5295-3 0-7645-5232-5

Available wherever books are sold. For more information or to order direct: U.S. customers visit www.dummies.com or call 1-877-762-2974.
U.K. customers visit www.wileyeurope.com or call 0800 243407. Canadian customers visit www.wiley.ca or call 1-800-567-4797.

HOME & BUSINESS COMPUTER BASICS

Also available:
�ACT! 6 For Dummies

0-7645-2645-6
�iLife ‘04 All-in-One Desk Reference

For Dummies
0-7645-7347-0

�iPAQ For Dummies
0-7645-6769-1

�Mac OS X Panther Timesaving
Techniques For Dummies
0-7645-5812-9

�Macs For Dummies
0-7645-5656-8

�Microsoft Money 2004 For Dummies
0-7645-4195-1

�Office 2003 All-in-One Desk Reference
For Dummies
0-7645-3883-7

�Outlook 2003 For Dummies
0-7645-3759-8

�PCs For Dummies
0-7645-4074-2

�TiVo For Dummies
0-7645-6923-6

�Upgrading and Fixing PCs For Dummies
0-7645-1665-5

�Windows XP Timesaving Techniques
For Dummies
0-7645-3748-2

0-7645-4074-2 0-7645-3758-X

Also available:
�2005 Online Shopping Directory

For Dummies
0-7645-7495-7

�CD & DVD Recording For Dummies
0-7645-5956-7

�eBay For Dummies
0-7645-5654-1

�Fighting Spam For Dummies
0-7645-5965-6

�Genealogy Online For Dummies
0-7645-5964-8

�Google For Dummies
0-7645-4420-9

�Home Recording For Musicians
For Dummies
0-7645-1634-5

�The Internet For Dummies
0-7645-4173-0

�iPod & iTunes For Dummies
0-7645-7772-7

�Preventing Identity Theft For Dummies
0-7645-7336-5

�Pro Tools All-in-One Desk Reference
For Dummies
0-7645-5714-9

�Roxio Easy Media Creator For Dummies
0-7645-7131-1

INTERNET & DIGITAL MEDIA

0-7645-1664-7 0-7645-6924-4

* Separate Canadian edition also available
† Separate U.K. edition also available

32_588745 bob.qxd 3/16/05 9:17 PM Page 391

Page 410

Also available:
�Adobe Acrobat 6 PDF For Dummies

0-7645-3760-1
�Building a Web Site For Dummies

0-7645-7144-3
�Dreamweaver MX 2004 For Dummies

0-7645-4342-3
�FrontPage 2003 For Dummies

0-7645-3882-9
�HTML 4 For Dummies

0-7645-1995-6
�Illustrator CS For Dummies

0-7645-4084-X

�Macromedia Flash MX 2004 For Dummies
0-7645-4358-X

�Photoshop 7 All-in-One Desk
Reference For Dummies
0-7645-1667-1

�Photoshop CS Timesaving Techniques
For Dummies
0-7645-6782-9

�PHP 5 For Dummies
0-7645-4166-8

�PowerPoint 2003 For Dummies
0-7645-3908-6

�QuarkXPress 6 For Dummies
0-7645-2593-X

SPORTS, FITNESS, PARENTING, RELIGION & SPIRITUALITY

Also available:
�Adoption For Dummies

0-7645-5488-3
�Basketball For Dummies

0-7645-5248-1
�The Bible For Dummies

0-7645-5296-1
�Buddhism For Dummies

0-7645-5359-3
�Catholicism For Dummies

0-7645-5391-7
�Hockey For Dummies

0-7645-5228-7

�Judaism For Dummies
0-7645-5299-6

�Martial Arts For Dummies
0-7645-5358-5

�Pilates For Dummies
0-7645-5397-6

�Religion For Dummies
0-7645-5264-3

�Teaching Kids to Read For Dummies
0-7645-4043-2

�Weight Training For Dummies
0-7645-5168-X

�Yoga For Dummies
0-7645-5117-5

Also available:
�Alaska For Dummies

0-7645-1761-9
�Arizona For Dummies

0-7645-6938-4
�Cancún and the Yucatán For Dummies

0-7645-2437-2
�Cruise Vacations For Dummies

0-7645-6941-4
�Europe For Dummies

0-7645-5456-5
�Ireland For Dummies

0-7645-5455-7

�Las Vegas For Dummies
0-7645-5448-4

�London For Dummies
0-7645-4277-X

�New York City For Dummies
0-7645-6945-7

�Paris For Dummies
0-7645-5494-8

�RV Vacations For Dummies
0-7645-5443-3

�Walt Disney World & Orlando For Dummies
0-7645-6943-0

TRAVEL

GRAPHICS, DESIGN & WEB DEVELOPMENT

0-7645-5146-9 0-7645-5418-2

0-7645-5438-7 0-7645-5453-0

0-7645-4345-8 0-7645-5589-8

Also available:
�A+ Certification For Dummies

0-7645-4187-0
�Access 2003 All-in-One Desk

Reference For Dummies
0-7645-3988-4

�Beginning Programming For Dummies
0-7645-4997-9

�C For Dummies
0-7645-7068-4

�Firewalls For Dummies
0-7645-4048-3

�Home Networking For Dummies
0-7645-42796

�Network Security For Dummies
0-7645-1679-5

�Networking For Dummies
0-7645-1677-9

�TCP/IP For Dummies
0-7645-1760-0

�VBA For Dummies
0-7645-3989-2

�Wireless All In-One Desk Reference
For Dummies
0-7645-7496-5

�Wireless Home Networking For Dummies
0-7645-3910-8

NETWORKING, SECURITY, PROGRAMMING & DATABASES

0-7645-6852-3 0-7645-5784-X

32_588745 bob.qxd 3/16/05 9:17 PM Page 392

Similer Documents