/*题目1
根据下列流程图编写程序实现相应分析处理并显示结果。返回结果“a=x:”(x为2、3或4);其中变量x、y均须为整型。编写程序代码,使用JUnit框架编写测试类对编写的程序代码进行测试,测试类中设计最少的测试数据满足语句覆盖测试,每条测试数据需要在测试类中编写一个测试方法。使用assertThat中equalTo断言判断期望结果值和实际返回值是否一致。
*/
```java
```java
package newProject1;
public class Kaishi {
public static int analysis(int x, int y) {
if(x >= 80 && y >= 60) {
if(x >= 90 || y >= 90) {
return 0;
}else {
int a = 2;
return a;
}
}else {
if (x <= 70 || y <= 70) {
int a = 3;
return a;
}else {
int a =4;
return a;
}
}
}
}
package newProject1;
import static org.junit.Assert.*;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.equalTo;
public class KaishiTest {
@Test
public void testAnalysis() {
assertThat(Kaishi.analysis(100, 100), equalTo(0));
assertThat(Kaishi.analysis(85, 85), equalTo(2));
assertThat(Kaishi.analysis(75, 65), equalTo(3));
assertThat(Kaishi.analysis(75, 75), equalTo(4));
}
}
/*题目2
题目2:根据输入的年份和月份判断月份的天数。若月份不在有效范围之内,应提示:“月份输入不正确。”。月份不为2月,根据输入月份输出对应的月份天数。月份为2月,根据年份判断如为普通闰年,输出2月份正确天数;如为世纪闰年,输出2月份正确天数;不为闰年输出2月份天数。返回结果格式:“year年month月份的天数是days天。”year、month为传入的值,days为判断得到的天数值。其中变量year、month均须为正整数。编写程序代码,使用JUnit框架编写测试类对编写的程序代码进行测试,测试类中设计最少的测试数据满足语句覆盖测试,每条测试数据需要在测试类中编写一个测试方法。使用assertThat中equalTo断言判断返回期望结果和实际返回是否一致。
*/
package newProject;
public class MonthDays {
public static String getMonthDays(int year, int month) {
int days = 0;
if (month < 1 || month > 12) {
return "月份输入不正确。";
} else if (month == 2) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
days = 29;
} else {
days = 28;
}
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
days = 30;
} else {
days = 31;
}
return year + "年" + month + "月份的天数是" + days + "天。";
}
}
package newProject;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.equalTo;
import org.junit.Test;
public class TestnewProject {
@Test
public void testMonthDays() {
assertThat(MonthDays.getMonthDays(2021, 2), equalTo("2021年2月份的天数是28天。"));
assertThat(MonthDays.getMonthDays(2020, 2), equalTo("2020年2月份的天数是29天。"));
assertThat(MonthDays.getMonthDays(2000, 2), equalTo("2000年2月份的天数是29天。"));
assertThat(MonthDays.getMonthDays(1900, 2), equalTo("1900年2月份的天数是28天。"));
assertThat(MonthDays.getMonthDays(2021, 4), equalTo("2021年4月份的天数是30天。"));
assertThat(MonthDays.getMonthDays(2021, 6), equalTo("2021年6月份的天数是30天。"));
assertThat(MonthDays.getMonthDays(2021, 9), equalTo("2021年9月份的天数是30天。"));
assertThat(MonthDays.getMonthDays(2021, 11), equalTo("2021年11月份的天数是30天。"));
assertThat(MonthDays.getMonthDays(2021, 1), equalTo("2021年1月份的天数是31天。"));
assertThat(MonthDays.getMonthDays(2021, 3), equalTo("2021年3月份的天数是31天。"));
assertThat(MonthDays.getMonthDays(2021, 5), equalTo("2021年5月份的天数是31天。"));
assertThat(MonthDays.getMonthDays(2021, 7), equalTo("2021年7月份的天数是31天。"));
assertThat(MonthDays.getMonthDays(2021, 8), equalTo("2021年8月份的天数是31天。"));
assertThat(MonthDays.getMonthDays(2021, 10), equalTo("2021年10月份的天数是31天。"));
assertThat(MonthDays.getMonthDays(2021, 12), equalTo("2021年12月份的天数是31天。"));
assertThat(MonthDays.getMonthDays(2021, 0), equalTo("月份输入不正确。"));
assertThat(MonthDays.getMonthDays(2021, 13), equalTo("月份输入不正确。"));
}
}
/*题目3
题目3:填写快递单时通常需要确定接收人的姓名、手机号和地址。其中要求手机号是 11 位数字字符,地址为字母开头的 10个(含10)以内字母或字母数字共同组成。填写正确则提示“OK”,否则根据实际情况提示“**不符合要求”(**为手机号或地址),退出。编写程序代码,使用JUnit框架编写测试类对编写的程序代码进行测试,测试类中设计最少的测试数据满足判定覆盖测试,每条测试数据需要在测试类中编写一个测试方法。使用assertThat中equalTo断言判断输出文字期望结果值和实际返回值是否一致。
*/
package newProject2;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.equalTo;
import org.junit.Test;
public class ExpressUtilTest {
@Test
public void testValidInput() {
String name = "张三";
String phone = "13812345678";
String address = "Avenue123";
String result = ExpressUtil.validate(name, phone, address);
assertThat(result, equalTo("OK"));
}
@Test
public void testInvalidPhone() {
String name = "李四";
String phone = "1381234";
String address = "Street456";
String result = ExpressUtil.validate(name, phone, address);
assertThat(result, equalTo("手机号不符合要求"));
}
@Test
public void testInvalidAddress() {
String name = "王五";
String phone = "13812345678";
String address = "123Avenue";
String result = ExpressUtil.validate(name, phone, address);
assertThat(result, equalTo("地址不符合要求"));
}
}
/*题目4
题目4:输入小写的字符串。如字符串前缀为ab开头,则将前缀ab替换为ef并打印出替换后字符串,返回文字“替换前缀后的字符串为:”和替换后字符串值;如后缀为cd并且前缀不为ab,替换字符串中所有cd为gh并打印出替换后字符串,返回文字“替换cd后的字符串为:”和替换后字符串值;否则全部字母大写输出,返回文字“大写字母的字符串为:”和转换后的字符串值。编写程序代码,使用JUnit框架编写测试类对编写的程序代码进行测试,测试类中设计最少的测试数据满足条件覆盖测试,测试类使用参数化测试(@Parameters)完成测试。使用assertEquals判断期望结果值和实际返回值是否一致。
*/
package newProject3;
public class StringProcessor {
public static String processString(String input) {
if (input.startsWith("ab")) {
String replaced = input.replaceFirst("ab", "ef");
System.out.println("替换前缀后的字符串为:" + replaced);
return replaced;
} else if (input.endsWith("cd") && !input.startsWith("ab")) {
String replaced = input.replaceAll("cd", "gh");
System.out.println("替换cd后的字符串为:" + replaced);
return replaced;
} else {
String uppercase = input.toUpperCase();
System.out.println("大写字母的字符串为:" + uppercase);
return uppercase;
}
}
}
package newProject3;
import static org.junit.Assert.*;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.Collection;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class StringProcessorTest {
@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{"abc", "efc"},
{"cdabcd", "ghabgh"},
{"Hello World", "HELLO WORLD"},
{"abxyz", "efxyz"},
{"pqrscd", "pqrsgh"}
});
}
private String input;
private String expectedOutput;
public StringProcessorTest(String input, String expectedOutput) {
this.input = input;
this.expectedOutput = expectedOutput;
}
@Test
public void testProcessString() {
String actualOutput = StringProcessor.processString(input);
assertEquals(expectedOutput, actualOutput);
}
}