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 part of the phone number according to user input * @param strphonenum phone number, such as: "0591-83279988-002" * @return Returns the number part, such as: "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
There are no restrictions on the number of users entered, such as the length of the number, what type, etc.;
(2) Write down the reason why the problem arises
If statement condition is not sufficient, only determine whether the number is empty
(3) Give the modified code
public string Getphonenumber (string strphonenum) {
if ((strphonenum==null) | | | "". Equals (Strphonenum)) {
if (strphonenum.lenth==8 &&!pattern.compile ("^\\d{8}$"). Matcher (Phonenumber.trim ( )). Matches ())) {
Return "true";
}
}
else{
Return "false";
System.out.println ("The number entered is wrong!") ");
}
String[] Arrphone=strphonenum.split ("-");
return arrphone[1];
}
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)
public class ArraySort {
/**
The implementation of the bubble sorting algorithm array sorting
*/
public static int[] sort (int[] num) {
This loop compares the two numbers of the array, passing the first number to the second number if the first number is larger than the second.
for (int i = 0; i<num.length; i++) {
for (int j= i+1; j<num.length; J + +) {
if (Num[i]>num[j]) {
int numb = Num[i];
Num[i] = Num[j];
NUM[J] = numb;
}
}
}
for (int i:num) {//num assigns a value to I
System.out.print (i + "");
}
return num;
}
}
-----------------------------------------------------------------------------------------
public class Array {
/**
* @param args
*/
public static void Main (string[] args) {
Int[] i={-45,-11,10,-15,1,2,77,8,56,0};
System.out.println ("Sort result is:");
Arraysort.sort (i); Call the Sort method
System.out.println ();
}
}
(2) Explain your design ideas
The idea of bubble sort is used to design array sorting, and then the sort method in the ArraySort class is called through the main function to implement array sorting.
(3) Write a possible error (at least five kinds)
1. When the elements in the array are part of the same;
2. When the elements in the array are all the same;
3. When the element in the array has a negative number present;
4. When the order of elements in the array is already lined up;
5. When the order of the elements in the array is reversed;
(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. When the elements in the array are part of the same;
2. When the elements in the array are all the same;
3. When the element in the array has a negative number present;
4. When the order of elements in the array is already lined up;
5. When the order of the elements in the array is reversed;
Third Blog Park Homework