Presentation is loading. Please wait.

Presentation is loading. Please wait.

About this course? EE2E1 (MS) EE2E1 (MS)  Introduction to Java programming  Basic ideas about objects and classes  We will also look at more advanced.

Similar presentations


Presentation on theme: "About this course? EE2E1 (MS) EE2E1 (MS)  Introduction to Java programming  Basic ideas about objects and classes  We will also look at more advanced."— Presentation transcript:

1 About this course? EE2E1 (MS) EE2E1 (MS)  Introduction to Java programming  Basic ideas about objects and classes  We will also look at more advanced features of Java GraphicsGraphics Files and streamsFiles and streams Multi-threadingMulti-threading NetworksNetworks EE2E2 (DP) EE2E2 (DP)  Object Oriented Software Design

2 Assessment EE2E1 and EE2E2 are assessed jointly (making up the EE2E module) EE2E1 and EE2E2 are assessed jointly (making up the EE2E module) EE2E1 is assessed through a classtest and programming exercises and a major programming assignment EE2E1 is assessed through a classtest and programming exercises and a major programming assignment  15% through a 1 hour class test  2 x 22.5% through 2 programming exercises  40% through a major programming assignment carried out in semester 2

3 Java resources Sun’s Java home page Sun’s Java home page  http://java.sun.com/ http://java.sun.com/ Java online tutorial Java online tutorial  http://java.sun.com/docs/books/tutorial/ http://java.sun.com/docs/books/tutorial/ Comparing C++and Java Comparing C++and Java  http://www.compapp.dcu.ie/~renaat/projects/cvjava.ht ml http://www.compapp.dcu.ie/~renaat/projects/cvjava.ht ml http://www.compapp.dcu.ie/~renaat/projects/cvjava.ht ml Textbook Textbook  Core Java 2. Volume 1-Fundamentals Core Java 2 Core Java 2  C.S.Horstmann, G. Cornell  Amazon Link Amazon Link Amazon Link My web page My web page  http://www.eee.bham.ac.u k/spannm/Courses/ee2e.h tm http://www.eee.bham.ac.u k/spannm/Courses/ee2e.h tm http://www.eee.bham.ac.u k/spannm/Courses/ee2e.h tm

4 Why should I learn Java? Main reason, Java is object oriented Main reason, Java is object oriented   What is OOP good for?   Modelling asynchronously interacting objects GUIs Event simulation Ray tracing visualisation CAD simulation Real-time control/embedded systems Robotics Image/Video processing Client/Server systems etc

5 OK, so what’s good about Java? Java is free to download and is easy to learn Java is free to download and is easy to learn Java has powerful (and free!) development tools e.g. Eclipse, Netbeans Java has powerful (and free!) development tools e.g. Eclipse, Netbeans Excellent documentation support – Javadocs Excellent documentation support – Javadocs Great community support Great community support Rich collection of open source libraries Rich collection of open source libraries Can develop Android apps in Java – supported by Eclipse and Netbeans Can develop Android apps in Java – supported by Eclipse and Netbeans  Android is a good platform for mobile apps because of  Android is a good platform for mobile apps because of ease of release, wide range of devices and its an open platform

6 EE2E1. JAVA Programming Lecture 1 From C to Java

7 Contents A simple Java program A simple Java program Data types Data types Variables Variables Assignment/initialization Assignment/initialization Operators Operators Control Flow Control Flow Strings Strings Arrays Arrays

