Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced Programming Rabie A. Ramadan vpro/ Lecture 1.

Similar presentations


Presentation on theme: "Advanced Programming Rabie A. Ramadan vpro/ Lecture 1."— Presentation transcript:

1 Advanced Programming Rabie A. Ramadan vpro/ Lecture 1

2 2 Welcome Back

3 Class Organization 3 Attendance is very important Assignments Projects Quizzes

4 Textbooks 4

5 Topics to be Covered 5 Object Serialization Advanced I/O - New I/O Reflection Advanced JDBC Networking with Sockets Remote Method Invocation Keys, Signatures, and Certificates Java Authentication and Authorization Service (JAAS) Parsing XML with Java - JAXP Java Design Patterns Effective Java topics

6 Class Format 6 Some presentations by myself Presentations by you Report discussion biweekly Labs

7 Grading 7 No Midterm – Midterm grades will be added towards the project Labs Assignments

8 Agenda 8 Introduction and Course Motivation Project Assignment 1 – Get your hands dirty

9 Introduction and Course Motivation 9 Computer Programming is the art of making a computer do what you want it to do.

10 Terminology API – Application Programming Interface Classes, Interfaces, Constructors, Members, and Serialized Forms By Which a Programmer Accesses a Class, Interface, or Package User A Programmer Who Writes a Program That Uses an API Client A Class Whose Implementation Uses an API

11 Key Principles For Using Any Language Focus on Clarity and Simplicity Don’t Surprise Users of an API Hide Complexity Underneath, Not In, The API Reuse Rather Than Copy Minimize Dependencies Between Modules Some Early Habits Need to Be Broken Detect Errors As Soon As Possible Compile-Time Detection Beats Run-Time Failure Run-Time Exceptions Beat Undefined Behavior

12 Performance Bloch Does not Focus on Performance Instead, Strive to Write Programs that are: Clear, Correct, Robust, Flexible, Maintainable Start With Sound Software Engineering Get Performance Later Where You Need It Excessive Performance Focus Has Real Costs Think About Why a Language Like C++ is so Complex

13 Who are You ? 13 Just a coder Most of us are coders not programmers Programmer Writes an efficient code

14 Project 14 Project

15 Some Salient Characteristics of Java Appendix A: Introduction to Java 15 Java is platform independent: the same program can run on any correctly implemented Java system Java is object-oriented: Structured in terms of classes, which group data with operations on that data Can construct new classes by extending existing ones Java designed as A core language plus A rich collection of commonly available packages Java can be embedded in Web pages

16 Java Processing and Execution Appendix A: Introduction to Java 16 Begin with Java source code in text files: Model.java A Java source code compiler produces Java byte code Outputs one file per class: Model.class May be standalone or part of an IDE A Java Virtual Machine loads and executes class files May compile them to native code (e.g., x86) internally

17 Compiling and Executing a Java Program Appendix A: Introduction to Java 17

18 Classes and Objects Appendix A: Introduction to Java 18 The class is the unit of programming A Java program is a collection of classes Each class definition (usually) in its own.java file The file name must match the class name A class describes objects (instances) Describes their common characteristics: is a blueprint Thus all the instances have these same characteristics These characteristics are: Data fields for each object Methods (operations) that do work on the objects

19 Grouping Classes: The Java API Appendix A: Introduction to Java 19 API = Application Programming Interface Java = small core + extensive collection of packages A package consists of some related Java classes: Swing: a GUI (graphical user interface) package AWT: Application Window Toolkit (more GUI) util: utility data structures The import statement tells the compiler to make available classes and methods of another package A main method indicates where to begin executing a class (if it is designed to be run as a program)

20 A Little Example of import and main Appendix A: Introduction to Java 20 import javax.swing.*; // all classes from javax.swing public class HelloWorld { // starts a class public static void main (String[] args) { // starts a main method // in: array of String; out: none (void) } public = can be seen from any package static = not “part of” an object

21 Processing and Running HelloWorld Appendix A: Introduction to Java 21 javac HelloWorld.java Produces HelloWorld.class (byte code) java HelloWorld Starts the JVM and runs the main method

22 References and Primitive Data Types Appendix A: Introduction to Java 22 Java distinguishes two kinds of entities Primitive types Objects Primitive-type data is stored in primitive- type variables Reference variables store the address of an object

