Sie sind auf Seite 1von 18

CSE304: Formal Methods in

Software Engineering

BSE-6 (A & B) – Spring 2018


COMSATS Institute of Information
Technology
Formal Methods
1. Formal means
– Definite, orderly or methodical – to be done with
discipline
– In Software Engineering
• Formal method is the branch of both these
discipline (CS & SE) to formalize the programs in
order not only to design but also to verify after
construction with the focus to avoid incorrect steps.
2. A formal method we shall understand a method whose
techniques and tools can be explained in mathematics
• Notorious Bugs in (1987, 1990 and 1991)
– e.g. Patriot Missile – Hitting own barracks, leaving 28 dead and
98 wounded
• Program to test for two equal strings
• Test cases:
– isEqual (“cat”, “dog”) - expected false
– isEqual (“Testing”, “Testing”) - expected true
– isEqual (“house”, “home”) - expected false
Code
equal = strlen(string1) == strlen(string2);
if (equal)
for (i = 0; i < strlen(string1); i++)
equal = string1[i] == string2[i];
return equal;
Structure Testing

Sequence
Structure Testing

Selection – if statement
Structure Testing

Selection – if-else statement


Structure Testing

Selection – case statement


Structure Testing

Loop
Flow graph for bubble sort
1
sorted = false; // 1
while (!sorted) { // 2 2
sorted = true;
for (int i = 0; i < SIZE-1; i++) { // 3 3
if (a[i] > a[i+1]) { // 4
swap(a[i], a[i+1]); // 5 4
sorted = false;
5
}
} //6 6
} //7
//8 7

8
2N Paths
Dijkstra’s Game

Rules of the Game

Jar of Black and White Balls


Heap of Black Balls
A THREE-BALL GAME

Rules of the Game


Assignment # 1
• Solve the Dijkstra’s Game using Slides 12-
14 (with 2 & 3 different color balls)?
• Also, prepare a presentation to present it
(Group Wise)
• What is the stopping criterion of drawing
balls from the Jar?
• Can you determine the color of the last
ball?
Assignment # 1 Cont…
1. (a) Using law of logic, simplify the statement
form p ∨ [~(~p ∧ q)].
(b) Construct a truth table for the statement
form (p →q)∧(~ p → r)
Assignment # 1 cont.
2. Let p and q be propositions:
p = “you get an A on the final exam”
q = “you do every exercise in this book”
r = “you get an A in this class”
Write the following propositions using p, q, and r and logical
connectives.

Assignment # 1 cont.
3. Write the following statements in the form “if p, then q” in
English.
a) Your guarantee is good only if you bought your CD less
than 90 days ago.

b) To get tenure as a professor, it is sufficient to be world-


famous.
c) That you get the job implies that you have the best
credentials.

d) It is necessary to walk 8 miles to get to the top of the Peak.


Mathematical Model
(2 black out, 1 black in) b-2+1, w  b-1, w
f(b,w) = (2 white out, 1 black in) b+1, w-2
(1 of each out, 1 white in) b-1, w-1+1  b-1, w

• Total number of balls is reduced by exactly one in each move.


• Parity of the white ball does not change

Conclusion
The color of the last ball is dependent on the number of white balls in the jar,
i.e. it is the parity of the white ball
If even number of white balls, end up with the black ball
If odd number of white balls, end up with the white ball.

Das könnte Ihnen auch gefallen