作业5 四则运算 测试与封装  5.2

 

 

开发环境:   Eclipse

开发人员:   欧其锋(201306114305)

                 余汉城(201306114317)(http://www.cnblogs.com/yuhancheng/

     分工:   欧其锋:异常处理

                 余汉城:重构

 


源代码:

 

  1 package GongNengpk;
  2 
  3 import GongNengpkTest.ChuShuLingException;
  4 import GongNengpkTest.JCException;
  5 
  6 
  7 
  8 
  9 @SuppressWarnings("serial")
 10 public class HeXinCode {
 11     
 12     public int Jia(int a,int b)
 13     {
 14         String str="";
 15         if(a>0 && b<0)
 16         {
 17             str=a+"+"+"("+b+")="+(a+b);
 18             
 19         }
 20         else if(a<0 && b>0)
 21         {
 22             str="("+a+")"+"+"+b+"="+(a+b);
 23         }
 24         else
 25         {
 26             str=a+"+"+b+"="+(a+b);
 27         }
 28         System.out.println(str);
 29         return a+b;
 30     }
 31     public int Jia(int a,int b,int c)
 32     {
 33         String str="";
 34         if(a<0 && b>0 && c>0 )
 35         {
 36             str="("+a+")"+"+"+b+c+"="+(a+b+c);
 37         }
 38         else if(a<0 && b<0 && c>0)
 39         {
 40             str="("+a+")"+"("+b+")"+c+"="+(a+b+c);
 41         }
 42         else if(a<0 && b<0 && c<0)
 43         {
 44             str="("+a+")"+"("+b+")"+"("+c+")"+"="+(a+b+c);
 45         }
 46         else if(a<0 && b<0 && c>0)
 47         {
 48             str="("+a+")"+"("+b+")"+c+"="+(a+b+c);
 49         }
 50         else
 51         {
 52             str=a+"+"+b+"+"+c+"="+(a+b+c);
 53         }
 54         System.out.println(str);
 55         return a+b;
 56     }
 57     public int Jian(int a,int b)
 58     {
 59         String str="";
 60         if(a>0 && b<0)
 61         {
 62             str=a+"-"+"("+b+")="+(a-b);
 63             
 64         }
 65         else if(a<0 && b>0)
 66         {
 67             str="("+a+")"+"-"+b+"="+(a-b);
 68         }
 69         else
 70         {
 71             str=a+"-"+b+"="+(a-b);
 72         }
 73         System.out.println(str);
 74         return a-b;
 75     }
 76     public int Cheng(int a,int b)
 77     {
 78         String str="";
 79         if(a>0 && b<0)
 80         {
 81             str=a+"*"+"("+b+")="+(a*b);
 82             
 83         }
 84         else if(a<0 && b>0)
 85         {
 86             str="("+a+")"+"*"+b+"="+(a*b);
 87         }
 88         else
 89         {
 90             str=a+"*"+b+"="+(a*b);
 91         }
 92         System.out.println(str);
 93         return a*b;
 94     }
 95     public int Chu(int a,int b) throws ChuShuLingException
 96     {
 97         int end=-1;
 98         String str="";
 99         if(b==0)
100         {    
101             throw new ChuShuLingException();    
102         }
103         else
104         {
105         if(a>0 && b<0)
106         {
107             str=a+"/"+"("+b+")="+(a/b);
108             
109         }
110         else if(a<0 && b>0)
111         {
112             str="("+a+")"+"/"+b+"="+(a/b);
113         }
114         else
115         {
116             str=a+"/"+b+"="+(a/b);
117         }
118         System.out.println(str);
119         end=a/b;
120         }
121         return end;
122     }
123     public int JC(int c) throws JCException
124     {
125         int end=-1;
126         int jc=1;
127         if(c<0)
128         {
129             throw new JCException();
130         }
131         else
132         {
133             
134         for(int i=c;i>0;i--)
135         {
136             jc=jc*i;
137             end=jc;
138         }
139         String str=c+"!="+end;
140         System.out.println(str);
141         }
142         return end;
143     }
144     
145 }
HeXinCode

相关文章: