5.1 (encapsulation, test), 5.1 encapsulation Test
Program source code (simple Java version ):
Package arrays. myArray; import java. util. required; public class SortObject {private static int intercePosition = 0; // record the length of a single operation data private static int [] intercePositionIndex = null; // record "(" subscript private static int [] intercePositionEnd = null; // record ")" subscript public static void main (String [] args) {partition input = new partition (System. in); do {System. out. println ("Enter the string you want to calculate (Note: You can only enter numbers, add, subtract, and multiply/divide symbols. After the input is complete, press enter directly ): "); String numberString = input. next (). trim (); // determines whether the input computation string complies with the required if (ispassString (numberString) = false) {System. out. println ("the calculated string you entered is incorrect. Please enter it correctly! ");} Else {// The calculation result returns System. out. println (interceResult (numberString) ;}} while (true) ;}// you can determine whether a String with parentheses exists in a private static String interceResult (String str) {String result = str; char [] numberString = str. toCharArray (); // 1 + 2 + (1*2 + 1-1*2 + 5) + 2 + (1 + 5 + 9 + 10-11) + 1*5/2 + 3 // 1 + 8-9 + (1*8/2-5 + (1 + 2 + 8) + 4/5*8/3*2 int IndexStart = 0; // The actual number of records ("int EndStart = 0; // the actual number of records") "for (int I = 0; I <numberStr Ing. length; I ++) {if ('= numberString [I]) {// record the position IndexStart = I;} if (') '= numberString [I]) {// record the initial position EndStart = I of the parentheses; // extract the result = result from the arithmetic string in the braces. substring (IndexStart + 1, EndStart); // intercept the string of the brackets for computation and generate a new computation string result = str. substring (0, IndexStart) + interceptOperation (result, '*', '/') + str. substring (EndStart + 1, str. length (); // callback execution, and the return inter operator string of other parentheses CeResult (result);} if (I = numberString. length-1) if (EndStart = 0) break;} // if no parentheses exist, perform the mixed operation result = interceptOperation (str ,'*','/'); return result;} // The arithmetic operation private static String interceptOperation (String operationNumber, char a, char B) {String mess = operationNumber; char [] stringOperation = mess. toCharArray (); // cyclically traverse the operation string and perform the corresponding operation for (int I = 0; I <stringOperation. length; I + +) {// Index of the judgment operator if (stringOperation [I] = a | stringOperation [I] = B) {if (I! = 0) {// The first number before the operator double num1 = interceptNumIndex (mess. substring (0, I); // record the length of the first data int frontPosition = intercePosition; // The second number before the operator double num2 = interceptNumEnd (mess. substring (I + 1, stringOperation. length); // record the length of the second data int backPosition = intercePosition; // after the division, replace the result with the original position, obtain the new String IndexMess = mess. substring (0, I-frontPosition + 1); String IndexResult = ""; // determines whether the final result is obtained. If (IndexMess. indexOf ('+') =-1 & IndexMess. indexOf ('*') =-1 & IndexMess. indexOf ('/') =-1 & IndexMess. lastIndexOf ('-') =-1) IndexMess = ""; if (IndexMess! = "") IndexResult = IndexMess. lastIndexOf ('-') = IndexMess. length ()-1? IndexMess. substring (0, I-frontPosition): IndexMess; // assemble the new arithmetic string mess = IndexResult // mess. substring (0, I-frontPosition + 1) + reslutString ("" + stringOperation [I], num1, num2) + mess. substring (I + backPosition + 1); // 0.111/1212/2/2/2/2/2/2/2 if (mess. lastIndexOf ('-') = 0 & mess. indexOf ('+') =-1 & mess. indexOf ('*') =-1 & mess. indexOf ('/') =-1) {break;} // callback, continue to return interceptOper Ation (mess, a, B); // 1 + 7-5 + 89/3 + 4-6*8/2 + 4-6} else continue;} if (I = stringOperation. length-1) {// recursive exit to determine whether there is any operation string in if (mess. indexOf ('+ ')! =-1 | mess. indexOf ('-')! =-1) return interceptOperation (mess, '+', '-'); break;} return mess;} // intercepts the second number of private static double interceptNumEnd (String str) {double a = 0; int InrerceIndex = 0; char [] stringOperation = str. toCharArray (); boolean ispas = false; // whether the record is negative for (int I = 0; I <stringOperation. length; I ++) {switch (stringOperation [I]) {case '*': case '/': case '+': case '-': InrerceIndex = I; if (I! = 0) // determine whether the number is negative ispas = true; break; default: break;} if (ispas) break ;} // determine whether the data is in the if (InrerceIndex = 0) {a = Double. parseDouble (str); intercePosition = str. length (); if (ispas) intercePosition ++;} else {a = Double. parseDouble (str. substring (0, InrerceIndex); // the actual length of the recorded data, intercePosition = str. substring (0, InrerceIndex ). length () ;}return a ;}// capture the first number of private static double interceptNumIndex (String str) {double a = 0; // record data int InrerceIndex = 0; // The location of the record operator boolean temp = false; // The status of the operator before recording data char [] stringOperation = str. toCharArray (); for (int I = stringOperation. length-1; I> = 0; I --) {switch (stringOperation [I]) {case '*': case '/': case '+ ': case '-': InrerceIndex = I; temp = true; break; default: break;} if (temp) break ;} // determine whether the data is in the first place of the Operation string if (InrerceIndex = 0) {a = Double. parseDouble (str); intercePosition = str. length (); // if (temp) // intercePosition ++;} else {a = Double. parseDouble (str. substring (InrerceIndex, str. length (); // the actual length of the recorded data intercePosition = str. substring (InrerceIndex, str. length ()). length () ;}return a ;}// calculation result private static double reslutString (String operation, double num1, double num2) {double sumResult = 0; if (operation. equals ("*") sumResult = num1 * num2; if (operation. equals ("-") sumResult = num1-num2; if (operation. equals ("/") sumResult = num1/num2; if (operation. equals ("+") sumResult = num1 + num2; return sumResult;} // determines whether the input method is correct. private static boolean ispassString (String messString) {boolean ispass = false; boolean operationIspass = true; // records the status of the dividend int ai = 0; // records whether an operator number exists. char [] IsString = messString. toCharArray (); int num1 = 0; int num2 = 0; for (int I = 0; I <IsString. length; I ++) {// There are several pairs of parentheses in the record if ('= IsString [I]) num1 ++; if (') '= IsString [I]) num2 ++; // determine whether the divisor is zero if ('/' = IsString [I] & IsString [I + 1] = '0') operationIspass = false; // determine whether the operation is in line with if (IsString [I] = '+' | IsString [I] = '-' | IsString [I] = '* '| IsString [I] = '/') ai ++; if (I = IsString. length-1) if (ai = 0) num2 ++;} if (operationIspass) if (num1 = num2) ispass = true; return ispass ;}}
Encapsulation code:
package test.test;public class Core { int sumResult=0; static double test1(String operation, double num1,double num2) { double sumResult = 0; if (operation.equals("*")) sumResult = num1 * num2; if (operation.equals("-")) sumResult = num1 - num2; if (operation.equals("/")) sumResult = num1 / num2; if (operation.equals("+")) sumResult = num1 + num2; return sumResult; }}
Test code:
package test.test;import static org.junit.Assert.*;import org.junit.Before;import org.junit.Test;public class CoreTest { @Before public void setUp() throws Exception { } @Test public void test() { Core core = new Core(); double a = core.test1("+",1,3); assertEquals(4,a); }}
The "Test Method" of this experiment is still being explored...