Essential_Java_for_Scientists_and_Engineers.pdf

(2179 KB) Pobierz
434619774 UNPDF
Essential Java for Scientists and Engineers
Essential Java
for
Scientists and Engineers
Brian D. Hahn
Department of Mathematics & Applied Mathematics
University of Cape Town
Rondebosch
South Africa
Katherine M. Malan
Department of Computer Science
University of Cape Town
Rondebosch
South Africa
OXFORD AMSTERDAM BOSTON LONDON NEW YORK PARIS
SAN DIEGO SAN FRANCISCO SINGAPORE SYDNEY TOKYO
434619774.002.png
Butterworth-Heinemann
An imprint of Elsevier Science
Linacre House, Jordan Hill, Oxford OX2 8DP
225 Wildwood Avenue, Woburn, MA 01801-2041
First published 2002
Copyright c
2002 Brian D. Hahn and Katherine M. Malan. All rights reserved
The right of Brian D. Hahn and Katherine M. Malan to be identified
as the authors of this work has been asserted in accordance with
the Copyright, Designs and Patents Act 1988
All rights reserved. No part of this publication
may be reproduced in any material form (including
photocopying or storing in any medium by electronic
means and whether or not transiently or incidentally
to some other use of this publication) without the
written permission of the copyright holder except
in accordance with the provisions of the Copyright,
Designs and Patents Act 1988 or under the terms of a
licence issued by the Copyright Licensing Agency Ltd,
90 Tottenham Court Road, London, England W1T 4LP.
Applications for the copyright holder’s written permission
to reproduce any part of this publication should be addressed
to the publishers
British Library Cataloguing in Publication Data
A catalogue record for this book is available from the British Library
Library of Congress Cataloguing in Publication Data
A catalogue record for this book is available from the Library of Congress
ISBN 0 7506 5422 8
For information on all Butterworth-Heinemann publications visit our website at www.bh.com
Typeset by Laserwords Private Limited, Chennai, India.
Printed and bound in Martins the Printers of Berwick Upon Tweed
434619774.003.png 434619774.004.png
Contents
Preface
xiii
Acknowledgements
xvi
Part I Essentials
1
1 Getting going
3
1.1 Introduction to programming
3
Java as a programming language
3
1.2 Setting up your computer for programming in Java
4
Installing Java 2
4
Jikes as an alternative compiler
5
Installing the essential package
6
1.3 Writing your first Java program
6
What happens when a program is compiled?
7
Understanding FirstProgram
7
Commands for printing to the screen
8
1.4 Input and output
9
How input from the keyboard works
10
Reading in numbers from the keyboard
10
Input without the Keyboard class (optional)
10
Example: calculating compound interest
11
1.5 Comments
11
1.6 Using objects
12
Using the Turtle class
12
Help on the essential package
13
Using the Graph class
14
1.7 Java on the WWW (optional)
15
2 Java programming basics
20
2.1 Compound interest again
20
2.2 Primitive data types
23
Bits and bytes
23
v
434619774.005.png
vi Contents
Numeric constants
24
double is default
24
2.3 Names
25
Identifiers
25
Case sensitivity
25
Va r i a b l e s
25
2.4 Vertical motion under gravity
26
2.5 Operators, expressions and assignments
27
Arithmetic operators
27
Precedence
27
Increment and decrement operators
28
Assignments and assignment operators
29
Cast operators
30
2.6 Repeating with for
30
Turtle spirals
30
Growing an investment
33
The for statement
34
Alternative ways of counting with for
35
Square rooting with Newton
35
Factorials!
36
Limit of a sequence
37
Reading data from a text file
37
2.7 Deciding with if
38
The if-else statement
40
The if-else-if statement
40
The if-else-if ladder
40
for and if : counting passes
41
Rolling dice
41
Logical operators
42
Boolean variables
43
Nested if s
44
The switch statement
44
2.8 Characters
45
Escape sequences
45
2.9 Math methods
46
2.10 Programming style
46
3 Solving a problem in Java
55
3.1 Introduction
55
3.2 The class provider, class user and end user
56
3.3 What are objects and classes?
56
Looking at the Turtle class in more depth
57
3.4 Writing and using a simple class
58
3.5 How memory works
59
What is memory?
59
How objects and primitives are stored in memory
60
The null keyword
60
3.6 The String class
62
Equality testing of strings
63
3.7 Understanding methods
63
Method arguments
63
Return types
64
Signature of a method
64
434619774.001.png
Zgłoś jeśli naruszono regulamin