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< /span> (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
(2) Write down the reason why the problem arises
(3) Give the modified code
Answer: (1) Question: The input format is not strictly restricted.
(2) Reason: The code does not implement error handling of the input.
(3) Modified code:
public static string Getphonenumber (String strphonenum) { if ((strphonenum==null) | | ". Equals (Strphonenum)) { return" "; } String[] Arrphone=strphonenum.split ("-"); Specify format for input format and character for (int i = 0;i<strphonenum.length (); i++) {try{if (arrphone.length!=3) { int n =10/0; } if (Strphonenum.charat (i)! = '-') { integer.parseint (string.valueof (Strphonenum.charat (i))); else if (Strphonenum.charat (i+1) = = '-') {}}catch (Exception e) {System.out.println ("the input contains illegal characters or the input format is incorrect!) "); arrphone[1]=" "; break;}} 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)
(2) Explain your design ideas
(3) Write a possible error (at least five kinds)
(4) Write JUnit unit tests to put the test data that was previously given in the possible error into the unit test for testing
Answer: (1)
package cn.zhi.mju;/** * Shaping Array sort * @author Chen Yazi * Update time: 2016/4/28 */import Java.util.scanner;public class Test3 {/** * @param args */public static void main (string[] args) {//TODO auto-generated m Ethod stubTest3 test3 = new Test3 (); Test3.pr ();} public void Pr () {Scanner in = new Scanner (system.in); SYSTEM.OUT.PRINTLN ("Please input array, space separated"); String str = in.nextline ();//The first character is a space or tab to remove the first character for (int i = 0; I <str.length (); i++) {if (Str.charat (0) = = "| | Str.charat (0) = = ") {str=str.substring (1);}} Suppose the passed-in parameter has been determined to be an integer array, converted to an array of string[] array = Str.split (""); int[] num = new Int[array.length];for (int i = 0; i < Array.Length; i++) {Num[i] = Integer.parseint (Array[i]);} Set (num); for (int i = 0; i < Array.Length; i++) {System.out.print (num[i]+ "");}} Bubble sort public void set (int[] num) {for (int i = 0; i < num.length; i++) {for (int j = 0; J < Num.length-1; J + +) {if ( Num[j]>num[j+1]) {num[j]=num[j+1]+num[j];num[j+1]=num[j]-num[j+1];num[j]=num[j]-num[j+1];}}} }}
(2) Using bubble sort to sort the array, from small to large output.
(3)
① negative integer ordering: 1-2-3-4-5
② positive Sort: 1 2 3 4 5
③ positive and negative numbers mixed sort:-1 2-3 4-5
④ Single number: 1
⑤ of the same size: 1 1 1-2 5
(4)
Package Cn.zhi.mju;import Java.util.scanner;import Org.junit.test;public class Test3test {@Testpublic void Test () { Scanner in = new Scanner (system.in); SYSTEM.OUT.PRINTLN ("Please input array, space separated"); String str = in.nextline ();//The first character is a space or tab to remove the first character for (int i = 0; I <str.length (); i++) {if (Str.charat (0) = = "| | Str.charat (0) = = ") {str=str.substring (1);}} Suppose the passed-in parameter has been determined to be an integer array, converted to an array of string[] array = Str.split (""); int[] num = new Int[array.length];for (int i = 0; i < Array.Length; i++) {Num[i] = Integer.parseint (Array[i]);} Bubble sort for (int i = 0, i < num.length; i++) {for (int j = 0; J < Num.length-1; J + +) {if (num[j]>num[j+1]) {Num[j]=nu M[J+1]+NUM[J];NUM[J+1]=NUM[J]-NUM[J+1];NUM[J]=NUM[J]-NUM[J+1];}}} for (int i = 0; i < Array.Length; i++) {System.out.print (num[i]+ "");}}}
Minjiang College School of the next 2015-2016 years software testing course-Third blog assignment