jaeki song isqs6337 lecture 01 the essence of programming language

44
ISQS6337 Jaeki Song Jaeki Song Lecture 01 The Essence of Programming Language

Upload: duane-richardson

Post on 31-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

Lecture 01The Essence of

Programming Language

Page 2: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

Course Structure

• Fundamentals of programming

• Object-oriented programming

• Graphics programming

• Developing comprehensive programming

Page 3: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

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

Page 4: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

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

Page 5: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

Processing a Java Program

Editor

Compiler

Bytecode

Linker

Loader

Interpreter

Bytecode

Create program in Java following the rules, calledsource 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, andThen executes it.

Page 6: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

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

Algorithmdesign

Coding

Compiler

Interpreter

Results

No error

Error

Error

Page 7: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

Program Development Life Cycle (PDLC)

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

3. Code Program- Translate solution algorithm- Enter program code into computer4. Test Program

- Remove any syntax error- Remove any logic error

5. Formalize Solution- Review program code- Review documentation- Implement program

6. Maintain Program- Identify errors- Identify enhancements

Page 8: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

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

Page 9: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

Structured Programming

• Hierarchical Input Process Output (HIPO) chart

• Algorithmic Thinking– Pseudocode– Flowchart– Storyboard

Page 10: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

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.

Page 11: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

HIPO Chart

Main

ProcessInitialization

DeclareVariable

Print Report

Read arecord

CalculateAccumulate

totalPrint

Detail Calculateaverage

Print totalAnd averages

Wrap-up

Calculatediscount

CalculateGross amount

Calculate netAmount due

Page 12: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

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

Page 13: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

Pseudocode

• uses English-like phrases to describe the instructions– List the actions using

keywords– Depicts logical

grouping or structures using indentation

MAIN MODULE:Call InitializationCall ProcessCall Output

END

PROCESS MODULE:Do While not End of File

Read a recordCall CalculateCall AccumulatePrint Detail Line

End DoRETURN

CALCULATE MODULE:If Hours > 40 then

Call overtimeElse

Call Regular timeEnd If

RETURN

Page 14: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

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

Page 15: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

Example: Paycheck-Pseudocode

1. Get baseSalary2. Get noOfServiceYears3. Calculate bonus using the following fomula:

if (noOfServiceYears is less than or equal to five)bonus = 10 * noOfServiceYears

otherwise bonus = 20 * noOfServiceYears

4. Get totalSale5. 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)

6. Calculate payCheck using the equation payCheck = baseSalary + bonus + additionalBonus

Page 16: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

Flow Chart

ProcessSymbol

Represent process

I/OSymbol

Makes data available for processing (input) orDisplaying (output) of process information

Decisionsymbol

Represents a decision that determines whichOf number of alternative paths is to be followed

Connectorsymbol

Represents any entry form, or exit to, another partof the flow chart

Terminalsymbol

Represents the beginning, the end, or a point of Interruption or delay in a program

Page 17: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

Storyboard

Interest Calculator

Principal:

Interest Rate:

Months:

Amount Paid:

CalculateCalculate ClearClear

principalField

amtlField

clearButton

monthslField

calButton

monthsLabel

intLabel

principalLabel

intField

amtLabel

Page 18: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

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

Page 19: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

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

BedroomDining Room

Page 20: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

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

Page 21: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

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

Page 22: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

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

Page 23: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

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 behaviorof an object are defined by the class definition

Page 24: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

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

Page 25: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

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

Page 26: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

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 namemodel nameserial numbercapacity

add clothes()add detergent()remove clothes

Page 27: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

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

Page 28: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

Class and Object

• A class serves another purpose in addition to categorization

• A class is a template for making objects

Washing Machinebrand namemodel nameserial numbercapacity

add clothes()add detergent()remove clothes

Washing Machine

brand namemodel nameserial numberCapacitydrum volumemotor

add clothes()add detergent()remove clothes( )time-the-soak()time-the-wash()

Page 29: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

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.

Page 30: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

Fundamental Properties of an Object Oriented System

• Abstraction with objects

• Encapsulated classes

• Communication via messages

• Object lifetime

• Class hierarchies

• Polymorphism

Page 31: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

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.

Page 32: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

Encapsulation

Encapsulation. The process of hiding allthe internal details of an object from the outsideworld. In Java, encapsulation is enforced byhaving the definitions for attributes, and methodsinside 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

Page 33: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

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.

Page 34: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

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

Page 35: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

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

Page 36: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

Class Hierarchies

• The arrangement of classes into hierarchies– The simplest hierarchy is called an association– Basic UML notation

Class

Class Diagrams

Class

publciAttr: type = initValueProtectedAttr: type

methodA(argList) : return typemethodB….

Simple Class View

Detailed Class View

Class Hierarchies

LibraryBook Reader0..*0..*

0..1

borrowing

A reader can borrow an unlimited numberof books, from 0 to an unspecified number

A specific book will be borrowed by at mostone reader

Page 37: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

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

Page 38: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

Class Hierarchies

• 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)

Animal

Mammal

Carnivore Primate

Dog Cat

Page 39: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

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

Page 40: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

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

Page 41: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

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

Page 42: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

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

Page 43: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

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

Page 44: Jaeki Song ISQS6337 Lecture 01 The Essence of Programming Language

ISQS6337

Jaeki SongJaeki Song

Example: UML Notation

LibraryPerson

Reader

Borrowing

LibraryBookPage

Book

1..* 1..*

0..4

1 1 1

0..*

0..*