1 / 44

Lecture 01 The Essence of Programming Language

Lecture 01 The Essence of Programming Language. Course Structure. Fundamentals of programming Object-oriented programming Graphics programming Developing comprehensive programming. The Languages of a Computer. Analog vs. digital signal

matty
Download Presentation

Lecture 01 The Essence of Programming Language

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. Lecture 01The Essence of Programming Language

  2. Course Structure • Fundamentals of programming • Object-oriented programming • Graphics programming • Developing comprehensive programming

  3. The Languages of a Computer • Analog vs. digital signal • Analog signal: continuous wave-forms used to represent things such as sound • Digital signal: information with a sequence of 0s and 1s. • The language of a computer, called machine language, is a sequence of 0s and 1s. • The digit 0 or 1 is called a binary digit or bit • Sometimes a sequence of 0s and 1s is referred to as binary code • A sequence of eight bits is called a byte • Every letter, number, or special symbol on your keyboard is encoded as a sequence of bits, each having a unique representation

  4. The Languages of a Computer • American Standard Code for Information Interchange (ASCII) • The most commonly used encoding scheme on personal computer • Consists of 128 characters numbered 0 through 127 • E.g. the letter “A” binary representation: 1000001 (66th character in the ASCII character code, but its position is 65) • Inside the computer, every character is represented as a sequence of eight bits (a byte) • Because ASCII is a seven-bit code, you must add 0 to the left of the ASCII encoding • E.g., “A”  01000001 • Other enconding schemes: EBCDIC and Unicode • EBCDIC (used by IBM) consists of 256 characters • Unicode consists of 65,356 characters • To store a Unicode, need two bytes • Java uses the Unicode character set • The first 128 characters of Unicode are the same as the characters in ASCII

  5. Create program in Java following the rules, called source program and saved in file named xxx.java Checks for correctness of syntax and translates the program into byte code The byte code is saved in the file with the .class extension Links the bytecode of your program with the necessary code residing in the library (SDK) Load an executable program into main memory A program reads, translates each bytecode Into the machine language of your computer, and Then executes it. Processing a Java Program Editor Compiler Bytecode Bytecode Linker Loader Interpreter

  6. Problem ACE Cycle • ACE (Analysis-coding-execution) cycle • Algorithm: A step-by-step problem solving process in which a solution is arrived at in a finite amount of time • Analyze the problem, outline the problem and its solution requirements, and design an algorithm to solve the problem • Implement the algorithm in a programming language, such as Java, and verify that the algorithm works • Maintain the program by using and modifying it if the problem domain changes Problem Analysis Algorithm design Coding Compiler Error No error Interpreter Error Results

  7. 1. Analyze Problem - Review program specification - Meet with users - Identify program components 2. Design Program - Group activity into modules - Devise solution algorithms - Test solution algorithms 6. Maintain Program - Identify errors - Identify enhancements 5. Formalize Solution - Review program code - Review documentation - Implement program 3. Code Program - Translate solution algorithm - Enter program code into computer 4. Test Program - Remove any syntax error - Remove any logic error Program Development Life Cycle (PDLC)

  8. Programming Methodologies • Structured programming • Divide a problem into smaller subproblems • Each subproblem is analyzed and a solution for the subproblem is obtained • Also known as top-down design, stepwise refinement and modular programming • Object-oriented programming • Object-oriented Design (OOD) • Identify components called object • Specify the relevant data for each object and possible operations to be performed on that data • In OOD, the final program is a collection of interacting objects • A programming language that implements OOD is called an OOP language

  9. Structured Programming • Hierarchical Input Process Output (HIPO) chart • Algorithmic Thinking • Pseudocode • Flowchart • Storyboard

  10. Top-Down Design • One method that is useful when defining the procedures to be used is termed Top-Down Design. With Top-Down Design you • break down a problem down into functional tasks or parts; • break each part, or task, into sub-parts; • continue the ‘chunking-up’ process until the sub-parts, or sub-tasks, are very simple and easily described.

  11. HIPO Chart Main Initialization Process Wrap-up Declare Variable Print Report Read a record Calculate Accumulate total Print Detail Calculate average Print total And averages Calculate Gross amount Calculate discount Calculate net Amount due

  12. Algorithmic Thinking • Correct • Using logical constructs and valid data in an organized way • The steps will be carried out correctly • The program will make suitable response • Efficient • The program’s ability to deliver a results in a time short enough to be useful and in a space small enough • An algorithm can be defined in various ways. Two common ways include the use of pseudocode and flowchart

  13. uses English-like phrases to describe the instructions List the actions using keywords Depicts logical grouping or structures using indentation MAIN MODULE: Call Initialization Call Process Call Output END PROCESS MODULE: Do While not End of File Read a record Call Calculate Call Accumulate Print Detail Line End Do RETURN CALCULATE MODULE: If Hours > 40 then Call overtime Else Call Regular time End If RETURN Pseudocode

  14. Example: Paycheck • Every salesperson has a base salary. The salesperson also receives a bonus at the en of each month based on the following criteria: • If the salesperson has been with the store for five or fewer years  $10 for each year • If the salesperson has been with the store for more than five years  $20 for each year The salesperson can earn an additional bonus as follows: • If salesperson’s total sales for the month are more than $5,000 but less than $10,000  3% commission on sales • If salesperson’s total sales for the month are more than $10,000  6% commission on sales

  15. Example: Paycheck-Pseudocode • Get baseSalary • Get noOfServiceYears • Calculate bonus using the following fomula: if (noOfServiceYears is less than or equal to five) bonus = 10 * noOfServiceYears otherwise bonus = 20 * noOfServiceYears • Get totalSale • Calculate additionalBonus using the following formula: if (totalSale is less than 5000) additionalBonus = 0 otherwise if (totalSale is greater than or equal to 5000 and totalSale is less than 1000) additionalBonus = totalSale * (0.03) otherwise additionalBonus = totalSale * ( 0.06) • Calculate payCheck using the equation payCheck = baseSalary + bonus + additionalBonus

  16. Flow Chart Process Symbol Represent process Makes data available for processing (input) or Displaying (output) of process information I/O Symbol Decision symbol Represents a decision that determines which Of number of alternative paths is to be followed Connector symbol Represents any entry form, or exit to, another part of the flow chart Represents the beginning, the end, or a point of Interruption or delay in a program Terminal symbol

  17. Storyboard Interest Calculator principalLabel amtLabel Principal: principalField intLabel intField InterestRate: Amount Paid: monthsLabel amtlField Months: monthslField Calculate Clear calButton clearButton

  18. Understanding Object-Orientation • Software development • You first create a system by creating a set of classes • You can expand the system by adding capabilities to components you’ve already built or by adding new components • You can reuse the classes you created when you build a new system, cutting down substantially on system development time

  19. Object Orientation • Objects are heart of object orientation • An object is a representation of almost anything you need to model in a program Kitchen Bath Bedroom Living room Bedroom Dining Room

  20. Object Orientation • One way to think of an object is as a black box with some buttons and lights • To use the object, you need to know • which ones you need to press to get the object to do what you need • what the lights mean about the status of the object • What is important is that • The object carries out its functions and responsibilities • The object interacts with the outside world • The object can provide information about its current state

  21. Object-Oriented Languages • Several object oriented languages • Samlltalk, C++, Objective C, Object Pascal, Java • Java is the emerging object-oriented language • Popularity of WWW • Ability to run Web applets directly on any computer or operating systems

  22. Object-Oriented Design and UML • Several different object-oriented development methodologies • Heavyweight vs lightweight (called “agile”) • Different graphical notation • The emergence of the UML as the standard notation • Originated in the mid-1990s • Shows objects and object relationships

  23. The Essence of Objects Object. The basic unit of object orientation. An object is an entity that has attributes, behavior, and identity. Objects are members of a class, and the attributes and behavior of an object are defined by the class definition

  24. Objects • Typically, each object has an associated set of attributes • Such as value, state, or whatever else is needed to model • E.g. Sensor attributes • might include a state such as active or inactive • Indicate current value, and information about its physical location • An object usually provides the ability to modify their state • An object has a set of responsibilities that it carries out by providing services to other objects

  25. Class • While a programming is running, individual objects usually don’t stand alone class. A class is a description of a set of objects. The set of objects share common attributes and common behavior. A class definition describes all the attributes of member objects of that class, as well as the class methods that implement the behavior of member objects

  26. Class • A category or group of things that have similar attributes and common behaviors • E.g. • Anything in the class of washing machines has attributes such as brand name, model, serial number, and capacity • Behaviors for things in this class include the operations “add clothes,” “add detergent,” and “remove clothes.” Washing Machine brand name model name serial number capacity add clothes() add detergent() remove clothes

  27. Class • Is-a relationships • “My GE’s Front Load Washer is a Washing machine” • The concept is useful because of its reusability • Objects inherits attributes from classes

  28. Washing Machine Washing Machine brand name model name serial number capacity brand name model name serial number Capacity drum volume motor add clothes() add detergent() remove clothes add clothes() add detergent() remove clothes( ) time-the-soak() time-the-wash() Class and Object • A class serves another purpose in addition to categorization • A class is a template for making objects

  29. What is an Object-Oriented Systems? Object orientation. A method of developing software that uses abstraction with objects, encapsulated classes, communication via messages, object lifetime, class hierarchies, and polymorphism.

  30. Fundamental Properties of an Object Oriented System • Abstraction with objects • Encapsulated classes • Communication via messages • Object lifetime • Class hierarchies • Polymorphism

  31. Abstraction • Abstraction is one of the basic tools of all programming • Abstraction serves as a way to model the real-world problem • E.g. address book • Abstractions: name, addresses, phone numbers, etc.

  32. Encapsulation Encapsulation. The process of hiding all the internal details of an object from the outside world. In Java, encapsulation is enforced by having the definitions for attributes, and methods inside a class definition. • Encapsulation is one of the most important aspect of OO. • The class definition is the main programming to that • used to implement encapsulation • A class is a description of a collection of objects with common • attributes, behavior, and responsibilities

  33. Encapsulation • One would say “How does that work?” The other replies “How does popcorn pop? Who cares?” • Encapsulation provides the ability to hide data • “data hiding” • Objects are composed of internal (private) sections and external (public) sections • Private is a combination of internal data and methods • External section is often referred to as its “interface” • Presents everything that the external users of the object need to know or are allowed to know • Objects encapsulate what they do. That is they hide the inner workings of their operations from the outside world and from other objects.

  34. Communication via Messages • Messages are how objects communicate with each other • Usually, a message is sent by a method call as a normal part of the execution of the program logic of an object • Messages may also originate from the operating system interface or language runtime system

  35. Object Lifetime • Instantiation • Creating an instance of an object of given class • Instance • A specific object that is an instantiation of a class • An instance has specific attributes and behaviors • It has a unique identify • Constructor • An operation that creates an object and defines its initial state • Object lifetime • The time an object exists • From its instantiation in its constructor until the object no longer exists and has been finalized by the Java garbage collector

  36. A reader can borrow an unlimited number of books, from 0 to an unspecified number A specific book will be borrowed by at most one reader Class Hierarchies • The arrangement of classes into hierarchies • The simplest hierarchy is called an association • Basic UML notation Class Diagrams Class Hierarchies Class LibraryBook Reader Simple Class View 0..* 0..* borrowing 0..1 Class publciAttr: type = initValue ProtectedAttr: type methodA(argList) : return type methodB…. Detailed Class View

  37. Class Hierarchies • Aggregation ( ) • A whole/part hierarchy • Whole/part • A relationship between classes in which one class is made up of or contains objects of another class • An aggregate object includes (has – a) other objects, each of which is considered to be a part of (part-of) the aggregate object • has – a • A way to state a whole/part relationship. The whole object has-a part • part –of • The opposite of has-a • Composition ( ) • A composition is a form of aggregation in which the whole cannot exist without having the parts

  38. Inheritance Genearlization/specialization is-a relationship: A term used in inheritance hierarchies A subclass is-a specialized kind of a more general super class A mechanism that allows one class (subclass) to share the attributes and behaviors of another class (superclass) Class Hierarchies Animal Mammal Carnivore Primate Dog Cat

  39. Class Hierarchies • Take the existing class, clone it, and then make additions and modifications to the clone • However, if the original class (bass or super or parent class) is changed, the modified “clone” (inherited or sub or child class) reflects those changes • In Java, a subclass can inherit from only one superclass – single inheritance • Other language supports multiple inheritance • E.g. C++ • Java supports an interface • A specification of methods that a class using the interface must implement • After you’ve created a number of classes, you might notice they’re not related to a particular parent, but their behaviors might include some of the same operation • E.g. typewrite vs. keyboard

  40. Polymorphism • Greek words • “Multiple” and “Shape” • Allows the appropriate method for any specific object to be used automatically • A characteristic of inheritance that ensures that instances of such subclasses behave correctly • Perform the same operation on different types of classes as long as they share a common trait • For example, you can open a door, you can open a window, you can open a newspaper, etc. • Two forms • Method overloading • Different types of information in the message being sent to an object • Method overriding • Dynamic binding

  41. Other OO Concepts • Abstract classes • A class that has no instances • It is usually defined with the assumption that concrete subclasses will be derived from it and extend its basic attribute and behavior • In Java, an abstract class is one that includes an abstract method • Setter and getter • When a class needs to provide information about its state, the convention is to use setters (mutators) and getters (accessors) • Setter • Allows the outside world to modify an attribute of a class • Getter • Returns the value of a class attribute

  42. Other OO Concepts • Attributes and method: class vs. instance • Class attribute and method • Attributes of a class that are shared by all instances of the class • A method defined by a class that operates only on class attributes • It is possible to access these class attributes/ method without creating any instances of class • Sometimes called static attributes/methods in Java • Instance attribute and method • An attributes of a class that is associated with a particular instance of the class • Methods defined by a class that operate on instance attributes

  43. Other OO Concepts • Visibility • The ability of one class to see and use the resources of another class • Public visibility • Visible to the whole world • Private visibility • Visible only to members of the given class • Protected visibility • Visible to the class and its subclasses • Friend visibility • Visible to a specified set of other classes • In Java, the package is used to define friend visibility • In C++, the friend specifier is used

  44. Example: UML Notation Library Person 1 1 1 Book 0..* Borrowing 1..* 0..* 1..* Page LibraryBook Reader 0..4

More Related