1.39k likes | 1.4k Views
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++)
E N D
IT 240Programming 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++) • OO Design (UML) • Advanced Topics (Design Patterns)
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)
Key OOP Concepts • Object, Class • Instantiation, Constructors • Encapsulation • Inheritance and Subclasses • Abstraction • Reuse • Polymorphism, Dynamic Binding
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
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
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
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)
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
Abstraction • OOP is about abstraction • Encapsulation and Inheritance are examples of abstraction • What does the verb “abstract” mean?
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
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
Dynamic Binding The capability of an implementation to distinguish between the different forms during run-time
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
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
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)
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)
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
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
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
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
The Paradigm Change • From a structured collection of functions • procedural programming • To a collection of interacting objects • object-oriented programming
Procedural Programming and the Structure Chart main() scanf() compute() print_results() printf()
Procedural Programming and DFDs Inventory Management Accept and Post Delivery Delivery info Transaction Item Master
OO Counterpart:Object Interaction new (delivery info) Encoder :Transaction post (item count) :Item Master
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
Hello.java Application println() System.out object
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
HelloAgain Applet:Another Scenario USER BROWSER 1: Move the browser window 2: paint() g: Graphics object 3: drawString() HelloAgain Applet
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
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
Language Differences • Compilation and Execution • Data Types and Operators • Variables • Others
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
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
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
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)
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
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
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
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
Variables and Values • Primitive type variables int x; … x = 5; X X 5
Variables and References • Reference type variables Button x; … x = new Button(“copy”); X Button Object “copy” X
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
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
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
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
#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)
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