Geoff Holmes and Bernhard Pfahringer
COMP206-08SGeneral
Programming 2
Department of Computer Science 2
Course Goals
First three weeks:- To learn about Java programming- To learn about data structures – in particular, learning how
to use those provided with Java (in libraries)
Second three weeks:
- To learn about Polymorphism- To learn about Java GUI programming (Swing)- To learn more about Java libraries such as FileIO and
streams
Department of Computer Science 3
Java programming language
Developed by Sun in the early 1990s Object-oriented: programs consist of objects that
interact with each other Platform-independent: programs can run on many
operating systems Java code is translated to byte code, which is
executed by a JVM Similar to C#
Department of Computer Science 4
Course Outline
Lectures (Mon, Wed, Fri) Labs every day – supervised 10-11 Assessment = 6 weekly assignments + 2
tests
Department of Computer Science 5
Data Structures
A way of organizing data so that certain operations can be performed efficiently
Data structures are generic (ideally) Examples (linked lists, stacks, queues, trees, hash
tables, heaps) Many of these are implemented in Java’s
Collection Framework
Department of Computer Science 6
IDEs Most programming languages have integrated
development environments For Java we will use Eclipse For instructions on how to set up Eclipse please
see the 203 homepage at http://www.cs.waikato.ac.nz/~eibe/COMP203A/
Note that you can use Version 5 or Version 6 of Java – the book is based on Version 5 and Version 6 is the latest.
Department of Computer Science 7
Java – summary of syntax
Comments One-line use:
// This is a comment Multi-line use:
/* This is a bit
longer */ Javadoc
/** Automatic documentation! */
Department of Computer Science 8
TypesPrimitive types and constants:
byte aByte = 12; // -128 to 127short aShort = 354; // -32,768 to 32,767int anInt = 55677; // +- 2 billionlong aLong = 12345678900; // +-2 power 63float aFloat = 32.1f; // 32-bit floating point 6 sdigdouble aDouble = 26.5; // 64-bit 15 sig digitschar aChar = ‘b’; // Unicode 30,000 chars!boolean aBool = true;
Department of Computer Science 9
Compiling Java code The command javac file.java compiles source
code and creates bytecode Source code is stored in .java files Bytecode is stored in .class files
The command java starts the Java Virtual Machine (JVM) and executes the bytecode The bytecode is translated into machine-code Most JVMs are just-in-time compilers
Department of Computer Science 10
Useful methods When the bytecode for a .java file is executed, the
main method is called:public static void main (String[ ] args) { }
This is the starting point of a Java program To get output from your program you can write to
the standard output stream using the println methodSystem.out.println(“Hello World”);
Department of Computer Science 11
Basic Operators Assignment operators:
+, +=, -=, *=, /= Binary arithmetic operators:
+, - , *, /, % (remainder) Unary operators:
-, ++, -- Type conversion operators:
int x = 6; int y = 10; double q = (double) x / y;
Department of Computer Science 12
Conditional statements Equality operators:
==, != Relational operators:
<, <=, >, >= Logical operators:
&&, ||, ! The if statement
if (expression) statement
next statement
Department of Computer Science 13
Conditionals continued The switch statement can be used when several
options are needed Break statement exits from the innermost loop or
switch statement Continue statement goes to the next iteration of
the innermost loop immediately (ie avoiding the statements of the loop)
Conditional operator testExpr ? yesExpr : noExpr
Department of Computer Science 14
Methods Methods can be overloaded: a class can have
multiple methods with the same name (but different parameter lists)
Public methods are visible in other classes Static methods are not bound to a particular
object
Department of Computer Science 15
Loops The for statement:
for (initialisation; test; update)statement
next statement The while statement:
while (expression)statement
next statement The do statement:
dostatement
while (expression);next statement
Department of Computer Science 16
Methods Basic method header has a name, return type and
parameter list The method declaration includes the body Arguments are copied into the parameters: so
Java is pass by value The return statement is used to return a value to
the caller return must be present unless method is void
Department of Computer Science 17
Reference types All types that are not primitive types are
reference types A reference variable holds the address of an
object References can be compared using == and !=
This checks whether two variables refer to exactly the same object (ie they hold the same address)
References have the value null if they do not refer to an object.
Department of Computer Science 18
Objects
An instance of any of the non-primitive types is an object
The keyword new is used to invoke the constructor of an object
The dot operator is used to access an object via a reference variable
Objects are garbage-collected when they are no longer referenced by any variable
Department of Computer Science 19
Arrays
Indexed from 0 The [ ] operator is used to index an array An array behaves like an object
new is used to create a new array An array variable is a reference to an array
Multi-dimensional Dynamic arrays achieved via the ArrayList
Department of Computer Science 20
Exception handling
Handling exceptional circumstances incl errors A try block encloses code that might generate an
exception A catch block processes the exception A finally block maybe used which is always
executed Used for runtime (array bounds, null pointer etc),
checked (IO, file not found) and errors Exceptions are thrown
Department of Computer Science 21
Strings
Objects with special properties: Immutable – cannot change once constructed Can be concatenated using +
To check equality use the equals method Many other useful methods (check the Javadoc)
Department of Computer Science 22
I/O in Java
The java.io package contains methods for input and output
Packages are made available in your class using an import statement java.lang does not need an import statement
Java I/O is based on streams Predefined streams include System.out,
System.in and Sytem.err
Department of Computer Science 23
Examples To read lines of input from the terminal
Create an InputStreamReader from System.in Create a BufferedReader from the
InputStreamReader Call the readLine method on the BufferedReader
To split up a line (string) into sub strings use a StringTokenizer
For file I/O you can use a FileReader and a FileWriter
Top Related