JavaKid8x11.pdf

(6481 KB) Pobierz
Java
TM
Programming for Kids,
Parents
and GrandParents
Yakov Fain
Java Programming for Kids, Parents and Grandparents
iii
Java Programming for Kids, Parents and Grandparents
by Yakov Fain
Copyright
©
2004 Smart Data Processing, Inc.
14 Molly Pitcher Dr.
Manalapan, New Jersey, 07726, USA
All rights reserved. No part of this book may be reproduced, in any form or by
any, without permission in writing from the publisher.
Cover design and illustrations:
Adult technical editor:
Kid technical editor:
Yuri Fain
Yuri Goncharov
David Fain
May 2004:
First Electronic Edition
The information in this book is distributed without warranty. Neither the author nor the publisher
shall have any liability to any person or entitle to any liability, loss or damage to be caused directly
or indirectly by instructions contained in this book or by the computer software or hardware
products described herein.
Java and all Java-based trademarks and logos are trademarks or registered trademarks of Sun
Microsystems, Inc. in the United States and other countries.
Windows 98, Windows NT, Windows 2000 and Windows XP are trademarks of Microsoft Corporation.
All other product names and company names are the property of their respective owners.
The publisher offers discount on this book when ordered in bulk quantities. For
more information, send an e-mail at books@smartdataprocessing.com.
ISBN: 0-9718439-5-3
Table of Contents
PREFACE
............................................................................................... IX
ACKNOWLEDGEMENTS.............................................................................
XI
CHAPTER 1. YOUR FIRST JAVA PROGRAM....................................................
1
How to Install Java on Your Computer ....................................................................................... 2
Three Main Steps in Programming .............................................................................................. 6
Step 1 – Type the Program ........................................................................................................... 6
Step 2 – Compile the Program...................................................................................................... 8
Step 3 – Run the Program............................................................................................................. 9
Additional Reading ...................................................................................................................... 10
CHAPTER 2. MOVING TO ECLIPSE..............................................................
11
Installing Eclipse .......................................................................................................................... 11
Getting Started with Eclipse........................................................................................................ 13
Creating Programs in Eclipse ..................................................................................................... 15
Running
HelloWorld
in Eclipse............................................................................................... 16
How
HelloWorld
Works? ........................................................................................................ 17
Additional Reading ...................................................................................................................... 20
Practice.......................................................................................................................................... 20
Practice for Smarty Pants............................................................................................................ 21
CHAPTER 3. PET AND FISH – JAVA CLASSES
.............................................. 22
Classes and Objects ...................................................................................................................... 22
Data Types .................................................................................................................................... 25
Creation of a Pet.......................................................................................................................... 28
Inheritance – a Fish is Also a Pet ................................................................................................ 33
Method Overriding ...................................................................................................................... 37
Additional Reading ...................................................................................................................... 38
Practice.......................................................................................................................................... 38
Practice for Smarty Pants............................................................................................................ 39
Java Programming for Kids, Parents and Grandparents
v
CHAPTER 4. JAVA BUILDING BLOCKS
....................................................... 40
Program Comments ..................................................................................................................... 40
Making Decisions with
if
Statements........................................................................................ 41
Logical Operators......................................................................................................................... 43
The
logical not
here is applied to the expression in parentheses. ............................................. 44
Conditional operator.................................................................................................................... 44
Using
else if
............................................................................................................................ 44
Making Decisions With
switch
Statement.............................................................................. 45
How Long Variables Live? .......................................................................................................... 46
Special Methods: Constructors ................................................................................................... 47
The Keyword
this......................................................................................................................
48
Arrays............................................................................................................................................ 49
Repeating Actions with Loops..................................................................................................... 51
Additional Reading ...................................................................................................................... 54
Practice.......................................................................................................................................... 54
Practice for Smarty Pants............................................................................................................ 54
CHAPTER 5. A GRAPHICAL CALCULATOR...................................................
55
AWT and Swing ........................................................................................................................... 55
Packages and Import Statements................................................................................................ 55
Major Swing Elements................................................................................................................. 56
Layout Managers.......................................................................................................................... 59
Flow Layout ............................................................................................................................... 59
Grid Layout ................................................................................................................................ 60
Border Layout ............................................................................................................................ 62
Combining Layout Managers ..................................................................................................... 62
Box Layout................................................................................................................................. 65
Grid Bag Layout......................................................................................................................... 66
Card Layout................................................................................................................................ 68
Can I Create Windows Without Using Layouts? ....................................................................... 68
Window Components ................................................................................................................... 68
Additional Reading ...................................................................................................................... 72
Practice.......................................................................................................................................... 72
Practice for Smarty Pants............................................................................................................ 73
Zgłoś jeśli naruszono regulamin