1, a website management system, the user registration, the phone number is optional input, the input format is: Area code-Telephone number-extension number, in the middle with "-" separated. The following is the design on the JSP page and does not have any control over the input.
Suppose the system now needs to take out the middle phone number part, the code is as follows:
/**
*
* This method takes the middle phone number part according to user input.
* @param strphonenum phone number, such as: "0591-83279988-002"
* @return return number section, e.g. "83279988"
*/
public string Getphonenumber (string strphonenum) {
if ((strphonenum==null) | | | "". Equals (Strphonenum)) {
Return "";
}
String[] Arrphone=strphonenum.split ("-");
return arrphone[1];
}
Use the techniques and methods you have learned to identify the problems in this approach, and analyze the cause of the problem, and give your solution. (Hint: there is more than one problem)
Requirements:
(1) Write the question
The integrity of user input data is not considered, as well as the data type. The result array returned is an error.
(2) Write down the reason why the problem arises
The IF statement has too few criteria to judge.
(3) Give the modified code
public string Getphonenumber (string strphonenum) {
if ((strphonenum==null) | | | "". Equals (Strphonenum)) {
Return "";
}
String[] Arrphone=strphonenum.split ("-");
if (arrphone.length>1) {
return arrphone[1];
}
Return "";
}
2, write a paragraph for the integer array sorting code, explain your design ideas, and use the error-guessing method to give a possible error (at least 5), design the test case, and use JUnit to write unit tests to test. (assuming that the passed-in parameter has been determined to be an integer array)
Requirements:
(1) Write code, and conform to code specification (naming to specification, not directly written in the main method, need to have class comments, method comments, and appropriate line comments)
Import Java.util.Scanner;
public class Demo {
public static void sort (int[] values) {
int temp;
for (int i=0;i<values.length;i++) {//Number of times
for (int j=0;j<values.length-i-1;j++) {//Number of comparisons
if (Values[j]>values[j+1]) {
TEMP=VALUES[J];
VALUES[J]=VALUES[J+1];
Values[j+1]=temp;
}
}
}
}
}
(2) Explain your design ideas
Compares the adjacent elements. If the first one is bigger than the second one, swap them both. Do the same for each pair of adjacent elements, starting with the last pair from the first pair to the end. At this point, the last element should be the maximum number. Repeat the above steps for all elements, except for the last one. Repeat the above steps each time for fewer elements, until there are no pairs of numbers to compare.
(3) Write a possible error (at least five kinds)
- Array has no elements
- Elements are all the same
- have negative participation
- Have a floating point number to participate
- Both negative and floating point numbers
(4) Write JUnit unit tests to put the test data that was previously given in the possible error into the unit test for testing
1.
2.
3.
4.
5.
"Software Test" third blog assignment