14 1414 chapter programming mis105 week-15/ lec02 irfan ahmed ilyas
TRANSCRIPT
1414
14141414CH
AP
TE
R
Programming
MIS105
Week-15/ Lec02
Irfan Ahmed Ilyas
02/3/2002 Prepared By: Irfan Ilyas 2
1414
Lecture Objectives• Phases in Software Development Cycle
– Phase1: Program Specifications– Phase2: Program Design (Documentation Techniques)
• Psuedo code• Flow charting
– Phase 3: Program Coding– Phase 4: Program Testing– Phase 5: Installing the Program– Phase 6: Program Documentation– Phase 7: Program Maintenance
02/3/2002 Prepared By: Irfan Ilyas 3
1414
3. Program Code• Program design/ flow chart logics are transferred
to Computer Language Code
• Two main types of Computer Languages– Low Level (Machine Languages)
• A computer’s own internal language• Different for every computer• Very complex to write programs• Memory addresses need to be used literally• Example: machine code of Intel x86 processors or
21 4A5E 4096
A Machine Language Instruction
02/3/2002 Prepared By: Irfan Ilyas 4
1414
… 3. Program Code• ….Main types of Computer Languages
– High Level (Symbolic Languages)• Uses English like syntax• Commonly used on different hardware platforms• Much easier to use than machine languages• Simple symbolic names can be used for Memory
addresses• Needs a translator program (compiler) to generate
the equivalent machine code• Examples: Quick BASIC, C++, Java
LET TOTAL = AMT1 + AMT2
A program instruction in Quick Basic
02/3/2002 Prepared By: Irfan Ilyas 5
1414
….2- Example ( Advantage Advertising)C++ Code for “Compute time for Client A”
02/3/2002 Prepared By: Irfan Ilyas 6
1414
…3. Program Code (Issues)• A variety of High Level Languages are available,
including• Visual Basic,• Visual C• Java• Small Talk• Fortran
• Selecting a particular language depends upon– The expertise of the programmer– Resources available for the program (hard-disk space, monitor
type etc.)– The application area of the program
02/3/2002 Prepared By: Irfan Ilyas 7
1414
4. Program Test (Debugging)
• Process of removing bugs (errors) from a program• Possible error types
– Syntax Errors (Coding Errors)• Coding incorrectly
• Causes: Typing mistakes/ violating language rules
• Example:– PRNT “Hello” ‘A syntax error in PRINT statement of Quick
Basic
– printf(“%s: %d”,”Result”,x) ‘In C++, statement does not end with a semicolon (;)
02/3/2002 Prepared By: Irfan Ilyas 8
1414
…4. Program Test (Debugging)
• ….Possible error types– Logic Errors (Algorithm Errors)
• Mistakes in algorithm steps
• Results in – incorrect outputs
– Run-time crashes
• Causes:– Mistakes in the instruction sequence
– Using inappropriate instructions
• Not very obvious when happening with some particular values of data inputs
02/3/2002 Prepared By: Irfan Ilyas 9
1414
…4. Program Test (Debugging)• How to debug?
– Syntax Errors• Carefully comparing the program with the language valid codes• Captured automatically by the translator programs (compilers)
– Logic Errors• Running the program with test data (input values for
which correct results are known)• Logic errors are pinpointed when
– a known input-output pair doesn’t match– Program crashes for some input values.
• Debugger Programs are available (with almost all language compilers) to pinpoint logic errors
02/3/2002 Prepared By: Irfan Ilyas 10
1414
…4. Program Test (Debugging)
• Standard program testing procedure– Desk checking– Manual testing with sample (test) data– Attempt at translation– Testing sample data on computer– Testing by a selected group of potential users
(beta users)
02/3/2002 Prepared By: Irfan Ilyas 11
1414
5. Installing the Program
• A program is usually implemented as part of an overall, integrated information system
• Example:– An Accounting Information System
• Composed of many different programs (built independently)
• Each program works for a different accounting area like general ledger, payroll etc.
• The programs are then implemented in an integrated manner to bring up the overall information system
02/3/2002 Prepared By: Irfan Ilyas 12
1414
6.Program Documentation
• Documentation explains every facet of a program (usage, development, error-handling)
• Should be on-going throughout program development
• Should be compiled and organized at the final stages
• Addresses three possible communities– Program Users– Program Operators– Program Developers
02/3/2002 Prepared By: Irfan Ilyas 13
1414
7. Program Maintenance
• Two broad Maintenance categories– Correction of emerging errors– Modification for changing user needs
• Significantly consumes the share of total development time (>75%)
• Mostly done by specially hired programmers (Maintenance Programmers)
02/3/2002 Prepared By: Irfan Ilyas 14
1414
02/3/2002 Prepared By: Irfan Ilyas 15
1414
Lecture Objectives
• Introducing Object Oriented Programming (OOP)– Classes & Objects– Attributes & methods
• Benefits of OOP– Reusability (inheritance)– Hiding complexity (encapsulation)– Context driven functionality (polymorphism)
02/3/2002 Prepared By: Irfan Ilyas 16
1414
… Lecture Objectives
• Introducing Microsoft Access VBA Programming– Concept of Microsoft Access application– VBA- Object based Programming – Microsoft Access Objects– Event driven programs– Event procedures
02/3/2002 Prepared By: Irfan Ilyas 17
1414
Object Oriented Programming (OOP)
• Object is a programming unit having – data (attributes)– methods (operations)
• Every object represents some physical object in the real life situation (being programmed)
• Every object in a program is created from its Class.• Programmers write Class code for every needed object (class)
• Main program is just creating objects and make use of their functionalities
02/3/2002 Prepared By: Irfan Ilyas 18
1414
Example: Advantage Advertising
Client_BillClientNameRegularHoursOvertimeHoursRegularRateOvertimeRate
computeClientBill( TimeCardRecords )printBill( )
Client_Bill object for Client A
Main Program
Client_Bill object for Client B
A class for client bill object
Attributes
Methods
02/3/2002 Prepared By: Irfan Ilyas 19
1414
Example: Advantage AdvertisingPsuedo code for the main program (using Client_Bill Objects)
START PROGRAM Create object bill_for_clientA from class Client_Bill
bill_for_clientA = new Client_Bill() Setting client attributes
bill_for_clientA.clientName= “ClientA”bill_for_clientA.RegularRates=100……
Calling object method for computing the billbill_for_clientA.computeClientBill ( timecardRecords )
Calling object method for printing the billbill_for_clientA.printClientBill ()
END PROGRAM
02/3/2002 Prepared By: Irfan Ilyas 20
1414
Benefits of OOP
• Maximize Code Reusability– Pre-written class codes can be used in any program development– New classes can be derived from pre-written classes with some
added functionalities [Inheritance]– Example: Car class can be used for deriving more specific car
classes Toyota, Lexus, Mercedes etc.
• Simplify Program Writing– Users of an object do not need to know the exact mechanism of
any of the object functionalities [Encapsulation]– Easier for the programmers to change the functionality mechanism
any time without asking users to change their programs
02/3/2002 Prepared By: Irfan Ilyas 21
1414
…Benefits of OOP
• Context driven method calls– Some methods look similar in interface and semantics
but implemented differently for different objects [Polymorphism]
– User doesn’t need to worry about the mechanism – Example:
• print method in Client_Bill class• For two different types of client bills
(clilent_printed_bill & client_onscreen_bill), the methods print does the same (semantically), however using different mechanism
02/3/2002 Prepared By: Irfan Ilyas 22
1414
Introducing Visual Basic for Applications
• Visual Basic for Applications (VBA) is the language for building applications with Microsoft office products
• VBA is an Object Based Language (not fully object Oriented)– It doesn’t allow you to use inheritance & polymorphism features– Encapsulation feature can be used to some extent.
• VBA allows to program already existing objects in different Microsoft Applications
02/3/2002 Prepared By: Irfan Ilyas 23
1414
…Introducing Visual Basic for Applications
• VBA code helps in integrating the use of different objects (tables, queries, forms etc.)
• Produces the effect of a completely customized application
02/3/2002 Prepared By: Irfan Ilyas 24
1414
An Example Microsoft Access Database
Tables-tblStudent-tblCourse-tblGrades
QueriesForms
-frmStudentManagement-frmCourseManagement-frmStudentGrades
A Microsoft Access Database (college.mdb)
User needs to use database objects interactively (using the command interface available in Access)
02/3/2002 Prepared By: Irfan Ilyas 25
1414
An Example Microsoft Access Application
College.mdb Database Objects
Users use all objects in an integrated fashion
02/3/2002 Prepared By: Irfan Ilyas 26
1414
VBA in Microsoft Access Environment
• Microsoft Access provides an event driven programming environment
• An event is a specific action that occurs on (or with) a certain Access object.
• Every object in Microsoft Access has a series of events defined.– Command Button Events
• Click, DblClick, GotFocus, LostFocus, MouseMove, MouseDown, MouseUp
– Form Events• Load, LostFocus, MouseDown, MouseUp, OnConnect, OnDiscount etc.
– And many others
02/3/2002 Prepared By: Irfan Ilyas 27
1414
Event Driven Programming
• An event may come into action (initiated/ triggered) because of– a user action (clicking, pressing a key, changing data etc.)– A time value reached (timing events)– A consequence of another event
• Programmers are allowed to write a series of actions (VBA code/ Macro commands) in response to any event.
Event Name Objects allowing the eventmouse click
command buttoncheck boxForm (Header/ Footer/ Detail Sections)radio button
changes in datatext boxcombo boxlist box
Opening or closingForm
Examples of Events in Microsoft Access
Event Name Objects allowing the eventmouse click
command buttoncheck boxForm (Header/ Footer/ Detail Sections)radio button
changes in datatext boxcombo boxlist box
Opening or closingForm
Examples of Events in Microsoft Access
02/3/2002 Prepared By: Irfan Ilyas 28
1414
An Example Event Driven Program
•Event Chart for a command button object
•Click event has a VBA Procedure attached to it.
Private Sub Command5_Click()MsgBox "Command Button Works OK"End SubMouse Click Event Happens
Go to the event chart of the command button
Run the procedure attached to it.
Program Output
02/3/2002 Prepared By: Irfan Ilyas 29
1414
Main components of VBA Programming
• Writing Procedures– VBA statements are written in form of procedures– A procedure is a series of VBA statements– A procedure may be
• attached to any objects event (event procedure) Or• written separately (without linking it to any object)
– Syntax:Sub procedure_name( ) statement 1 statement 2 : :End Sub
02/3/2002 Prepared By: Irfan Ilyas 30
1414
….Main components of VBA Programming
• What to write in VBA Procedures?– Data declaration statements
• Syntax: – Dim variable_name As data_type_in_VBA
• Example:– Dim X As Integer
– Calling Microsoft Access standard Procedures\ Functions (e.g. MsgBox)
• Syntax: – MsgBox(prompt[, buttons] [, title] [, helpfile, context])
• Example: – MsgBox(“My First VBA Program”, vbOK, “Test Message”)
02/3/2002 Prepared By: Irfan Ilyas 31
1414
….Main components of VBA Programming
• .. What to write in VBA Procedures?– Using Microsoft Access objects
• Setting attributes/ calling methods from Microsoft Access Objects
• Syntax:– ObjectName.attribute_name = <new value>– ObjectName.method_name [arguments]
• Example:– DoCmd.OpenForm frmStudents
– Forms!frmStudents.Caption = “New Caption”
– Me.Caption = “Changed Caption”
02/3/2002 Prepared By: Irfan Ilyas 32
1414
….Main components of VBA Programming
• .. What to write in VBA Procedures?– Using VBA Control Structures
• Selection StructureIf condition Then
Statement(s)[Else
Statement(s)]End If
• Example:If value = 0 Then
AlertLabel.ForeColor = "Red"
AlertLabel.Font.Bold = True
AlertLabel.Font.Italic = True
End If
02/3/2002 Prepared By: Irfan Ilyas 33
1414
….Main components of VBA Programming
• .. What to write in VBA Procedures?– Using VBA Control Structures
• Loop Structure
• Example:
02/3/2002 Prepared By: Irfan Ilyas 34
1414
Generations of Programming Languages
• Occurring in “generations”– Levels
– Machine languages to natural languages
• Lower level closer to machine language
• Higher level closer to human-like language
02/3/2002 Prepared By: Irfan Ilyas 35
1414
Generations of Languages• 1st -- Machine languages
• 2nd -- Assembly languages
• 3rd -- High level procedural languages
• 4th -- Problem-Oriented Languages
• 5th -- Natural Languages & Visual
programming
02/3/2002 Prepared By: Irfan Ilyas 36
1414