1 / 53

Object Oriented Programming

Object Oriented Programming. Lecture 2: Object Oriented Design + Variables and Basics Mustafa Emre İlal emreilal@iyte.edu.tr. Recap. Architects and programming A Program – Programming languages Java Portable Purely object oriented Comprehensive library. Today.

guy
Download Presentation

Object Oriented Programming

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. Object Oriented Programming Lecture 2: Object Oriented Design + Variables and Basics Mustafa Emre İlal emreilal@iyte.edu.tr

  2. Recap • Architects and programming • A Program – Programming languages • Java • Portable • Purely object oriented • Comprehensive library

  3. Today • 1. Half: Object Oriented Design • Software Engineering • 2. Half: Variables and basics • Your first program in Java

  4. 1: Object Oriented Design • Too many new concepts • No examples • Do not expect to grasp all immediately • You should ask questions • Thinking in Java – First half of Chapter 1 • www.omg.org/uml

  5. Programming • Problem solving • Architecture = Programming? • Constrained by mathematical models

  6. Modeling • Design (mathematical) • Description • Architectural problem • Mass models • Working models • Presentation models • Software problem • Structural models (data) • Functional models (behavior) Input data CPU Memory Output result

  7. UML • Unified Modeling Language • Booch, Rumbaugh and Jacobson • Common “language” for describing object oriented systems • Representation is mostly through visual diagrams

  8. UML • Necessary for large, complex systems • Documents entire software development process • Use cases: help formalize requirements • Interaction diagrams: "sequence diagram", "collaboration diagrams” document system behavior • Class diagrams: provide specification for data structure

  9. Use Cases • Specific scenarios

  10. Interaction • Sequence Diagram • Collaboration Diagram

  11. Class Diagrams • Data Structures

  12. Objects and Classes • Data in programming languages • Primitive types (e.g. integers) • Arrays or collections • Objects (agents) • Include both Data + Behavior = “Encapsulation” • All belong to some “Class” (classification) • Classes are templates for objects • Each object is an “Instance” of the Class it belongs to • An object can include other objects

  13. Object – TV • Data (Properties ) • Screen size • Weight • Number of channels • Contrast setting • Brightness setting • Channel currently being viewed • The existence of the required voltage

  14. Object – TV • Behavior (methods / functions) • Turning it on/off • Remote commander • Numeric pad • Channel switcher • Sound/Color/Setting dial • Menu • Each button press is a “message” from the user to the object.

  15. Messaging • Every message • Activates a process that will prepare a suitable reply to the message. (method invocation) • Needs to include the information necessary to complete the requested action. (parameters/arguments) • Is answered with a proper reply (the return value) unless it requests none (a void reply)

  16. Objects TV UML: • Encapsulation • Properties • Methods • Interface • Information Hiding screenSize weight noChannels contrastSetting brightnessSetting currentChannel suppliedVoltage isTurnedOn turnOn() changeChannel(newChannel) lowerVolume() increaseVolume() mute() lowerContrast() increaseContrast() showMenu()

  17. Information Hiding • The object interface is a “contract” between the developer of the class and the other developers • There needs to be a way to be able to make changes to the class without breaking contract. • Java keywords for access: • public • protected • private • “default" or “friendly”– when nothing is specified

  18. Reuse • Once a class is designed and developed, it should be used in many different projects. • Reuse: • Association, aggregation, composition • Inheritance • Libraries, APIs

  19. Object Composition • Association (has-a relationship) • Aggregation (part-of relationship) Structural System Site Column Beam Slab

  20. Inheritance • superclass – subclass • Java: "extends" keyword (“class A extends B") Structural System Site StrSysElement Column Beam Slab

  21. Inheritance • Also called “generalization” • Java allows only a single superclass • Subclass automatically includes all properties and methods of the superclass except private ones. • Methods can be rewritten in which case the behavior of subclass is altered. (“overriding”) • Basis of “Polymorphism”

  22. Even more confusion • Abstract classes • Classes that describe concepts rather than concrete objects. • Interfaces • Specifications (list of methods) for objects in order to be considered for certain tasks. • A class can adhere to multiple interfaces. • Class variables and methods • Properties and methods that are not for any objects (the instances of a class) but rather accessed centrally through the class definition itself. They are specified by the "static” keyword.

  23. 2: Variables and Basics • Variables • Primitive types • Classes and objects • Your first program • Basics • Types • Operators • Thinking in Java – Chapter 2 - 3

  24. Variables • Primitive Types • Arrays • Objects

  25. Primitive Types • Java’s primitives • byte : -128 to 127 (8 bits) • short : -32,768 to 32,767 (16 bits) • int : -2,147,483,648 to 2,147,483,647 (32 bits) • long :-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (64 bits) • float : 1.40129846432481707e-45 to 3.40282346638528860e+38 (positive or negative) (32 bits) • double : 4.94065645841246544e-324d to 1.79769313486231570e+308d (positive or negative) (64 bits) • boolean : true/false (1 bit) • char : Unicode 0 to 65,535 (16 bits)

  26. byte a = 17; 1 0 0 1 0 0 0 1 +/- 26 25 24 23 22 21 20 float b = 34.56; c 17 Object c = new Object(); 34.56 a b c References Primitives References (pointers)

  27. a b a b 3 6 6 6 6 7 a b Primitives int a = 3; int b = 6; a = b; b = 7;

  28. x y Object Object References Object x; Object y; x = new Object(); y = x; // we do not get two Objects y = x.clone();

  29. Arrays • Array • A collection of a known number of variables of the same type. • In Java, it is a reference type – an object

  30. Scope of Variables • { } curly brackets create scope • Primitives are erased from memory once the process leaves their scope • Objects are not affected by scope. They are erased only when there are no more referances pointing to them

  31. Scope { int x; //definition { int q = 96; //definition x = q; // OK } x = q; // error! – ‘q’ is out of scope }

  32. Classes and Objects • Class is the definition for a new type • Instances of the class, objects, are created (instantiated) with the “new” operator. • Access to properties and methods of an object is by placing a ‘.’ character between the object reference and the property or method name. anObject.anIntVariable = 5;anObject.aMethod();anObject.anotherObject.aMethod(); • “object oriented”

  33. static • Properties and methods that are only for the Class and will not be a part of the instances (objects) are defined with the keyword “static” • Two common uses • Variables for keeping track of all objects belonging to a class. • Methods that need to be called without any objects.

  34. Packages • “File system” to avoid naming conflicts between source codes from different developers. • In order to use a class in your code, you need to specify the package where it belongs by preceeding the class name with the package name. java.util.ArrayList a = new java.util.ArrayList(); • import statement provides a shorthand for the source code.import java.util.ArrayList;...ArrayList a = new ArrayList(); • import java.lang.*; statement above is implied for every java program and need not be included.

  35. Your First Program • The famous "Hello World" • Java is case-sensitive • public static void main (String[] args)is the entry point for the program • “javac.exe fileName.java” (include the suffix)> javac HelloWorld.java • “java.exe classToRun” (class name that includes the main() method without suffix )> java HelloWorld

  36. Operators

  37. Casting Operator • Java is "strongly-typed" • Transfer of value between two variables of different types is a “cast” • As long as there is no loss of information, Java will automatically execute the statement. (widening) • If a loss of data is possible, the programmer needs to take responsibility and force the “cast”(narrowing) int a = 5; float b = 3.56; b = a; // automatic a = b; //error. Will not compile a = (int)b; // "cast" – programmer’s initiative

  38. Object Cast • Same thing for Objects • Superclass – subclass relationships • An object can automatically be treated as an instance of its superclass (generalization) • A “cast” operator is required if an object is to be used as an instance of its subclass (specialization)

  39. Arithmetic operators • General rules apply + : a + b : addition - : a – b : subtraction * : a * b : multiplication / : a / b : division % : a%b : modulus (remainder after a/b) • <left operand> <operator> <right operand> result is the return value

  40. Assignment operator • "=" • DIFFERENT from math:a = a + 5; first, the right side is resolved, then is assigned to the left side. • IMPORTANT: it does not mean “Are they equal?”

  41. Shorthand for increment/decrement • ++a; a = a+1; equivalent (shorthand notation) • ++counter; use the value after incrementing • --counter ; use the value after decrementing • counter++; use the value first, then increment • counter--; use the value first, then decrement

  42. Comparison operators • The operators below compare the right operand with the left operand and return a boolean value (trueor false) • > : a >b; is a greater than b? • >= : a>=b; is a greater than or equal to b? • < : a<b; is a less than b? • <= :a<=b; is a lass than or equal to b?? • == : a==b; is a equal to b? (note the double equals) • != : a!=b; is a not equal to b? • IMPORTANT: You should not compare objects with these operators.

  43. Conditional operators • Conditional operators return a boolean result depending on the evaluation of the two boolean components left and right. • && : a && b; return true if both a and b are true. • || : a || b; return true if a or b is true. • ! : !a; returns the opposite of a (“not a”) • & : a & b; same as ‘&&’ operator except that it forces both components to be evaluated even if the first term is false. • | : a | b; same as ‘||’ operator except that it forces both components to be evaluated even if a is true.

  44. Promotion Rule • If an operator is used on two values which are of different types, the ‘smaller’ of the two types is promoted to the ‘larger’ type and then the evaluation is performed. For example, 3.1415 * 7 • 3.1415 is a float and 7 is an int. Since int is ‘smaller’, it is converted to a float 7.0 and then the numbers are multiplied.

  45. Operator Precedence • Order of evaluation • ( ) • ++, -- • * / % • + - • < <= > >= • == !=

  46. Unexpected Results • Computers follow the rules of type conversion and operator precedence strictly in evaluating expressions. • Best to use parenthesis to clarify the order of evaluation. • float a = 3 / 2; what is the value of a? • 1 (not 1.5!) • float a = (float)3/(float)2; • float a = (float)3/2; // automatic promotion • float a = 3.0/2.0; // wise

  47. Wrapper Classes • For each of the primitive data types, Java also provides a useful ‘wrapper’ Class in java.lang package • int : Integer • long : Long • float : Float • double : Double • char : Character • boolean : Boolean • The wrapper classes provide a bridge between primitive data types and Objects

  48. java.lang.Math • The Math Class provides some basic mathematical functions. Examples: • static double sqrt(double a) • static double random() • static double sin() • static double abs(double a) • static double max(double a, double b)

  49. Characters • Characters (char) were “digitized” based on the ASCII standard with a 256 character limit. • Java now uses the UNICODE standard that offers 65,536 characters for internationalization. • Numeric operators work with char types in the same manner as int types. • char c = 'A'; UNICODE value 65 • C++ ; UNICODE value 66 which is 'B'

  50. java.lang.String • Input/output using single characters is difficult • Java makes use of STRINGs, Objects that manipulate an array of characters. It comes with the API. • Some methods: • boolean compareTo(String anotherString) • String subString(int beginIndex, int endIndex) • int length() • String concat(String str) • Some useful shortcuts for Strings: • Quotes instantiate a new String objectString s = “Message"; • Addition operator concatenates two Strings together String s = s + "lar uzatılabilir";

More Related