230 likes | 346 Views
COMP206-08S General Programming 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
E N D
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
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
Course Outline • Lectures (Mon, Wed, Fri) • Labs every day – supervised 10-11 • Assessment = 6 weekly assignments + 2 tests Department of Computer Science
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
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
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
Types Primitive types and constants: byte aByte = 12; // -128 to 127 short aShort = 354; // -32,768 to 32,767 int anInt = 55677; // +- 2 billion long aLong = 12345678900; // +-2 power 63 float aFloat = 32.1f; // 32-bit floating point 6 sdig double aDouble = 26.5; // 64-bit 15 sig digits char aChar = ‘b’; // Unicode 30,000 chars! boolean aBool = true; Department of Computer Science
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
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 method System.out.println(“Hello World”); Department of Computer Science
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
Conditional statements • Equality operators: • ==, != • Relational operators: • <, <=, >, >= • Logical operators: • &&, ||, ! • The if statement if (expression) statement next statement Department of Computer Science
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
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
Loops • The for statement: • for (initialisation; test; update) statement next statement • The while statement: • while(expression) statement next statement • The do statement: • do statement while (expression); next statement Department of Computer Science
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
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
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
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
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
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
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
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 Department of Computer Science