By: Md Rezaul Huda Reza UNIVERSITY of South Asia 2 Md Rezaul Huda Reza Examples of real-world...
-
Upload
melina-barker -
Category
Documents
-
view
220 -
download
0
description
Transcript of By: Md Rezaul Huda Reza UNIVERSITY of South Asia 2 Md Rezaul Huda Reza Examples of real-world...
UNIVERSITY OF SOUTH ASIACSE 617
VISUAL AND INTERNET PROGRAMMING
Lecture 11: Review for the final
By: Md Rezaul Huda [email protected]
UNIVERSITY of South Asia
2Md Rezaul Huda [email protected]
m
OBJECTS. REAL WORLD EXAMPLES
Examples of real-world objects: your pet, your desk, your bicycle (car?)
What do these real-world objects share? Answer: two characteristics
StateBehaviour
2
UNIVERSITY of South Asia
3Md Rezaul Huda [email protected]
m
OBJECTS. STATES AND BEHAVIOURS
State example:ColourBreedHungry
BehaviourRunningWagging tail Eating
3
UNIVERSITY of South Asia
4Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
4
CLASSES Definition: A class is a blueprint, or
prototype, that defines the variables and the methods common to all objects of a certain kind.
Example : Class Car
Variables: make, currentSpeed, colour, currentGear
Methods: brake, change gears,accelerate
4
UNIVERSITY of South Asia
5Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
5
ENCAPSULATION A class encapsulates:
Fields (Instance variables) represent the data
Instance methods represent the behavior The program does not worry how the class works
internally What is special about a class: both
data and behavior are presented in one “unit”
The methods and variables that constitute a class are called members of the class
5
UNIVERSITY of South Asia
6Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
6
CREATING CLASSES IN C# Class declaration
declares the name of the class along with other attributes
Class Body within curly braces { and } declarations for all fields (instance variables) and
static fields (class variables) and implementations for all instance methods
and static methods (class methods)
6
UNIVERSITY of South Asia
7Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
7
CONSTRUCTORS These are also part of a class definition A constructor is a sort of class method used to
create new instances - objects of the defined by the class
They are defined and used (called) Example of using/calling a constructor:
Form f; f = new Form();
7
class (type)Variable/field
constructor
UNIVERSITY of South Asia
8Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
8
MORE ON CONSTRUCTORS
Same identifier (name) as the class name
Do not return a value Void is not included Overloading possibleDefault constructor (the “no-args” constructor)No arguments Example: Form() It is defined by default if no other constructors
are defined
8
UNIVERSITY of South Asia
9Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
9
THE MEMBERS OF A TYPICAL CLASS At least one constructor (the default
one) Methods Fields (the data, sometimes called
member variables) Properties (accessed like fields, but
actually methods)
9
class name
fields
methods
UNIVERSITY of South Asia
10Md Rezaul Huda [email protected]
m
REFERENCE TYPES AND VALUE TYPE
• Value types: int, short, double, bool….• Reference types : objects that store references to the
actual data, not the value– built-in reference types: • object• string
• To instantiate a reference object in C#, you have to use new keyword.
• In C# we create a reference and then point the reference at an object allocated the heap with the new keyword, like:Object myobj;myobj = new Object();
10
UNIVERSITY of South Asia
11Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
11
MODIFIERS Appear in
method/ constructor headings variable declarations the declaration heading for classes and other
class members Indicates how it can be accessed Types of modifiers
Access (to do with visibility)Static
11
UNIVERSITY of South Asia
12Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
12
ACCESS MODIFIERS (PROTECTIONLEVELS FOR CLASS MEMBERS) Class members and classes can have one of the
following protection levelspublic – accessible to everyone internal – accessible within the same
assembly/namespaceprivate – accessible only inside classprotected – accessible for descendants –later lecture
Default protection levelsClass members (fields, instance methods) – privateClasses– internal
12
UNIVERSITY of South Asia
13Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
13
THE EFFECTS OF PUBLIC AND PRIVATE ACCESSIBILITY
13
violateEncapsulationUnless properties
enforceencapsulation
provide services to clients – for example, otherclasses
support othermethods in theclass
public private
variables
methods
UNIVERSITY of South Asia
14Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
14
IMPLEMENTING DATA ENCAPSULATION USING PROPERTIES
Use C# properties to provide access to data safelyFields(instance variables) should be declared
private, with public properties that allow safe access to them
Public properties allow clients to:Get (obtain the values of) private data
getter (accessor): controls formatting and retrieval of dataSet (assign values to) private data
setter (mutator): ensure that the new value is appropriate for the data member
14
UNIVERSITY of South Asia
15Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
15
CALLING STATIC METHODS EXAMPLE
double aValue = 78.926;double result1, result2;result1 = Math.Floor(aValue); // result1 = 78result2 = Math.Sqrt(aValue); // result2 = 8.88403061678651MessageBox.Show (“aValue rounded to 2 decimal places” + Math.Round(aValue, 2));
15
Call it through the class name, not an instanceEach call returns a value
Round is called through a class. Must be static
A Value rounded to 2 decimal places is 78.93
MessageBox class as a static method Show()
UNIVERSITY of South Asia
16Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
16
THE MEMBERS OF A TYPICAL CLASS
At least one constructor Methods (functions in other languages) Fields (the data, sometimes called member
variables) Properties (accessed like fields, but actually
methods)
16
class name
fields
methodsproperties
UNIVERSITY of South Asia
17Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
17
RELATIONSHIP BETWEEN C# ELEMENTS
17
UNIVERSITY of South Asia
18Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
18
WHAT IS A NAMESPACE? Namespaces provide scope for the
classes defined within a group -an “umbrella”
System: most important and frequently used namespace
Each namespace enclosed in curly braces—{ }
Can define your own namespace
18
UNIVERSITY of South Asia
19Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
19
ENTITY-RELATIONSHIP (E-R) MODELING Notation uses three main constructs
Data entitiesRelationshipsAttributes
Entity-Relationship (E-R) DiagramA detailed, logical representation of the
entities, associations and data elements for an organization or business
Md Rezaul Huda Reza
19
10.19
UNIVERSITY of South Asia
20Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
20
ENTITY-RELATIONSHIP (E-R) MODELING
Entity A person, place, object, event or concept in the
user environment about which the organization wishes to maintain data
Represented by a rectangle in E-R diagrams Examples
An object such as a house or a car, an event such as a house sale or a car service, or a concept such as a customer transaction or order
Attribute A named property or characteristic of an entity
that is of interest to an organization Examples:
CUSTOMER can have id, name, phone no, etc A SALE has id, amount, date, etc
Md Rezaul Huda Reza
20
10.20
UNIVERSITY of South Asia
21Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
21
RELATIONSHIPS Relationship
Association indicates that an event has occurred or that there is a natural link between entity types
Relationships are always labeled with verb phrases
Examples: One Park Has many Animals Many animals have the same species Many children can take a test
Md Rezaul Huda Reza
21
UNIVERSITY of South Asia
22Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
22
ERD KEYS Primary Key
Each entity type must have an attribute (or set of attributes) that distinguishes one instance from other instances of the same type
1. key that will not change its value2. A key that will never be null3. The key’s value must be unique for each
instance of an entity Foreign key
is an attribute that completes a relationship by identifying the parent entity
Every relationship in the model must be supported by a foreign key.
Md Rezaul Huda Reza
22
UNIVERSITY of South Asia
23Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
23
ENTITIES FOR BEAUTIFUL WILD DB CW Entities (you should have at least the
following): Animal Species RescuePark Donation Test- Child
Other possible entities for more complete design Donator– to know who adopted an animal; TestQuestion – to store separate questions for a test TestAttempt – if you allow a child to take the test more than
once. Voucher – to know what adopters or children were given
vouchers. Country – to know where the rescue park is
Md Rezaul Huda Reza
23
UNIVERSITY of South Asia
24Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
24
TABLES Relational databases views all data in the
form of tables For our example:
A table for each Entity Each column represents an attribute, e.g. the
Animal table has attributes ID, animalName, etc. Relationships between entities are represented by
values stored in columns of the corresponding tables, e.g. Species_ID is an attribute of both the Animal table and the Species table. This makes it easy to link an animal with its species.
Md Rezaul Huda Reza
24
UNIVERSITY of South Asia
25Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
25
BEAUTIFUL WILD DB VERSION 3 One extended version (not complete)
You would have to adapt/ extend further to fit your solution Available on teachmat
Md Rezaul Huda Reza
25
3
UNIVERSITY of South Asia
26Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
26
SQL When a user wants to get some information from
a database file, he can issue a query. SQL is a query language that allows user
to specify the conditions. The program will go through all the records in
the database file and select those records that satisfy the condition.(searching).
A database driven application involves an application (C #)a set of tables in the databaseRead/ write to/from the DB using SQL
Md Rezaul Huda Reza
26
UNIVERSITY of South Asia
27Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
27
3 TYPES OF SQL COMMANDS
1. Data Definition Language (DDL) 2. Data Manipulation Language (DML) 3. Data Control Language (DCL)
Md Rezaul Huda Reza
27
Programmers should know this one
UNIVERSITY of South Asia
28Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
28
SELECT Syntax:
Examples: SELECT animalName, animalWeight FROM
AnimalSELECT * FROM Animal
Md Rezaul Huda Reza
28
SELECT column_name(s)FROM table_name
SELECT * FROM table_name
UNIVERSITY of South Asia
29Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
29
THE WHERE CLAUSE Syntax:
The WHERE clause is used to extract only those records that fulfill a specified criterion..
Examples: SELECT * FROM Animal
WHERE Gender=‘Male'
Md Rezaul Huda Reza
29
SELECT column_name(s)FROM table_nameWHERE column_name operator value
Values are in single quotes
UNIVERSITY of South Asia
30Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
30
BASIC STRUCTURE OF AN SQL QUERY
Md Rezaul Huda Reza
30
GeneralStructure
SELECT, ALL / DISTINCT, *,AS, FROM, WHERE
Comparison IN, BETWEEN, LIKE "% _"
Grouping GROUP BY, HAVING,COUNT( ), SUM( ), AVG( ), MAX( ), MIN( )
Display Order ORDER BY, ASC / DESC
LogicalOperators
AND, OR, NOT
Output INTO TABLE / CURSORTO FILE [ADDITIVE], TO PRINTER, TO SCREEN
Union UNION
UNIVERSITY of South Asia
31Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
31
ADO.NET Is the .NET technology for accessing
structured data ADO.NET (ActiveX Data Objects) consists of a
number of classes that manage data and access data sources
Uniform object oriented interface for different data sources relational data bases XML dataother data sources
XML-based and independent from any particular database product or storage format
Md Rezaul Huda Reza
31
UNIVERSITY of South Asia
32Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
32
CORE ADO.NET OBJECTSDataReader
Md Rezaul Huda Reza
32
Datastore
Adapter DataSet
1*
1*
populates
Command
uses
uses
DataConsumer
usesConnection
Data Provider Objects
UNIVERSITY of South Asia
33Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
33
TWO-LAYER (CONNECTED) APPROACH
Md Rezaul Huda Reza
33
Application Data Source
DataReader
SelectCommand
Rows
Insert/Update
/DeleteCommand
Connection
UNIVERSITY of South Asia
34Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
34
PROGRAM PATTERN FOR DATA ACCESS
• Use the right one
• Declare and request connection to database
• Open connection• Execute SQL
commands• create SQL command• execute SQL command
• Release resources and close connection
Md Rezaul Huda Reza
34
1. Use Namespace2. Declare connection3. Declare a command object 4. Open connection to database
Process result5. Execute SQL commands
6. Close connection
UNIVERSITY of South Asia
35Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
35
TRY…CATCH…FINALLY BLOCKS
Code that may create a problem is placed in the try block
try {Behaviour} Code to deal with the problem (the exception handler) is
placed in catch blocks This is called a catch clause
catch {WhatToDoIfExeptionOccurs} Code to be executed whether an Exception is thrown or
not is placed in the finally block
finally {WhatToDoAtTheEnd}
Md Rezaul Huda Reza
35
UNIVERSITY of South Asia
36Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
36
PROGRAM PATTERN FOR DATA ACCESS
• Namespace• Declare and request
connection to database
• Open connection• Execute SQL
commands• create SQL command• execute SQL command
• Release resources and close connection
Md Rezaul Huda Reza
36
1. Use the right Namespace2. Declare connection3. Declare a command object try { 4. Open connection to database
Process result5. Execute SQL commands
Release Resources } catch ( Exception e) {
Handle exception } finally {6. Close connection }
UNIVERSITY of South Asia
37Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
3737
ERRORS Design errors – the program simply
doesn’t do what was intendedExample: a program is supposed to
square each value of a list, but in fact it doubles each value
Runtime errors are unplanned – they give rise to premature abortion of a program run. A runtime error often can be handled in a program
Compilation errors - before run-time
UNIVERSITY of South Asia
38Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
3838
COMPILATION ERRORS The compiler converts source code into
MSIL (MicroSoft Intermediate Language) which can be executed by the CLR (Common Language Runtime). In Visual Studio, we run the compiler by Building the application The compiler works by applying language rules It checks errors in syntax (grammar) It checks resources are present (so it will pick up a
missing needed using statement) It checks that types match Compilation of program source code is a totally
separate process from running a program. We sort out the compilation errors before we can run a program.
UNIVERSITY of South Asia
39Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
3939
ERRORS AT RUNTIME Code is compiled runtime errors trigger (raise) the
creation of an exception object some circumstances are beyond
programmer’s control You have assumed nothing unusual would
occur Unless provisions are made for
handling exceptions, your program may crash or produce erroneous results
UNIVERSITY of South Asia
40Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
4040
EXCEPTION-HANDLING TECHNIQUES Use if statements if you can
can be used to guard against common problems such as integer divide by zero
Use try{ } catch{ } for serious errors that occur infrequently
UNIVERSITY of South Asia
41Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
4141
FILTERING MULTIPLE EXCEPTIONS
You can include multiple catch clauses Should be placed from most specific to the
most generic If a generic catch is included, it should
always be placed last
UNIVERSITY of South Asia
42Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
4242
DEBUGGING Purpose: to correct those logic errors
that keep your application from running correctly
Visual studio: integrated debugging functions. stop at procedure locations, inspect memory and register values, change variables, observe message traffic, and get a close look at what your code
does.
UNIVERSITY of South Asia
43Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
4343
THE DEBUGGER The Debugger is not meant to handle
runtime Exceptions – it is meant to help you determine their source
Possible to insert “breakpoints”, so that program will stop at them and then variables etc can be examined to determine the cause of the problem
UNIVERSITY of South Asia
44Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
4444
DEBUG SYMBOLS vital for a successful debugging session
BreakpointsStepping Through CodeViewing State
help the debugger correlate instructions in your application back to file names and line numbers in your source code
stored in a program database file with a .pdb extension
UNIVERSITY of South Asia
45Md Rezaul Huda [email protected]
m45
STEPPING THROUGH CODE Once you pause execution you have the
ability to step through code, in other words, execute code one line at a time.
3 methods:Step Into Step Over Step Out
UNIVERSITY of South Asia
46Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
4646
TESTING Purpose – to test that every part of a
program works correctly according to the program specification and to engender confidence in the correctness of the program
Objective – to show the presence of errors in a program. A non-trivial program is unlikely to be 100% error free.
Testing involves : the design of test data, execution of the program with the test data
and evaluation of the results obtained.
UNIVERSITY of South Asia
47Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
4747
TESTINGGenerally the rate of error detection is proportional to the number of errors still remaining in the software. Consequently the number of errors presents declines exponentially over time.
No. of errors
Time0
Testing often continues until it becomes uneconomic. Hence often bugs remain in software.
UNIVERSITY of South Asia
48Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
4848
UNIT VS SYSTEM TESTING Small programs comprising a single
class are usually tested all at once. Larger programs are tested class by
class, called unit testing, and then brought together to test the whole, called integration or system testing.
UNIVERSITY of South Asia
49Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
4949
TECHNIQUES Black box (functional): focuses on the
range inputs and outputs of the program ignoring the actual construction of the code.
White box (structural): tests each and every possible path through software according to the statements and logic of the code.
Reviews or walkthroughs: desk checking, ‘dry run’
Stepping through code with debugger
UNIVERSITY of South Asia
50Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
5050
BLACK BOX TESTING– refers to the technique of testing a
system with no knowledge of the internals of the system.
– Black Box testers do not have access to the source code, and are oblivious of the system architecture.
i attempts to find the following types of error
incorrect or missing functions interface errors errors in data structures performance errors initialization and termination errors
UNIVERSITY of South Asia
51Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
5151
EXAMPLESuppose a program must accept the price of an orange, the number of oranges requested and to generate the total cost.
The price of an orange will be less than £1.00
The number of oranges will be in the range 1 to 20 inclusive.
A suitable error message should be displayed if either input entry in invalid.
UNIVERSITY of South Asia
52Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
5252
WHITE BOX TESTING technique of testing a system with knowledge of the internals of the system. White Box testers have access to the source code and are aware of the
system architecture.
UNIVERSITY of South Asia
53Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
53
UNIT TESTING The first unit testing software was called
SUnitDeveloped by Kent Beck (from Extreme
Programming fame) and Erich Gamma (one of the Gang of Four, from Design Patterns fame)
Used for testing SmallTalk code Test frameworks now exist for many
programming languages JUnit for Java, CppUnit for C++, NUnit
for .NET, many othersKnown collectively as xUnit
UNIVERSITY of South Asia
54Md Rezaul Huda [email protected]
m54
UNIVERSITY of South Asia
54Md Rezaul Huda Reza
UNIVERSITY of South Asia
55Md Rezaul Huda [email protected]
m
UNIVERSITY of South Asia
55
ANY QUESTION ? ? ?