JBUILDER2005 Unit Test Business class introduction _jsp programming

Source: Internet
Author: User
Tags abs trim
For ease of explanation, it is proposed to introduce test cases through two simple business classes, one is Piecewise function class and the other is string processing class, in which we first familiarize ourselves with these two business classes.

piecewise function Class

The Piecewise function subsection class has two functions, sign () is a symbolic function, and the getvalue (int d) function functions as follows:

When D < 2, the value is abs (d);

When -2≤d<2 and d!=0, the value is d*d;

When D=0, the value is 100;

When 2≤d, the value is d*d*d.

The code is shown in the following illustration:

Code Listing Error! Text with no specified style in the document. piecewise function

1. Package chapter25;
2.
3. public class subsection
4. {
5. public static int GetValue (int d) {
6. if (d = = 0) {
7. return 100;
8. else if (D <-2) {
9. Return Math.Abs (d);
Any else if (d >=-2 && D < 2) {
return d * D;
} else {//d >= 2
//if (d > 32) {
//return integer.max_value;
15.//}
Return d * d * D;
17.}
18.}
19.
public static int sign (double d) {
if (d < 0) {
return-1;
} else if (d > 0) {
return 1;
.} else {
-Return 0;
27.}
28.}
29.}

In the GetValue () method, when d>32, the D*d*d value will exceed the maximum value of the INT data type (32768), so when d>32, it should be handled in a special way, where we specifically comment out the specially handled code (line 13th to 15th), Simulate a potential bug.

String Handling class

Since the string classes provided in the standard JDK are limited to string manipulation, and string processing is a very common operation, the general system provides a string-handling class of its own. Here's a string-processing class, and for simplicity, we only provide a method String2array () that converts a string to an array, and its code looks like this:

Code Listing Error! Text with no specified style in the document. String Handling class

1. Package chapter25;
2. public class StringUtils
3. {
4. public static string[] String2array (String str, char Splitchar, Boolean trim) {
5. if (str = null) {
6. return null;
7.} else {
8. String tempstr = str;
9. int arraysize = 0; Array size
string[] Resultarr = null;
One. if (trim) {//If you need to remove the extra delimiters from the ends of the tail
TempStr = Trim (str, splitchar);
13.}
ArraySize = GetCharCount (TempStr, Splitchar) + 1;
Resultarr = new String[arraysize];
int fromindex = 0, endindex = 0;
for (int i = 0; i < resultarr.length; i++) {
Endindex = Tempstr.indexof (Splitchar, Fromindex);
if (Endindex = = 1) {
Resultarr[i] = tempstr.substring (Fromindex);
break;
22.}
Resultarr[i] = tempstr.substring (Fromindex, endindex);
Fromindex = Endindex + 1;
25.}
Resultarr return;
27.}
28.}
29.
30.//Remove the extra delimiters before and after the string.
private static string Trim (String str, char Splitchar) {
int beginindex = 0, Endindex = str.length ();
for (int i = 0; i < str.length (); i++) {
if (Str.charat (i)!= Splitchar) {
Beginindex = i;
break;
37.}
38.}
for (int i = Str.length (); i > 0; i--) {
if (Str.charat (i-1)!= Splitchar) {
Endindex = i;
break;
43.}
44.}
Str.substring return (Beginindex, endindex);
46.}
47.
48.//Calculate the number of delimiters in the string
private static int GetCharCount (String str, char Splitchar) {
Count of int = 0;
"For" (int i = 0; i < str.length (); i++) {
if (Str.charat (i) = = Splitchar) {
count++;
54.}
55.}
return count;
57.}
58.}

In addition to the external API String2array (), the class also contains two support methods. Trim () is responsible for removing the extra delimiters for the leading and trailing characters (line 31st to 46th), and the GetCharCount () method gets the number of delimiters in the character to get the size of the destination string array (line 49th to 57th).
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.