8 Simple example Java program public class firstProgram { public static void main(String[] args) { System.out.println(“Java is fun!”); }

9 Main points Everything in a Java program is a class Everything in a Java program is a class Keyword public is an access modifier Keyword public is an access modifier Program starts execution from the main method Program starts execution from the main method We will worry about what static void means later We will worry about what static void means later The program prints the string “Java is fun!” The program prints the string “Java is fun!” System.out.println(“…”) means call the println() method of the object System.out (which is part of the class System) System.out.println(“…”) means call the println() method of the object System.out (which is part of the class System)

10 Data types Like C, Java is strongly typed Like C, Java is strongly typed Java has 8 primitive data types Java has 8 primitive data types Machine independent storage requirements Machine independent storage requirements

11 Primitive data typesType Storage requirement Range int 4 bytes -2,147,483,648.. 2,147,483,647 short 2 bytes -32768.. 32767 long 8 bytes Approx ± 9x10 18 byte 1 byte -128.. 127 float 4 bytes Approx ± 3.4x10 38 double 8 bytes Approx ± 1.8x10 308 char 2 bytes (Unicode) boolean false, true

12 The char datatype char represented by a 2-byte Unicode value char represented by a 2-byte Unicode value Designed to represent all characters in the written world Designed to represent all characters in the written world Allows 65536 characters (35000 are in use) whereas ascii only allows 255 Allows 65536 characters (35000 are in use) whereas ascii only allows 255 Expressed as hexidecimal ‘\u0000’ to ‘\uFFFF’ (‘\u0000’ to ‘\u00FF’ is the ascii set) Expressed as hexidecimal ‘\u0000’ to ‘\uFFFF’ (‘\u0000’ to ‘\u00FF’ is the ascii set)  \u indicates a Unicode value Check out www.unicode.org for more details Check out www.unicode.org for more detailswww.unicode.org

13 Variables Variables must be declared before use Variables must be declared before use Variable names must begin with a letter but can contain letters and digits Variable names must begin with a letter but can contain letters and digits Variable names are case sensitive Variable names are case sensitive

14 Assignment/initialization Assignment and initialization are identical to C Assignment and initialization are identical to C int myVariable=20;// initialization int anotherVariable; anotherVariable=myVariable;// assignment char yes=‘y’;// initialization char cc; cc=yes;// assignment

15 Constant variables final double electronicCharge=1.6E-19; electronicCharge=1.6E-18;// illegal assignment! In Java, the keyword final denotes a constant In Java, the keyword final denotes a constant Constant variables cannot be assigned to Constant variables cannot be assigned to

16 Operators Usual arithmetic operators + - * / are used in Java as in C Usual arithmetic operators + - * / are used in Java as in C Integer divide / and modulus % as in C Integer divide / and modulus % as in C Increment ++ and decrement -- Increment ++ and decrement -- Exponentiation uses pow() function which is part of the Math class Exponentiation uses pow() function which is part of the Math class double y=Math.pow(x,a); // y=x a

17 Relational and boolean operators Java uses the same relational operators as C Java uses the same relational operators as C  == (equal to)  != (not equal to) , = (less, greater, less or equal, greater or equal) Java uses the same bitwise operators as C Java uses the same bitwise operators as C  & (and)  | (or)  ^ (xor)  ~ (not)

18 Boolean expressions In Java the result of a boolean expression is a boolean type (true or false) In Java the result of a boolean expression is a boolean type (true or false) This can’t be converted to an int (as in C) This can’t be converted to an int (as in C) if (x == y) {…} // Result is a boolean Java eliminates a common C programming bug Java eliminates a common C programming bug if (x = y) {…} // Ok in C, won’t compile in // Java

19 Control flow Java uses the same control structures as in C Java uses the same control structures as in C Selection (conditional) statements Selection (conditional) statements  if (..) {…}  if (..) {…} else if (..) {…} …. else {…}  switch (..) { case 1: break; … default: break; } Iteration statements Iteration statements  for (..) {…}  while (..) {…}  do {…} while (..);

20 Example – a square root calculator public class SquareRoot { public static void main(String[] args) { double a,root; do { a=Console.readDouble("Enter a positive number : "); if (a<0) { System.out.println(“Please enter a positive number!); …. } } while (a<0); root=a/2; double root_old; do { root_old=root; root=(root_old+a/root_old)/2; } while (Math.abs(root_old-root)>1.0E-6); System.out.println("The square root of " + a + " is " + root); }

21 Computes the square root of an inputted number using a simple algorithm Computes the square root of an inputted number using a simple algorithm Same control structure as in C Same control structure as in C  Note the use of indentation to indicate control In Java, keyboard input is not straightforward In Java, keyboard input is not straightforward  Done by the readDouble() method in class Console

22 Strings Strings are sequences of characters as in C Strings are sequences of characters as in C The standard Java library has a predefined class String The standard Java library has a predefined class String Strings are immutable (unlike in C) – individual characters in the string cannot be changed Strings are immutable (unlike in C) – individual characters in the string cannot be changed String name = “Mike”; name[0] = ‘m’;// Not allowed!

23 Strings can be concatenated using the “+” operator Strings can be concatenated using the “+” operator In Java, every object, even literals, can be automatically converted to a string In Java, every object, even literals, can be automatically converted to a string String name1 = “Mike”; String name2 = “Spann”; String myName=name1+name2; String postcode = “B”+15+” “+2+”TT”;

24 The println(.) function makes use of string concatentation The println(.) function makes use of string concatentation This works with any data type This works with any data type int age = 25; System.out.println(“I am ” + age + “ years old!”); final double pi = 3.14159; System.out.println(“The value of PI = ” + pi);

25 Other string facilities A substring(.) method is provided to access a substring of a larger string A substring(.) method is provided to access a substring of a larger string A charAt(int n) method returns the character at position n in the string A charAt(int n) method returns the character at position n in the string String java=“Java”; String s = java.substring(0,3);// Jav String java=“Java”; char c= java.charAt(2)// v

26 An equals(.) method tests for string equality An equals(.) method tests for string equality The == operator should not be used – it tests to see if the strings are stored in the same location! The == operator should not be used – it tests to see if the strings are stored in the same location! int length() returns the length of the string int length() returns the length of the string There are more than 50 methods in the Java String class! (java.lang.String) There are more than 50 methods in the Java String class! (java.lang.String) if (s.equals(“Hello”)) {…}

27 Arrays Arrays created with the new operator Arrays created with the new operator Arrays can be created and initialized as in C Arrays can be created and initialized as in C The array length can be determined using name.length The array length can be determined using name.length int[] intArray = new int[20]; // 20 int array int[] evenNumbers = {2,4,6,8}; for (int j=0; j<evenNumbers.length; j++) System.out.println(evenNumbers[j]);

28 Array variable is effectively a pointer to an array allocated on the heap (hence arrays passed by reference) Array variable is effectively a pointer to an array allocated on the heap (hence arrays passed by reference)BUT Can’t do pointer arithmetic (as in C) Can’t do pointer arithmetic (as in C) int[] intArray = new int[20]; // creates a 20 int array intArray++;// NOT ALLOWED!

29 Multi-dimensional arrays are defined as follows : Multi-dimensional arrays are defined as follows : Its effectively a 1D array of pointers : Its effectively a 1D array of pointers : int[][] a = new int[5][4]; // 5 x 4 int array a[][] a[3] a[0] a[1] a[2] a[4] a[3][0] a[3][1] a[3][2] a[3][3]

30 Copying arrays Copying 1 array variable to another is equivalent (in C) to copying pointers Copying 1 array variable to another is equivalent (in C) to copying pointers int[] newArray = evenNumbers; evenNumbers newArray 2 4 6 8

31 The method System.arraycopy(…) should be used to copy the array contents The method System.arraycopy(…) should be used to copy the array contents  System.arraycopy(from, fromIndex, to, toIndex,n) int[] newArray = {0,0,0,0} System.arraycopy(evenNumbers,0,newArray,0,4); evenNumbers newArray 2 4 6 824 6 8

32 Class java.util.Arrays has a number of convenience utility functions for arrays Class java.util.Arrays has a number of convenience utility functions for arrays  Arrays.sort(a) - sorts array a into ascending order  Arrays.fill(a,val) – fills array a with value val  Arrays.binarySearch(a, key) – searches for a value key in array a  Arrays.equals(a1,a2) – test for equivalence of arrays a1 and a2

33 And finally… Basic Java programming is less error prone than C Basic Java programming is less error prone than C  No pointers to worry about  There is a genuine boolean type We have yet to think about object oriented concepts We have yet to think about object oriented concepts  Classes are the subject of the next lecture

34 Introduction to the Java lab All the Java programming assignments for this semester will be available on the course web site and Canvas All the Java programming assignments for this semester will be available on the course web site and Canvas http://www.eee.bham.ac.uk/spannm/Courses/ee2e.htm http://www.eee.bham.ac.uk/spannm/Courses/ee2e.htm http://www.eee.bham.ac.uk/spannm/Courses/ee2e.htm Lab structure Lab structure  Semester 1 (weeks 4-6, 9-10) Tuesday 2-5pm N122  Lab intro. (1 week), non-assessed  Classes (2 weeks), assessed  Inheritance (2 weeks), assessed  Semester 2  Major programming assignment, assessed

35 Organisation of the lab Organisation of the lab  You will work in pairs  The programming assignments cover material already done in lectures  Please carry out the preparatory work before the lab with your partner  You will need to put in some time outside the lab slots to finish each exercise

36 Assessment : Assessment :  Makes up 85% of the 2E1 mark  There will be 2 programming assignments this semester  Assessed by submission of code + program outputs per lab group  More details will follow and submission will be at the end of the semester  There will be 1 major programming assignment next semester  Assessed by a formal lab report per lab group


Download ppt "About this course? EE2E1 (MS) EE2E1 (MS)  Introduction to Java programming  Basic ideas about objects and classes  We will also look at more advanced."

Similar presentations


Ads by Google