0% found this document useful (0 votes)
7 views7 pages

Quiz 2 FM

The document contains a quiz on formal methods submitted by students at COMSATS University Islamabad, Wah Campus. It includes methods for finding the maximum of two numbers, checking if a number is even, finding the minimum of three numbers, and calculating the length of a string, along with corresponding test methods. Each method is defined with preconditions and postconditions, demonstrating basic programming concepts and assertions for correctness.

Uploaded by

Mujtaba Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

Quiz 2 FM

The document contains a quiz on formal methods submitted by students at COMSATS University Islamabad, Wah Campus. It includes methods for finding the maximum of two numbers, checking if a number is even, finding the minimum of three numbers, and calculating the length of a string, along with corresponding test methods. Each method is defined with preconditions and postconditions, demonstrating basic programming concepts and assertions for correctness.

Uploaded by

Mujtaba Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

COMSATS UNIVERSITY ISLAMABAD,

WAH CAMPUS

FORMAL METHODS
Quiz No 2

Submitted by:
Hammad Ali SP21-BSE-031
Muhammad Atif SP21-BSE-027
Ahmad Bahar SP21-BSE-022
Farhad Nazir SP21-BSE-068

Class/Sec:
BSE-8A

Submitted to:
Sir Riaz Ahmad Bhatti
1. Simple Arithmetic Function: Maximum of Two Numbers

method Max(a: int, b: int) returns (max: int)

ensures max == if a > b then a else b

if a > b

{ max :=

a;

} else {

max := b;

print "The maximum of ", a, " and ", b, " is ", max,

"\n";

method TestMax() {

var result := Max(5, 3);

assert result == 5;

method Main() {

var result := Max(7, 10);

print "Result of Max(7, 10): ", result, "\n";

TestMax();

}
2. Check if a Number is Even

method IsEven(n: int) returns (isEven: bool)


ensures isEven == (n % 2 == 0)
{
isEven := n % 2 == 0;
if isEven {
print n, " is even\n";
} else {
print n, " is odd\n";
}
}

method TestIsEven()
{ var result :=
IsEven(4); assert
result == true;

result := IsEven(5);
assert result == false;
}

method Main() {

var result := IsEven(8); // This should print "8 is


even"
print "Result of IsEven(8): ", result, "\n";

result := IsEven(11); // This should print "11 is


odd"
print "Result of IsEven(11): ", result, "\n";
TestIsEven();
}

3 Find the Minimum of Three Numbers


method MinOfThree(a: int, b: int, c: int) returns
(min: int)

ensures min == if a <= b && a <= c then a else if


b <= c then b else c

if a <= b && a <= c

{ min := a;

} else if b <= c

{ min := b;

} else {

min := c;

print "The minimum of ", a, ", ", b, ", and ", c, "
is ", min, "\n";

method TestMinOfThree() {

var result := MinOfThree(3, 1, 2);


assert result == 1;

result := MinOfThree(5, 5, 7);

assert result == 5;

method Main() {

var result := MinOfThree(6, 2, 9);

print "Result of MinOfThree(6, 2, 9): ", result,


"\n";

result := MinOfThree(4, 4, 3);

print "Result of MinOfThree(4, 4, 3): ", result,


"\n";

// Optionally, call TestMinOfThree to


verify correctness

TestMinOfThree();

}
4. Calculate the Length of a String

method StringLength(s: string) returns (length:


int)
ensures length == |s|
{
length := 0;
var i := 0;
while i < |s|
invariant 0 <= i <= |s|
invariant length == i
{
length := length +
1; i := i + 1;
}
print "The length of \"", s, "\" is ", length, "\n";
}

method TestStringLength() {
var result := StringLength("Dafny");
assert result == 5;

result := StringLength("");
assert result == 0;
}

method Main() {
// Call StringLength method with sample
values and display the result
var result := StringLength("Hello, World!"); //
This should print the length of "Hello, World!"
print "Result of StringLength(\"Hello, World!\"):
", result, "\n";

result := StringLength("OpenAI"); // This should


print the length of "OpenAI"
print "Result of StringLength(\"OpenAI\"): ",
result, "\n";

// Optionally, call TestStringLength to


verify correctness
TestStringLength();
}

You might also like