Table Of ContentTEAM LinG
JavaTech
AnIntroductiontoScientificandTechnicalComputingwithJava
JavaTechisapracticalintroductiontotheJavaprogramminglanguagewithan
emphasisonthefeaturesthatbenefittechnicalcomputing,suchasplatform
independence,extensivegraphicscapabilities,multi-threading,andtoolsto
developnetworkanddistributedcomputingsoftwareandembeddedprocessor
applications.
Thebookisdividedintothreeparts.Thefirstpresentsthebasicsof
object-orientedprogramminginJavaandthenexaminestopicssuchas
graphicalinterfaces,threadprocesses,I/O,andimageprocessing.Thesecond
partbeginswithareviewofnetworkprogramminganddevelopsWeb
client-serverexamplesfortaskssuchasmonitoringofremotedevices.The
focusthenshiftstodistributedcomputingwithRMI,whichallowsprogramson
differentplatformstoexchangeobjectsandcalleachother’smethods.CORBA
isalsodiscussedandasurveyofwebservicesispresented.Thefinalpart
examineshowJavaprogramscanaccessthelocalplatformandinteractwith
hardware.TopicsincludecombiningnativecodewithJava,communicationvia
seriallines,andprogrammingembeddedprocessors.
JavaTechdemonstratestheeasewithwhichJavacanbeusedtocreate
powerfulnetworkapplicationsanddistributedcomputingapplications.Itcanbe
usedasatextbookforintroductoryorintermediatelevelprogrammingcourses,
andformoreadvancedstudentsandresearcherswhoneedtolearnJavafora
particulartask.JavaTechisuptodatewithJava5.0.
C S. L receivedhisPh.D.inphysicsfromtheUniversityof
CaliforniaatRiversideandhasheldresearchpositionsatIowaStateUniversity,
Fermilab,andtheRoyalInstituteofTechnology,Sweden.Thisbookgrewoutof
acourseinJavaprogramminghedevelopedwithProfessorLindblad.Henow
runshisowncompanythatdevelopsJavaapplications,Webpublications,and
educationaltoolsandmaterials.
J S. T holdsaPh.D.inComputationalPlasmaPhysicsand
hasworkedinfusionenergyresearch,computersecurity,andtrustedoperating
systems.HeisaSunCertifiedJavaProgrammerandhasbeenactivelyusing
Javasince1997.HeiscurrentlyatOakRidgeNationalLaboratory,developing
WebservicessoftwareandaGPSvehicletrackingapplicationusing
GPS-enabledwirelessphonehandsetsandotherGPSdevices.
T L receivedhisPh.D.inphysicsattheUniversityof
Stockholmin1972andbecameassociateprofessortwoyearslater.Heis
currentlyaprofessorintheDepartmentofPhysicsattheRoyalInstituteof
Technology,Stockholm,andalsoservesparttimeasDirectorofUndergraduate
Studies.Hisresearchcurrentlyconcentratesontechniquesinimageanddata
analysisinhighdataratesystems.
JavaTech
An Introduction to Scientific and Technical
Computing with Java
Clark S. Lindsey, Johnny S. Tolliver
and Thomas Lindblad
cambridge university press
Cambridge, New York, Melbourne, Madrid, Cape Town, Singapore, São Paulo
Cambridge University Press
The Edinburgh Building, Cambridge cb2 2ru, UK
Published in the United States of America by Cambridge University Press, New York
www.cambridge.org
Information on this title: www.cambridge.org/9780521821131
© C. S. Lindsey, J. S. Tolliver and T. Lindblad 2005
This publication is in copyright. Subject to statutory exception and to the provision of
relevant collective licensing agreements, no reproduction of any part may take place
without the written permission of Cambridge University Press.
First published in print format 2005
isbn-13 978-0-511-13093-9 eBook (NetLibrary)
isbn-10 0-511-13093-7
eBook (NetLibrary)
isbn-13 978-0-521-82113-1 hardback
isbn-10 0-521-82113-4 hardback
Cambridge University Press has no responsibility for the persistence or accuracy of urls
for external or third-party internet websites referred to in this publication, and does not
guaranteethatanycontentonsuchwebsitesis,orwillremain,accurateorappropriate.
Contents
Preface page xiii
Acknowledgements xix
PartI IntroductiontoJava
1 Introduction 3
1.1 WhatisJava? 3
1.2 HistoryofJava 4
1.3 VersionsofJava 5
1.4 Java–openorclosed? 8
1.5 Javafeaturesandbenefits 8
1.6 Real-worldJavaapplicationsinscienceandengineering 12
1.7 TheJavaprogrammingprocedure 14
1.8 Gettingstarted 17
1.9 ChangesinJava2StandardEdition5.0 23
1.10 WebCoursematerials 27
References 27
Resources 28
2 Languagebasics 29
2.1 Introduction 29
2.2 Languageelementsandstructures 29
2.3 Asimpleapplication 31
2.4 Comments 32
2.5 DatatypesandJavaprimitives 33
2.6 Strings 35
2.7 Expressions 35
2.8 Operators 36
2.9 Statements 39
2.10 Castsandmixing 44
2.11 Floating-point 47
2.12 Programming 50
2.13 BasicmathinJava 53
2.14 WebCoursematerials 55
Resources 55
v
vi Contents
3 ClassesandobjectsinJava 57
3.1 Introduction 57
3.2 Customdatatypes 57
3.3 Classdefinition 58
3.4 Classinstantiation 65
3.5 Static(orclass)members 67
3.6 Moreaboutprimitiveandreferencevariables 69
3.7 Wrappers 73
3.8 Arrays 79
3.9 Exceptions 80
3.10 OOPinengineeringandscience 84
3.11 WebCoursematerials 90
Resources 90
4 MoreaboutobjectsinJava 91
4.1 Introduction 91
4.2 Classinheritance 91
4.3 Moreaboutconstructors 100
4.4 Abstractmethodsandclasses 105
4.5 Interfaces 109
4.6 Moreaboutclasses 115
4.7 Moreaboutarrays 120
4.8 Improvedcomplexnumberclass 125
4.9 Randomnumbergeneration 126
4.10 Improvedhistogramclass 128
4.11 UnderstandingOOP 130
4.12 WebCoursematerials 130
References 131
Resources 131
5 OrganizingJavafilesandotherpracticalities 132
5.1 Introduction 132
5.2 Classdefinitionfiles 132
5.3 Packages 133
5.4 Thefinalmodifierandconstants 140
5.5 StaticimportinJ2SE5.0 141
5.6 JARfiles 143
5.7 DistributingJavacode 146
5.8 Appletdirectories 148
5.9 Javadoc 149
5.10 Codingconventions 150
5.11 Formattingnumbers 153
Contents vii
5.12 WebCoursematerials 158
References 159
Resources 159
6 Javagraphics 160
6.1 Introduction 160
6.2 AWT 161
6.3 Swing:lightweightbeatsheavyweight 162
6.4 Swingclasshierarchy 163
6.5 Containers 164
6.6 Drawing 174
6.7 DrawingwiththeGraphicsclass 178
6.8 DrawingintheJava2DAPI 183
6.9 Images 190
6.10 Javaandtechgraphics 192
6.11 Histogramgraphics 192
6.12 WebCoursematerials 199
References 200
7 GraphicalUserInterfaces 201
7.1 Introduction 201
7.2 Events 201
7.3 Moreuserinterfacecomponents 212
7.4 Layoutmanagers 223
7.5 Convenienceclasses 237
7.6 Framesandmenus 242
7.7 Userinterfacewithhistogramdisplay 247
7.8 WebCoursematerials 251
References 252
8 Threads 253
8.1 Introduction 253
8.2 Introductiontothreads 253
8.3 Stoppingthreads 258
8.4 Multiprocessingissues 259
8.5 Usingmultiplethreads 262
8.6 Animations 276
8.7 Timers 281
8.8 ConcurrencyutilitiesinJ2SE5.0 285
8.9 WebCoursematerials 285
References 286
viii Contents
9 Javainput/output 287
9.1 Introduction 287
9.2 Streams 289
9.3 Streamwrappers 290
9.4 ConsoleI/O 291
9.5 TheFileclass 299
9.6 FileI/O 301
9.7 Characterencoding 312
9.8 ObjectI/O 313
9.9 Choosingastreamclass 315
9.10 Primitivetypestobytesandback 315
9.11 Sources,destinations,andfilters 317
9.12 TheJFileChooserdialog 318
9.13 HistogramI/O 320
9.14 MoreJavaI/O 325
9.15 WebCoursematerials 326
References 326
10 Javautilities 327
10.1 Introduction 327
10.2 Thejava.utilpackage 327
10.3 VectorandEnumeration 328
10.4 Hashtable,Properties,andHashMap 329
10.5 Preferences 331
10.6 TheCollectionsFramework 335
10.7 GenericsinJ2SE5.0 338
10.8 ConcurrencyutilitiesinJ2SE5.0 341
10.9 EnumeratedtypesinJ2SE5.0 343
10.10 TheArraysclass 345
10.11 Toolsforstrings 349
10.12 Calendar,Date,andTime 353
10.13 Arbitraryprecisionnumbers 356
10.14 Bithandling 360
10.15 Otherutilities 362
10.16 WebCoursematerials 363
References 363
11 Imagehandlingandprocessing 365
11.1 Introduction 365
11.2 TheImageandBufferedImageclasses 365
11.3 Imageloading 367
11.4 Imagedisplay 371
11.5 Creatingimages 372
11.6 Savingimages 373
Contents ix
11.7 Imageprocessing 373
11.8 Pixelhandling 374
11.9 Filtering 380
11.10 WebCoursematerials 389
References 389
Resource 389
12 Moretechniquesandtips 390
12.1 Introduction 390
12.2 Printing 390
12.3 Cursoricons 392
12.4 Mousebuttons 394
12.5 Popupmenu 396
12.6 Handlingkeystrokes 399
12.7 Audio 402
12.8 Performanceandtiming 404
12.9 LifelongJavalearning 409
12.10 WebCoursematerials 410
References 410
PartII Javaandthenetwork
13 Javanetworkingbasics 413
13.1 Introduction 413
13.2 Internetbasics 413
13.3 Ports 415
13.4 Javanetworking 416
13.5 TheURLclass 417
13.6 InetAddress 423
13.7 Sockets 426
13.8 Theclient/servermodel 428
13.9 WebCoursematerials 430
References 430
Resources 430
14 AJavawebserver 431
14.1 Introduction 431
14.2 Designingawebserver 432
14.3 HypertexTransferProtocol(HTTP) 435
14.4 Runningtheserver 438
14.5 Amoresecureserver 439
14.6 Aclientapplication 443
14.7 Serverapplications 445
14.8 Servers,servletsandJSP 446