Presentation is loading. Please wait.

Presentation is loading. Please wait.

Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.

Similar presentations


Presentation on theme: "Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2."— Presentation transcript:

1 Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2

2 Department of Computer Science2 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

3 Department of Computer Science3 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#

4 Department of Computer Science4 Course Outline Lectures (Mon, Wed, Fri) Labs every day – supervised 10-11 Assessment = 6 weekly assignments + 2 tests

5 Department of Computer Science5 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

6 Department of Computer Science6 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/ 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.

7 Department of Computer Science7 Java – summary of syntax Comments  One-line use:  // This is a comment  Multi-line use:  /* This is a bit longer */  Javadoc  /** Automatic documentation! */

8 Department of Computer Science8 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;

9 Department of Computer Science9 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

10 Department of Computer Science10 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”);

11 Department of Computer Science11 Basic Operators Assignment operators:  +, +=, -=, *=, /= Binary arithmetic operators:  +, -, *, /, % (remainder) Unary operators:  -, ++, -- Type conversion operators:  int x = 6; int y = 10;  double q = (double) x / y;

12 Department of Computer Science12 Conditional statements Equality operators:  ==, != Relational operators: , >= Logical operators:  &&, ||, ! The if statement if (expression) statement next statement

13 Department of Computer Science13 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

14 Department of Computer Science14 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

15 Department of Computer Science15 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

16 Department of Computer Science16 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

17 Department of Computer Science17 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.

18 Department of Computer Science18 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

19 Department of Computer Science19 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

20 Department of Computer Science20 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

21 Department of Computer Science21 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)

22 Department of Computer Science22 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

23 Department of Computer Science23 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


Download ppt "Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2."

Similar presentations


Ads by Google