实验三
1、 计算整数X和整数Y的最大公约数。(不允许采用课堂上所用的方式实现)
l 请用类和方法(写一个求最大公约数的方法)实现,命名时请按照规范命名。
l 在main方式中获取用户输入的两个整数,调用之前写的方法,输出它们的最大公约数。
l 利用FindBugs查找程序中是否存在bug。
2、 逻辑覆盖的应用
l 按照所给的程序流程图,写出Java代码(用类和方法实现)
l 写出语句覆盖、分支覆盖的测试用例,以及它所覆盖的路径,用JUnit编写测试用例进行测试
1、
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class baihetest {
public static void main(String[] args){
int a = 0;
System.out.println("请输入x:");
BufferedReader strin = new BufferedReader(new InputStreamReader(System.in));
try{
a = Integer.parseInt(strin.readLine());
}catch(NumberFormatException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
int b = 0;
System.out.println("请输入y:");
BufferedReader strin2 = new BufferedReader(new InputStreamReader(System.in));
try {
b = Integer.parseInt(strin2.readLine());
} catch (NumberFormatException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
int c = 0;
for (int i = 1; i < a; i++) {
if (a%i==0) {
if (b%i==0) {
c = i;
}
}
}
System.out.println("x与y的最大公约数为:"+c);
}
}
2、
import java.util.Scanner;
public class test {
public static void mian(String[] args){
Scanner scanner = new Scanner(System.in);
int x = scanner.nextInt();
int y = scanner.nextInt();
if (x < 4 || y > 0) {
if (y > 1) {
y = y + 1;
}
}else {
if (x >= 5) {
x = x - y;
}else {
x = x + y;
}
}
}
}
语句覆盖:
X=5 y=0 路径aeg
X=3 y=1 路径abd
分支覆盖:
X=5/4 y=1 路径aef/aeg
X=3 y=1/3 路径abd/abc