23 Primitive Data Types Appendix A: Introduction to Java 23 Represent numbers, characters, boolean values Integers: byte, short, int, and long Real numbers: float and double Characters: char

24 Primitive Data Types Data typeRange of values byte (8 bits) short -32, ,767 (16 bits) int -2,147,483, ,147,483,647 (32 bits) long -9,223,372,036,854,775, (64 bits) float +/ to +/ and 0, about 6 digits precision double +/ to +/ and 0, about 15 digits precision char Unicode characters (generally 16 bits per char) boolean True or false Appendix A: Introduction to Java 24

25 Operators Appendix A: Introduction to Java subscript [ ], call ( ), member access. 2. pre/post-increment ++ --, boolean complement !, bitwise complement ~, unary + -, type cast (type), object creation new 3. * / % 4. binary + - ( + also concatenates strings) 5. signed shift >, unsigned shift >>> 6. comparison >=, class test instanceof 7. equality comparison == != 8. bitwise and & 9. bitwise or |

26 Operators Appendix A: Introduction to Java logical (sequential) and && 12. logical (sequential) or || 13. conditional cond ? true-expr : false-expr 14. assignment =, compound assignment += -= *= /= >= >>>= &= |=

27 Type Compatibility and Conversion Appendix A: Introduction to Java 27 Widening conversion: In operations on mixed-type operands, the numeric type of the smaller range is converted to the numeric type of the larger range In an assignment, a numeric type of smaller range can be assigned to a numeric type of larger range byte to short to int to long int kind to float to double

28 Declaring and Setting Variables Appendix A: Introduction to Java 28 int square; square = n * n; double cube = n * (double)square; Can generally declare local variables where they are initialized All variables get a safe initial value anyway (zero/null)

29 Java Control Statements Appendix A: Introduction to Java 29 A group of statements executed in order is written { stmt1; stmt2;...; stmtN; } The statements execute in the order 1, 2,..., N Control statements alter this sequential flow of execution

30 Java Control Statements (continued) Appendix A: Introduction to Java 30

31 Java Control Statements (continued) Appendix A: Introduction to Java 31

32 Methods Appendix A: Introduction to Java 32 A Java method defines a group of statements as performing a particular operation static indicates a static or class method A method that is not static is an instance method All method arguments are call-by-value Primitive type: value is passed to the method Method may modify local copy but will not affect caller’s value Object reference: address of object is passed Change to reference variable does not affect caller But operations can affect the object, visible to caller

33 The String Class Appendix A: Introduction to Java 33 The String class defines a data type that is used to store a sequence of characters You cannot modify a String object If you attempt to do so, Java will create a new object that contains the modified character sequence

34 Comparing Objects Appendix A: Introduction to Java 34 You can’t use the relational or equality operators to compare the values stored in strings (or other objects) (You will compare the pointers, not the objects!)

35 The StringBuffer Class Appendix A: Introduction to Java 35 Stores character sequences Unlike a String object, you can change the contents of a StringBuffer object

36 StringTokenizer Class Appendix A: Introduction to Java 36 We often need to process individual pieces, or tokens, of a String

37 Arrays Appendix A: Introduction to Java 37 In Java, an array is also an object The elements are indexes and are referenced using the form arrayvar[subscript]

38 Array Example Appendix A: Introduction to Java 38 float grades[] = new float[numStudents];... grades[student] = something;... float total = 0.0; for (int i = 0; i < grades.length; ++i) { total += grades[i]; } System.out.printf(“Average = %6.2f%n”, total / numStudents);

39 Array Example Variations Appendix A: Introduction to Java 39 // possibly more efficient for (int i = grades.length; --i >= 0; ) { total += grades[i]; } // uses Java 5.0 “for each” looping for (float grade : grades) { total += grade; }

40 Input/Output using Streams Appendix A: Introduction to Java 40 An InputStream is a sequence of characters representing program input data An OutputStream is a sequence of characters representing program output The console keyboard stream is System.in The console window is associated with System.out

41 Opening and Using Files: Reading Input Appendix A: Introduction to Java 41 import java.io.*; public static void main (String[] args) { // open an input stream (**exceptions!) BufferedReader rdr = new BufferedReader( new FileReader(args[0])); // read a line of input String line = rdr.readLine(); // see if at end of file if (line == null) {... }


Download ppt "Advanced Programming Rabie A. Ramadan vpro/ Lecture 1."

Similar presentations


Ads by Google