1 / 139

IT 240 Programming Paradigms

IT 240 Programming Paradigms. MS Information Technology Offshore Program Ateneo de Davao Session 3. Course Outline Revisited. Programming Language Concepts Survey of Languages and Paradigms Imperative, Functional, Object-Oriented Focus: OO Programming OO Languages (Java and C++)

jhamm
Download Presentation

IT 240 Programming Paradigms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. IT 240Programming Paradigms MS Information Technology Offshore Program Ateneo de Davao Session 3

  2. Course Outline Revisited • Programming Language Concepts • Survey of Languages and Paradigms • Imperative, Functional, Object-Oriented • Focus: OO Programming • OO Languages (Java and C++) • OO Design (UML) • Advanced Topics (Design Patterns)

  3. Schedule This Weekend • Friday Evenin • OOP Concepts • Saturday Morning • OOP Languages: More Java • Saturday Afternoon • OOP Languages: C++, contrast with Java • Intro to OO Design & the Unified Modeling Language (UML)

  4. Key OOP Concepts • Object, Class • Instantiation, Constructors • Encapsulation • Inheritance and Subclasses • Abstraction • Reuse • Polymorphism, Dynamic Binding

  5. Object Definition: a thing that has identity, state, and behavior • identity: a distinguished instance of a class • state: collection of values for its variables • behavior: capability to execute methods * variables and methods are defined in a class

  6. Class Definition: a collection of data (fields/ variales) and methods that operate on that data • data/methods define the contents/capabilities of the instances (objects) of the class • a class can be viewed as a factory for objects • a class defines a recipe for its objects

  7. Instantiation • Object creation • Memory is allocated for the object’s fields as defined in the class • Initialization is specified through a constructor • a special method invoked when objects are created

  8. Encapsulation • A key OO concept: “Information Hiding” • Key points • The user of an object should have access only to those methods (or data) that are essential • Unnecessary implementation details should be hidden from the user • In Java/C++, use classes and access modifiers (public, private, protected)

  9. Inheritance • Inheritance: • programming language feature that allows for the implicit definition of variables/methods for a class through an existing class • Subclass relationship • B is a subclass of A • B inherits all definitions (variables/methods) in A

  10. Abstraction • OOP is about abstraction • Encapsulation and Inheritance are examples of abstraction • What does the verb “abstract” mean?

  11. Reuse • Inheritance encourages software reuse • Existing code need not be rewritten • Successful reuse occurs only through careful planning and design • when defining classes, anticipate future modifications and extensions

  12. Polymorphism • “Many forms” • allow several definitions under a single method name • Example: • “move” means something for a person object but means something else for a car object

  13. Dynamic Binding The capability of an implementation to distinguish between the different forms during run-time

  14. Visual and Event-driven Programming • Fits very well with the OO Paradigm • Visual Programming and GUIs • windows, icons, buttons, etc. are objects created from ready-made classes • Event-driven Programming • execution associated with user interaction with visual objects that causes the invocation of other objects’ methods

  15. What’s Next? • OO Languages • how are these concepts implemented in language platforms such as Java and C++ • implementation tradeoffs • OO Design • the success of the paradigm lies on how well classes are designed • need models and techniques (the UML) that allow for good planning and design

  16. Report Topic • Part of the requirement for this course is a report to be presented on my next visit • Topic on OO Paradigm • propose a topic by tomorrow • report on a language, an environment, or an application of OOP (e.g., files or networking) • Requirements • 15-20 minute presentation, 2-page report • hands-on demo (unless excusable)

  17. Java

  18. Computing in the 1990s The Internet and the WWW • from retrieving documents to executing remote programs • Graphical User Interfaces (GUIs) • visual and event-driven programming • Object-oriented Programming (OOP)

  19. Java Intro Summary(Last Session) • Simple Java Application • HelloWorld example • Standalone Java program • public static void main( … ) … • Simple Java Applet • HelloAgain example • Executed through browser or appletviewer • Requires .html file

  20. Lab Exercise 1:TextCopy Applet • Filename: TextCopy.java • Variables: • two TextField variables • one Button variable • Methods: • init(): creates and displays the UI objects • action(): processes UI events

  21. Invoking Methods on Objects • Syntax for method invocation object.methodName(arguments) • Example: message.setText(“Hello”); • calls setText method on a TextField object • To find out what methods are available for a given class • javap package.name.NameOfClass • ex. javap java.awt.TextField

  22. Java Program Structure • Java Class • (optional) import declarations • class declaration • Class • class name should match its file name • extends for inheritance • contains method/function definitions

  23. The Paradigm Change • From a structured collection of functions • procedural programming • To a collection of interacting objects • object-oriented programming

  24. Procedural Programming and the Structure Chart main() scanf() compute() print_results() printf()

  25. Procedural Programming and DFDs Inventory Management Accept and Post Delivery Delivery info Transaction Item Master

  26. OO Counterpart:Object Interaction new (delivery info) Encoder :Transaction post (item count) :Item Master

  27. OOP and Object Interaction • Objects pass messages to each other • An object responds to a message by executing an associated method defined in its class • Causes a “chain reaction” • The user could be viewed as an object • Depicted in an Object Interaction Diagram

  28. Hello.java Application println() System.out object

  29. HelloAgain Applet: Creation USER BROWSER 1: Open HA.html 2: new 3: paint() g: Graphics object 4: drawString() HelloAgain Applet Note: new means the object of class HelloAgain is created

  30. HelloAgain Applet:Another Scenario USER BROWSER 1: Move the browser window 2: paint() g: Graphics object 3: drawString() HelloAgain Applet

  31. TextCopy Applet:Creation USER BROWSER 1: Open the .html file 2: new 3: init() 4: new 5: setText() message: TextField object TextCopy Applet 7: new 6: new copy: Button object destination: TextField object

  32. TextCopy Applet:Clicking on the Button USER BROWSER 1: Click on button 2: action() 3: getText() message: TextField object HelloAgain Applet copy: Button object 4: setText() destination: Button object

  33. Java Versus C

  34. Language Differences • Compilation and Execution • Data Types and Operators • Variables • Others

  35. C ProgramCompilation and Execution • prog.c is compiled to prog.exe • for multiple modules, produce prog.objfirst and then link with other .obj files • prog.exe is a readily executable binary-coded program • Execution begins with the function “main()” in the C program

  36. Java ProgramCompilation and Execution • Prog.java is compiled to Prog.class • Execution • for applets, the browser loads Prog.class and UI events can then be processed • for applications, a Java interpreter loads Prog.class and causes program execution to begin in the “main()” method of this class

  37. The Java Virtual Machine • Browsers and the Java interpreters have to simulate this “standard” machine • “Compile once, run anywhere” • Class Loader • The JVM facilitates the loading and execution of classes • Several classes, not just one class, are loaded • Java class library

  38. Data Types • Most C types apply to Java • int, char, float, double • Other “primitive” types • short, long (also available in C) • byte, boolean • Main difference • In Java, the size of a data type type is strictly specified (in C, size depends on the machine)

  39. Value Ranges for theJava Data Types • boolean: true, false • size: 1 bit • Not compatible with integer types as in C • char: Unicode character set • size: 2 bytes • Superset of ASCII • Internationalization • Still compatible with integer types

  40. Sizes and Rangesfor Other Java Types • int: 4 bytes • -2,147,483,648 to 2,147,483,647 • float: 4 bytes • 1.01e-45 to 3.40e+38 • double 8 bytes • 4.94e-324 to 1.80e+308

  41. Operators • All operators in C apply • &&, ||, and, !now apply to boolean operands only • & and | as boolean operators do not perform “short-cutting” • can be distinguished from integral bitwise operations • + for String concatenation

  42. Two Kinds of Variables • Variables of a primitive type e.g., int x; char c; • Variables of a reference type (class) e.g., Button b; String s; • Conventions • Primitive types are reserved words in Java and are indicated in all-lower-case letters • Class names: first letter usually capitalized

  43. Variables and Values • Primitive type variables int x; … x = 5; X X 5

  44. Variables and References • Reference type variables Button x; … x = new Button(“copy”); X Button Object “copy” X

  45. The new Keyword • new Button(“copy”) creates a Button object and returns a reference (an address) to that object that a Button variable could hold Button Object “copy” X 1023 1023: 1023 is some address in memory

  46. The null Keyword • Use null to indicate (or test) that the variable does not currently refer to an object x = null; if (x == null) ... X null

  47. Global Variables • Java has no global variables • Scope of variables analogous to a single C source program containing several functions • Within a class, variables declared outside the methods are available to every method* • Variables declared within a method (including parameters) are local to that method

  48. Prototypes in C • In C, prototypes are sometimes required double squareroot(int); /* prototype */ … d = squareroot(5); /* used before definition */ … double squareroot(int num) … /* definition */ • In Java, the compiler reads the program at least twice to resolve usage

  49. #include vs import • In C, • #include <somefile.h> contains the macros and prototypes that enable a program to call functions defined in the standard library • In Java, • import some.package.*; enables a program to refer to existing classes (particularly those from the class library) by its simple name (Button versus java.awt.Button)

  50. Statements • All control structures follow the same syntax (if, switch, while, do-while, for) • In a compound statement or block, variable declarations and statements may intersperse • New statement for exception handling: try-catch

More Related