Download java PDF

Titlejava
TagsInheritance (Object Oriented Programming) Java Programming Language Class (Computer Programming) Method (Computer Programming) Data Type
File Size763.0 KB
Total Pages395
Document Text Contents
Page 1

Programming in Java

by
Willi-Hans Steeb
International School for Scientific Computing
email address of the author:

[email protected]

and
Yorick Hardy
International School for Scientific Computing
email address of the author:

[email protected]

Web page:

http://issc.uj.ac.za

Page 2

Contents

1 Introduction 1

1.1 Why Java ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

1.2 Aim of Object-Oriented Programming . . . . . . . . . . . . . . . . . 2

1.2.1 Information Hiding . . . . . . . . . . . . . . . . . . . . . . . . 3

1.2.2 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

1.2.3 Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

1.2.4 Built-In Classes . . . . . . . . . . . . . . . . . . . . . . . . . . 4

1.2.5 Java Compared To C++ . . . . . . . . . . . . . . . . . . . . . 5

1.3 Identifiers and Keywords in Java . . . . . . . . . . . . . . . . . . . . 6

2 Java Basics 7

2.1 My First Java Program . . . . . . . . . . . . . . . . . . . . . . . . . . 7

2.2 My First Applet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

2.3 Basic Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

2.4 Arithmetic Operations . . . . . . . . . . . . . . . . . . . . . . . . . . 16

2.5 Unicode, ASCII Table, and Types Conversion . . . . . . . . . . . . . 18

2.6 Precedence Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

2.7 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

2.8 Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

2.8.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

2.8.2 The if Statement . . . . . . . . . . . . . . . . . . . . . . . . . 30

2.8.3 The for Loop, while Loop, do-while Loop . . . . . . . . . . . . 31

2.8.4 The switch Statement . . . . . . . . . . . . . . . . . . . . . . 35

2.9 Logical AND, Logical OR and Logical NOT . . . . . . . . . . . . . . 38

2.10 Bitwise Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

2.11 Shift Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42

2.12 Pass by Value, Pass by Reference . . . . . . . . . . . . . . . . . . . . 43

2.13 Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

2.14 Jump Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

2.15 Reading from Keyboard . . . . . . . . . . . . . . . . . . . . . . . . . 54

2.16 Command Line Arguments . . . . . . . . . . . . . . . . . . . . . . . . 56

2.17 System Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

2.18 Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61

2.19 Applets and HTML Parameters . . . . . . . . . . . . . . . . . . . . . 62
i

Page 197

8.2. EXAMPLES 189

The following program counts the number of occurrences of the name Miller in the
file data.dat. All the lines in the file data.dat are read using the command

while(!(null == (buffer = in.readLine())))

The StringTokenizer class is used.

// NoOfNames.java

import java.io.*;

import java.util.*;

public class NoOfNames

{

public static void main(String[] args) throws IOException

{

FileInputStream fin = new FileInputStream("data.dat");

BufferedReader in;

in = new BufferedReader(new InputStreamReader(fin));

int count = 0;

String buffer;

String name = new String("Miller");

while(!(null == (buffer = in.readLine())))

{

StringTokenizer tok = new StringTokenizer(buffer);

while(tok.hasMoreTokens())

{

String s = tok.nextToken();

if(s.equals(name))

count++;

}

}

System.out.println("Name " + name + " occurse " + count + " times");

}

}

Page 198

190 CHAPTER 8. FILE MANIPULATIONS

In the following three programs we show how readByte is used in file manipulation.
A file can be considered as a sequence of bytes.

In the following program we read in the file character by character or more precisly
byte by byte. Then we do type conversion to char. Everytime we read a byte
we test for the end of the file. This is done with the method available(). The
program counts the number of characters in the file.

// EndOfFile.java

import java.io.*;

public class EndOfFile

{

public static void main(String[] args)

{

long count = 0L;

try

{

DataInputStream in = new DataInputStream(

new BufferedInputStream(

new FileInputStream("EndOfFile.java")));

while(in.available() != 0)

{

System.out.print((char) in.readByte()); // type conversion

count++;

}

in.close();

} // end try block

catch(IOException e)

{

System.err.println("IOException");

}

System.out.println("count = " + count);

}

}

Page 394

Index

Arrays, 22
BufferedReader, 54
Character, 76
Color, 162
DataInputStream, 179
DataOutputStream, 179
Graphics, 153
Image, 166
Iterator, 310
JTree, 345
Math, 55, 80, 378
Number, 75
Object, 27, 85
Panel, 141
Scanner, 379
Socket, 282
String, 64
StringBuffer, 72
StringTokenizer, 69
TreeMap, 321
TreeSet, 317
available(), 190
break, 31, 35, 51
case, 35
catch, 173
char, 14
class, 2
clone(), 27
continue, 51
default, 35
dispose, 153
do-while, 31
double, 13
drawArc, 153
drawLine, 153
drawOval, 153
drawRect, 153

exit, 57
extends, 117
final, 16
finally, 177
float, 13
for, 31
getGraphics, 153
goto, 51
if, 30
length, 22
length(), 37
main(), 8
new, 22
notify(), 245
null, 188
paint, 153
readLine(), 54, 180
repaint, 231
return, 51
run, 228
static, 8, 10
super, 120
supper, 3
switch, 35
synchronized, 240
this, 88
throw, 173
transient, 203, 207
try, 173
wait(), 245
while, 31

Abstract class, 114
Applet, 9
Array, 22

Basic data types, 11
Binary notation, 40

386

Page 395

Bitwise operations, 40
Broadcast address, 268

Character, 14
Checksum, 192, 194
Constructor, 123
Control statements, 29

Default constructor, 123
deprecated, 4

Equality operator, 14
Exception, 173

Fibonacci numbers, 50
Fields, 4

Garbage collection, 22
Garbage collector, 97
Generic Types, 374

Host, 267

Infinity symbol, 18
Information hiding, 3
Inheritance, 3, 114, 117
Inner classes, 129
Interfaces, 131
Internationalization, 218
Internet, 267

Layout Managers, 133
Locale object, 218
Logical operators, 38

Methods, 4
Modulus operator, 16, 34
Multicast address, 268
Multitasking, 228
Multithreading, 228
MVC, 341

Network, 267
Node, 267

Objects, 8

Pass by reference, 43
Pass by value, 43

Polygon, 34
Polymorphism, 4
Port, 273
Postincrement operator, 20
Precedence, 20
Preincrement operator, 20
Primitive data types, 11
Priority, 237

Recursion, 48
Regular expression, 98
Relational operators, 29
Remainder operator, 16

Scope, 10
Serialization, 203
Shift operators, 42
Socket, 282
String, 14
Synchronization problem, 245

Thread, 228
Tool tips, 336
Two Complement, 41
Type conversion operator, 11

Unicast address, 268
Unicode, 14, 18

Varargs functionality, 378

Wrapper class, 75

387

Similer Documents