Four arithmetic testing and encapsulation (improved) 5.2 5.3

Source: Internet
Author: User

A pair of Friends: Chen Shu-yun http://www.cnblogs.com/babybluecsj/

II. (1) I used my friend's last 5.1 job for unit testing. I found that when the divisor is zero, no exception was thrown during the operation, so we added

   1.     if(fh2==‘/‘){

             if (b== 0 ){                  throw  new  Exception( "The divisor cannot be 0! " );              }              answer=a/b;          }2.   try  {               res=Calculator.Onefhnum( 1 , 1 , 3 );                        catch  (Exception e) {              e.printStackTrace();              Assert.fail( "Throw an exception and test failed! " );          }This code; (2). The Code of the last job is also found to be incorrect. The last one of the three operators is the divisor, but it is not written:
If (fh2 = '/') // The first is except {If (fh3 = '+') {If (fh4 = '+ ') {If (B = 0) {Throw new exception ("the divisor cannot be 0! ");} Answer = A/B + C + D;} If (fh4 = '-') {If (B = 0) {Throw new exception ("the divisor cannot be 0! ");} Answer = A/B + c-d;} If (fh4 = '*') {If (B = 0) {Throw new exception ("the divisor cannot be 0! ");} Answer = A/B + C * D;} If (fh4 = '/') {If (B = 0) {Throw new exception ("the divisor cannot be 0! ");} If (D = 0) {Throw new exception (" the divisor cannot be 0! ");} Answer = A/B + C/d;} If (fh3 = '-') {If (fh4 = '+ ') {If (B = 0) {Throw new exception ("the divisor cannot be 0! ");} Answer = A/B-C + D;} If (fh4 = '-') {If (B = 0) {Throw new exception ("the divisor cannot be 0! ");} Answer = a/B-c-d;} If (fh4 = '*') {If (B = 0) {Throw new exception ("the divisor cannot be 0! ");} Answer = a/B-c * D;} If (fh4 = '/') {If (B = 0) {Throw new exception ("the divisor cannot be 0! ");} If (D = 0) {Throw new exception (" the divisor cannot be 0! ");} Answer = A/B-C/d;} If (fh3 = '*') {If (fh4 = '+ ') {If (B = 0) {Throw new exception ("the divisor cannot be 0! ");} Answer = A/B * C + D;} If (fh4 = '-') {If (B = 0) {Throw new exception ("the divisor cannot be 0! ");} Answer = A/B * c-d;} If (fh4 = '*') {If (B = 0) {Throw new exception ("the divisor cannot be 0! ");} Answer = A/B * C * D;} If (fh4 = '/') {If (B = 0) {Throw new exception ("the divisor cannot be 0! ");} If (D = 0) {Throw new exception (" the divisor cannot be 0! ");} Answer = A/B * C/d;} If (fh3 = '/') {If (fh4 = '+ ') {If (B = 0) {Throw new exception ("the divisor cannot be 0! ");} If (C = 0) {Throw new exception (" the divisor cannot be 0! ");} Answer = A/B/C + D;} If (fh4 = '-') {If (B = 0) {Throw new exception ("the divisor cannot be 0! ");} If (C = 0) {Throw new exception (" the divisor cannot be 0! ");} Answer = A/B/C-D;} If (fh4 = '*') {If (B = 0) {Throw new exception ("the divisor cannot be 0! ");} If (C = 0) {Throw new exception (" the divisor cannot be 0! ");} Answer = A/B/C * D;} If (fh4 = '/') {If (B = 0) {Throw new exception ("the divisor cannot be 0! ");} If (C = 0) {Throw new exception (" the divisor cannot be 0! ");} If (D = 0) {Throw new exception (" the divisor cannot be 0! ") ;}Answer = A/B/C/D ;}} return answer ;}

(3) Here, we also add the exact decimal point of the answer:

static double answer=0.0d;

public static double Onefhnum(int a,int b,int e)throws Exception{// Method of an operator

public static double Twofhnum(int a,int b,int c,int e,int h)throws Exception{// Two operators

public static double Threefhnum(int a,int b,int c,int d,int e,int h,int f)throws Exception{// Methods of the three operators

(4). Due to time, the following code is added:int precision = 0;

         if  (NUM_MAX_OPERATOR == precision) {              // Here I can process strings with a maximum of three operators :)          else  if  (NUM_SCALE == precision) {              // Here I can process a string with a value range of-100 to 100 :)          else  if  (NUM_PRECISION == precision) {              // Here I can handle strings whose precision is the first digit after the decimal point :)          }After this code, you do not know how to add or process information in parentheses.

Four arithmetic testing and encapsulation (improved) 5.2 5.3

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.