1. computes the greatest common divisor of an integer x and an integer y
Use the class and method implementation (define a class, define a greatest common divisor method in the Class), and name it according to the specification. In the main method, get the two integers entered by the user, call the previously written method, and output their greatest common divisor. Use FindBugs to find out if a bug exists in the program.
import Java.util.Scanner;
Public class Demo {
Public void Compare (int A,int b) {
int i = a>b?a:b;
for (; i>0;i--) {
if (a%i==0&&b%i==0) {
System. out. println ("A, B greatest common divisor" +i);
break;
}
}
return;
}
Public Static void Main (string[] arg) {
Demo demo = New demo ();
System. out. println ("Please enter A:");
Scanner NUM1 = new Scanner (System. in);
int a=num1.nextint ();
System. out. println ("Please enter B:");
Scanner num2 = new Scanner (System. in);
int b=num2.nextint ();
Demo.compare (A, b);
}
}
2. Application of Logic overlay
According to the program flowchart given, write out the statement overlay, branch coverage of the test case, and the path it covers.
Statement overrides:
X |
Y |
Path |
3 |
2 |
Abc |
5 |
0 |
Aef |
4 |
0 |
Aeg |
Branch Coverage:
X |
Y |
Path |
3 |
2 |
Abc |
3 |
1 |
Abd |
5 |
0 |
Aef |
4 |
0 |
Aeg |
Add-on: According to the program flowchart, write the code (define a class and method to implement), generate unit tests with JUnit, and test using the test cases previously designed.
Public class Demo {
Public void Demo (int x,int y) {
if (x<4| | y>0) {
if (y>1) {
y=y+1;
System. out. println ("x=" +x+ ", y=" +y);
}
Else {
System. out. println ("x=" +x+ ", y=" +y);
}
}
Else {
if (x>=5) {
X=x-y;
System. out. println ("x=" +x+ ", y=" +y);
}
X=x+y;
System. out. println ("x=" +x+ ", y=" +y);
}
}
}
Test Case:
Import static org.junit.assert.*;
Import Org.junit.Test;
public class Demotest {
public static void Main (string[] args) {
Demo Demo =new demo ();
Demo.demo (3,2);
Demo.demo (3,1);
Demo.demo (5,0);
Demo.demo (4,0);
}
}
Software test experiment three white box test