-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfact.java
executable file
·60 lines (50 loc) · 1.11 KB
/
fact.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.util.Scanner;
import java.io.*;
public class fact
{
//precondition
int y =1;
int z =0;
//function compute y=z!
public int inv(int n)
{
if(n==0)
return 1;
else
return n*inv(n-1);
} //end func factorial
//invariant : y=z!
//postcondition : y=n! ,z=n
public int factorial(int n)
{
//test precondition satisfy invariant
int tv =this.inv(z);
assert y==tv : "precondition does not satisfy invariant";
while(z != n)
{
z =z+1;
y =y*z;
}//end loop
//check postcondition after loop
assert !this.controlB(n,z):"control flow n= z";
return y;
}//end func invariant
//test factorial controlflow : B: n >=0
private boolean controlB(int n,int z)
{
return n!=z;
}//end func controlB
//test factorail
public static void main(String[] args)
{
Scanner reader = new Scanner(System.in);
System.out.print("Please Input number:");
int x = reader.nextInt();
fact f =new fact();
int a =f.factorial(x);
int b =f.inv(x);
//test invariant holds
assert a==b : "y is not equal to z!";
System.out.println(x+"!="+a);
}//end func main
} //end class fact