Programming paradigms

Download Programming paradigms

Post on 19-Dec-2014




0 download

Embed Size (px)




<ul><li> 1. A.V.C.COLLEGE OF ENGINEERING MANNAMPANDAL, MAYILADUTHURAI-609 305 COURSE MATERIAL FOR THE SUBJECT OF PROGRAMMING PARADIGMS SUB NAME : CS2305 PROGRAMMINGPARADIGMS SEM : VI DEPARTMENT : COMPUTER SCIENCE AND ENGINEERING ACADEMIC YEAR : 2013-2013 NAME OF THE FACULTY : PARVATHI.M DESIGNATION : Asst.Professor 1 </li></ul><p> 2. A.V.C College of Engineering Department of Computer Science &amp; Engineering 2013 Odd Semester SYLLABUS CS2305 PROGRAMMING PARADIGMS L T P C 3 0 0 3 AIM: To understand the concepts of object-oriented, event driven, and concurrent programming paradigms and develop skills in using these paradigms using Java. UNIT I OBJECT-ORIENTED PROGRAMMING FUNDAMENTALS 9 Review of OOP - Objects and classes in Java defining classes methods - access specifiers static members constructors finalize method Arrays Strings - Packages JavaDoc comments UNIT II OBJECT-ORIENTED PROGRAMMING INHERITANCE 10 Inheritance class hierarchy polymorphism dynamic binding final keyword abstract classes the Object class Reflection interfaces object cloning inner classes proxies UNIT III EVENT-DRIVEN PROGRAMMING 10 Graphics programming Frame Components working with 2D shapes Using color, fonts, and images - Basics of event handling event handlers adapter classes actions mouse events AWT event hierarchy introduction to Swing Model-View- Controller design pattern buttons layout management Swing Components UNIT IV GENERIC PROGRAMMING 8 Motivation for generic programming generic classes generic methods generic code 2 3. and virtual machine inheritance and generics reflection and generics exceptions exception hierarchy throwing and catching exceptions Stack Trace Elements - assertions logging UNIT V CONCURRENT PROGRAMMING 8 Multi-threaded programming interrupting threads thread states thread properties thread synchronization thread-safe Collections Executors synchronizers threads and event-driven programming TOTAL=45 PERIODS TEXT BOOK: 1. Cay S. Horstmann and Gary Cornell, Core Java: Volume I Fundamentals, Eighth Edition, Sun Microsystems Press, 2008. REFERENCES: 1. K. Arnold and J. Gosling, The JAVA programming language, Third edition, Pearson Education, 2000. 2. Timothy Budd, Understanding Object-oriented programming with Java, Updated Edition, Pearson Education, 2000. 3. C. Thomas Wu, An introduction to Object-oriented programming with Java, Fourth Edition, Tata McGraw-Hill Publishing company Ltd., 2006. 3 4. CS2305 PROGRAMMING PARADIGMSnit No Title I Object-oriented Programming Fundamentals II Object-oriented Programming Inheritance III Event-Driven Programming IV Generic Programming V Concurrent Programming 4 5. CS2305 PROGRAMMING PARADIGMS UNIT I OBJECT-ORIENTED PROGRAMMING FUNDAMENTALS 1) Review of OOP Concepts a) Encapsulation b) Inheritance c) Polymorphism 2) Objects and Classes in Java 3) Language Basics a) Comments b) Data Types c) Variables d) Statements e) Functions 4) Defining Classes 5) Methods 6) Access Specifiers 7) Static Members 8) Constructors a) Parameterized Constructors b) this Keyword c) Garbage Collection 9) Finalize Method 10) Arrays a) One Dimensional Arrays b) Multi Dimensional Arrays 11) Strings 12) Packages 13) JavaDOC Comments 14) References 1. Review OOP concepts The object oriented paradigm is built on the foundation laid by the structured programming concepts. The fundamental change in OOP is that a program is designed around the data being operated upon rather upon the operations themselves. Data and its functions are encapsulated into a single entity. OOP facilitates creating reusable code that can eventually save a lot of work. Polymorphism permits to create multiple definitions for functions. Inheritance permits to derive new classes from old ones. Benefits of object oriented programming Data security is enforced. Inheritance saves time. User defined data types can be easily constructed. A.V.C College of Engineering 6. CS2305 PROGRAMMING PARADIGMS Inheritance emphasizes inventions of new data types. Large complexity in the software development can be easily managed. Java History Java is a general-purpose object oriented programming language developed by Sun Microsystems of USA in 1991. Originally called oak by James Gosling, one of the inventors if the language. This goal had a strong impact on the development team to make the language simple, portable, highly reliable and powerful language. Java also adds some new features. While C++ is a superset of C. Java is neither a superset nor a subset of C or C++. C++ C Java Basic concepts of Object oriented programming All Java programs are object oriented. Computer programs consist of two elements: code and data. In process oriented model code acting on data. Procedural languages such as C employ this model. To manage increasing complexity object-oriented Programming approach was conceived. Object-oriented programming organizes a program around its data (that is, objects) and a set of well-defined interfaces to that data. An object-oriented program can be characterized as data controlling access to code. Abstraction An essential element of object-oriented programming is abstraction. Abstraction refers to the act of representing essential features without including the background details or explanations. For example, people do not think of a car as a set of tens of thousands of individual parts. They think of it as a well-defined object with its own unique behavior. This abstraction allows people to use a car to drive to the grocery store without being overwhelmed by the complexity of the parts that form the car. They can ignore the details of how the engine, transmission, and braking systems work. Instead they are free to utilize the object as a whole. The Three OOP Principles All object-oriented programming languages provide mechanisms to implement the object-oriented model. They are Encapsulation Inheritance Polymorphism A.V.C College of Engineering 7. CS2305 PROGRAMMING PARADIGMS a) Encapsulation Encapsulation is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse. For example, shifting gears does not turn on the headlights in car, because it encapsulates the information about all process. In Java the basis of encapsulation is the class. A class defines the structure and behavior (data and code) that will be shared by a set of objects. For this reason, objects are sometimes referred to as instances of a class. Thus, a class is a logical construct and an object has physical reality. b) Inheritance Inheritance is the process by which one object acquires the properties of another object. This is important because it supports the concept of hierarchical classification. For example Bird Attributes: Feathers Lay eggs Flying bird Attributes: ----------- ---------- Non flying bird Attributes: ----------- ----------- The bird 'robin ' is a part of the class 'flying bird' which is again a part of the class 'bird'. The concept of inheritance provides the idea of reusability. c) Polymorphism Polymorphism means the ability to take more than one form. Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class. Shape Draw() Circle Object Draw() Box Object Draw() Triangle Object Draw() 8. 2. Class and Object Class defines a new data type. Once defined, this new type can be used to create objects of that type. Thus, a class is a template for an object, and an object is an instance of a class. Class A class is a blueprint or prototype from which objects are created. The entire set of data and code of an object can be made of a user defined data type with the help of a class. We can create any number of objects for the same class. A.V.C College of Engineering 9. CS2305 PROGRAMMING PARADIGMS For example the construction of building requires a plan. Without plan we can't construct building. Here plan acts as class. Building acts as object. Using the same plan we can build any number of buildings. In the same way we can create any number of objects for the same class. Class tells the objects state and behavior. Basically class tells what the object should have. Object Object is an instance of class. Object is real world entity which has state, identity and behavior. It may represent a person, a place, a bank account, a table of data or any item that the program has to handle. JAVA Program structure A file containing Java source code is considered a compilation unit. Such a compilation unit contains a set of classes and, optionally, a package definition to group related classes together. Classes contain data and method members that specify the state and behavior of the objects in your program. Java programs come in two flavors: 1. Standalone applications that have no initial context such as a pre-existing main window 2. Applets for WWW programming Execution begins when you launch the java virtual machine and tell it to find a particular class. After finding the class, java executes the enclosed main() method with signature: public static void main(String[] args) { ... } Program Execution Create a source code file using a text editor. The source code is then compiled using the java compiler javac. Each java program is converted into one or more class files. The content of the class file is a set of instructions called bytecode to be executed by Java Virtual Machine (JVM).Java introduces bytecode to create platform independent program. JVM is an interpreter for bytecode which accepts java bytecode and produces result. Java Source Code Java Compiler (Javac) Java Byte Code (.class) Java Virtual Machine Java program Output A.V.C College of Engineering 10. CS2305 PROGRAMMING PARADIGMS 3. Language Basics a) Comments Java comments make your code easy to understand, modify and use. Java supports three different types of comment styles. 1.Everything between initial slash asterisk and ending asterisk-slash is ignored by the java compiler.(/**/) 2.Double slash (//)mark is ignored by java compiler. 3.Everything between initial slash asterisk - asterisk and ending asterisk-slash is ignored by the java compiler and another program called JAVADOC.EXE that ships with the JDK uses these comments to construct HTML documentation files that describe your packages, classes and methods as well as all the variables used. (/***/) b) Data Types Java defines eight simple (or elemental) types of data: byte, short, int, long, char, float, double, and boolean. These can be put in four groups: Integers - This group includes byte, short, int, and long, which are for whole valued signed numbers. Floating -point numbers- This group includes float and double, which represent numbers with fractional precision. Characters - This group includes char, which represents symbols in a character set, like letters and numbers. Boolean - This group includes boolean, which is a special type for representing true/false values. c) Variables Variables are locations in memory in which values can be stored. They have a name, a type, and a value. Before you can use a variable, you have to declare it. After it is declared, you can then assign values to it. Java actually has three kinds of variables: instance variables, class variables, and local variables. Instance variables are used to define attributes or the state for a particular object. 11. Class v ariables are similar to instance variables, except their values apply to all that classs instances (and to the class itself) rather than having different values for each object. Local variables are declared and used inside method definitions, for example, for index counters in loops, as temporary variables, or to hold values that you need only inside the method definition itself. Although all three kinds of variables are declared in much the same ways, class and instance variables are accessed and assigned in slightly different ways from local variables. Scope A.V.C College of Engineering 12. CS2305 PROGRAMMING PARADIGMS Java has no global variables. All variables must be declared within either a class scope or a function scope. Declaring Variables For variable declarations, Java syntax is similar to C and C++ syntax. For native types, the syntax may be summarized as follows: One word type name. A list of variable names, separated by commas. An optional initializer following each variable name. Terminated by semicolon. The following are examples of variable declarations: This line of Java code declares a 32-bit integer variable named i: int i; This line declares two 32-bit integer variables named i and j: int i , j; This line declares i and j as above and initializes them: int i=3, j=4; d) Statements Java, like C, is an expression-based language. Expressions must always appear in the context of statements. A statement consists of an expression followed by a terminating semicolon (;). Statements can be made up of zero or more expressions, provided that their combination makes syntactic sense. Expressions (except for the return value of void methods) have a value and a type. A terminating semicolon (e.g., 27;) turns it into a legal statement even though it does not do anything useful. A simple statement consists of an expression and a semicolon. The semicolon is a required terminator. 4. Defining Class The class is at the core of Java. It is the logical construct upon which the entire Java language is built because it defines the shape and nature of an object. As such, the class forms the basis for object-oriented programming in Java. The entire set of data and code of an object can be made of a user defined data type with the help of a class. Thus, a class is a template for an object, and an object is an instance of a class. The General Form of a Class The class contains data and the code that operates on that data. A class is declared by use of the class keyword. The data, or variables, defined within a class are called instance variables. The code is contained within methods. Collectively, the methods and variables defined within a class are called members of the class. The general form of a class definition is shown here: class classname { type instance-variable1; type instance-variable2; A.V.C College of Engineering 13. CS2305 PROGRAMMING PARADIGMS // ... type instance-variableN; type methodname1(parameter-list){ // body of method } type methodname2(parameter-list) { // body of method } // ... type methodnameN(parameter-list) { // body of method } } Here is a class called Box that defines three instance variables: width, height, and depth. Currently, Box does not contain any methods (but some will be added soon). class Box { double width; double height; double depth; } Instantiating a class Class declaration only creates a template; it does not create an actual object. To create a Box object, you will use a statement like th...</p>