DO We) ©). aBae ai o>— _ »x 4 Keead Yoyh aa & » ps ; bh 7 d ep. p A * F # we i a Engineers “GS cientists f Fd Judith Bishop & Nigel Bishop ¥ RS NN | 2.St abe | = Mssissaugy onranig § Java Gently for Engineers and Scientists INTERNATIONAL COMPUTER SCIENCE SERIES Consulting Editor: A D McGettrick University of Strathclyde SELECTED TITLES IN THE SERIES Software Development with Z J B Wordsworth Program Verification N Francez Concurrent Systems: An Integrated Approach to Operating Systems, Database, and Distributed Systems (2nd edn) J Bacon Concurrent Programming <A Burns and G Davies Comparative Programming Languages (2nd edn) L B Wilson and R G Clark Programming in Ada 95 (2nd edn) J G P Barnes Software Design D Budgen Ada 95 From the Beginning (3rd edn) J Skansholm Programming Language Essentials H E Bal and D Grune Human-Computer Interaction J Preece et al. Distributed Systems: Concepts and Design (2nd edn) G Coulouris, J Dollimore and T Kindberg Fortran 90 Programming TM R Ellis, I R Philips and T M Lahey Foundations of Computing: System Development with Set Theory and Logic T Scheurer Principles of Object-Oriented Software Development 4 Eliéns Object-Oriented Programming in Eiffel (2nd edn) P Thomas and R Weedon Compiler Design R Wilhelm and D Maurer Miranda: The Craft of Functional Programming S$ Thompson Haskell: The Craft of Functional Programming (2nd edn) S Thompson Software Engineering (Sth edn) J Sommerville Compiler Construction N Wirth Software Engineering with BJ B Wordsworth Functional CP Hartel and H Muller Java Gently: Programming Principles Explained (2nd edn) J Bishop C++ From the Beginning J Skansholm Ada 95 for C and C++ Programmers S Johnston Algorithms and Data Structures: Design, Correctness, Analysis (2ndedn) J Kingston Discover Delphi: Programming Principles Explained S$ Williams and S Walmsley Introductory Logic and Sets for Computer Scientists N Nissanke Discrete Mathematics for Computer Scientists (2nd edn) J K Truss Introduction to Programming using SML_M Hansen and H Rischel Algorithms: a Functional Programming Approach F Rabhi and G Lapalme Java Gently for Engineers and Scientists Judith M Bishop Department of Computer Science University of Pretoria Nigel T Bishop Department of Mathematics, Applied Mathematics and Astronomy University of South Africa An imprint of Pearson Education Harlow, England - London - New York - Reading, Massachusetts - San Francisco Toronto - Don Mills, Ontario - Sydney - Tokyo - Singapore - Hong Kong - Seoul Taipei - Cape Town - Madrid - Mexico City - Amsterdam - Munich - Paris - Milan Pearson Education Limited Edinburgh Gate Harlow Essex CM 20 2JE England and Associated Companies throughout the world Visit us on the World Wide Web at: http://www.pearsoneduc.com First published 2000 © Pearson Education Limited 2000 The rights of Judith Bishop and Nigel Bishop to be identified as authors of this Work have been asserted by them in accordance with the Copyright, Designs and Patents Act 1988. All rights reserved; no part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form-or by any means, electronic, mechanical, photocopying, recording, or otherwise without either the prior written permission oft he publisher or a licence permitting restricted copying in the United Kingdom issued by the Copyright Licensing Agency Ltd, 90 Tottenham Court Road, London W1P OLP. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Pearson Education Limited has made every attempt to supply trademark information about manufacturers and their products mentioned in this book. A list of the trademark designations and their owners appears below. ISBN 0-201-34304-5 British Library Cataloguing-in-Publication Data A catalogue record for this book is available from the British Library Library of Congress Cataloging-in-Publication Data A catalog record for this book can be obtained from the Library of Congress 10987654321 05 04 03 02 01 00 Typeset by 43 Printed and bound in Great Britain by T.J. International Ltd., Padstow, Cornwall Trademark Notice The following are trademarks or registered trademarks of their respective companies: Unix is a trademark of X/Open Company Ltd; Windows, Windows 95, Windows NT, Visual Basic, Visual J++ and MS-DOS are trademarks of Microsoft Corporation; Netscape and Netscape Communicator are trademarks of Netscape Communications Corporation; Explorer is a trademark of Microsoft; Mosaic is a trademark of the University of Illinois; Macintosh, iMac and MacOS are trademarks of Apple Computer Inc.; Pentium and Intel are trademarks of Intel Corporation; Silicon Graphics is a trademark of Silicon Graphics Inc.; Java Workshop, HotJava, Java Development Kit and Java Platform 2 are trademarks of Sun Microsystems Inc.; GIF is a trademark of Unisys Corporation; Symantec Café is a trademark of Symantec Corporation; JBuilder is a trademark of Borland; CodeWarrior is a trademark of Metrowerks; Linux is a trademark of Linus Torvalds; Kawa is a trademark of Tek-Tools. To our parents in France, Francoise and Michael And in memory of Jean, Yvonne, Francis and Irene who were always so interested in our scientific careers Digitized by the Internet Archive in 2022 with funding from Kahle/Austin Foundation https://archive.org/details/javagentlyforengOO00Obish Contents Example programs and Case studies Preface Xill Chapter | Introduction 1.1 Welcome to Java 1.2 Software development 1.3 Scientific computing 1.4 Getting started with Java 1.5 The web site Quiz ° Chapter 2 Simple programs 2.1 Two starter programs 2.2 Java basics 2.3 Beginning with output 2.4 Expressions and assignment 2.5 Numerical computing Quiz Problems Chapter 3 Structuring 3.1 Properties of a good program 3.2 Repetition with for-loops 3.3. Input and output on a display 3.4 Making methods 5d lneGaaoh.class 3.6 Numerical methods with for-loops Quiz Problems viii Contents Chapter 4 Controlling 99 4.1 Files for input and output 99 4.2. The types boolean and char 106 4.3 Selection with if-else and switch 1 it 4.4 Conditional loops with while and do 116 4.5 Handling exceptions [21 4.6 Arithmetic accuracy 126 4.7 Numerical methods with while-loops 133 Quiz 138 Problems lie) Chapter 5 Arrays and matrices 143 pel Simple arrays 143 5.2. Matrices 156 5.3 Numerical methods — linear curve fitting 161 5.4 Numerical methods — solving linear equations 167 5.5 Case study 1: Linear boundary value problem |Mi ge Quiz 181 Problems 182 Chapter 6 Abstraction 185 6.1 Class power 185 6.2 Class-independent tables 190 6.3. Abstract methods and classes 196 6.4 Working within a hierarchy 206 6.5 Numerical methods with abstraction 216 6.6 Case study 2: Process control Zo Quiz 238 Problems 239 Chapter 7 Customizing 243 7.1. Introduction to the awt 243 7.2 Laying out a GUI 255 7.3 Events, listeners and handlers 261 7.4 Inside the Graph class 267