隨筆 - 117  文章 - 72  trackbacks - 0

          聲明:原創(chuàng)作品(標(biāo)有[原]字樣)轉(zhuǎn)載時(shí)請(qǐng)注明出處,謝謝。

          常用鏈接

          常用設(shè)置
          常用軟件
          常用命令
           

          訂閱

          訂閱

          留言簿(7)

          隨筆分類(130)

          隨筆檔案(123)

          搜索

          •  

          積分與排名

          • 積分 - 155531
          • 排名 - 390

          最新評(píng)論

          [關(guān)鍵字]:java,design pattern,設(shè)計(jì)模式,《Java與模式》學(xué)習(xí),Immutable Pattern,不變模式
          [環(huán)境]:StarUML5.0 + JDK6
          [作者]:Winty (wintys@gmail.com) http://www.aygfsteel.com/wintys/
          [正文]:

          package pattern.immutable;
          import java.text.NumberFormat;

          /**
           * 不變模式:Immutable Pattern
           *
           * 復(fù)數(shù)的四則運(yùn)算
           * @version 2009-6-15
           * @author Winty(wintys@gmail.com)
           */
          public class ImmutableTest{
              public static void main(String[] args){
                  Complex a , b , c , d , e , f , g;
                  a = new Complex(0 , 2);
                  b = new Complex(3 , -5);

                  c = a.add(b);
                  d = a.sub(b);
                  e = a.mul(b);
                  f = a.div(b);
                  g = a.add(Complex.i);

                  System.out.println("a="+a);
                  System.out.println("b="+b);
                  System.out.println("i="+Complex.i);
                  System.out.println("a+b="+c);
                  System.out.println("a-b="+d);
                  System.out.println("a*b="+e);
                  System.out.println("a/b="+f);
                  System.out.println("a+i="+g);        
              }
          }

          final class Complex extends Number{
              private double real;//實(shí)部
              private double imaginary;//虛部
              public static final Complex i;//虛數(shù)單位

              static{
                  i = new Complex(0 , 1);
              }

              public Complex(){
                  real = 0.0;
                  imaginary = 0.0;
              }

              public Complex(double real , double imaginary){
                  this.real = real;
                  this.imaginary = imaginary;
              }

              public double getReal(){
                  return real;
              }

              public double getImaginary(){
                  return imaginary;
              }

              /**
               * 復(fù)數(shù)相加
               */
              public Complex add(Complex c){
                  return new Complex(real + c.getReal() ,
                                              imaginary + c.getImaginary());
              }

              /**
               * 復(fù)數(shù)相減
               */
              public Complex sub(Complex c){
                  return new Complex(real - c.getReal() ,
                                              imaginary - c.getImaginary());
              }

              /**
               * 復(fù)數(shù)相乘:(a+bi)(c+di)=(ac-bd)+(ad+bc)i
               */
              public Complex mul(Complex c){
                  double r = c.getReal();
                  double i = c.getImaginary();
                  r = real*r - imaginary*i;
                  i = real*i + imaginary*r;

                  return new Complex(r , i);
              }

              /**
               * 復(fù)數(shù)除法:
               * (a+bi)(c+di) = (a*c + b*d)/(c*c+d*d) + (b*c - a*d)/(c*c+d*d)
               */
              public Complex div(Complex complex){
                  double a,b,c,d;
                  double denominator;
                  a = real;
                  b = imaginary;
                  c = complex.getReal();
                  d = complex.getImaginary();
                  denominator = c*c + d*d;

                  double r = (a*c + b*d)/denominator;
                  double i = (b*c - a*d)/denominator;

                  return new Complex(r , i);
              }

              public double abs(){
                  return Math.sqrt(real*real + imaginary*imaginary);
              }

              @Override
              public double doubleValue(){
                  return real;
              }

              @Override
              public float floatValue(){
                  return (float)real;
              }

              @Override
              public int intValue(){
                  return (int)real;
              }

              @Override
              public long longValue(){
                  return (long)real;
              }

              public String toString(){
                  NumberFormat f = NumberFormat.getNumberInstance();
                  f.setMaximumFractionDigits(2);
                  String strReal = f.format(real);
                  String strImaginary = f.format(imaginary);
                  String str = "";

                  String sign = "";
                  
                  if(real != 0){//實(shí)部不為0時(shí)才顯示實(shí)部
                      str = strReal;

                      if(imaginary > 0)
                          sign = "+";
                  }
                  str += sign + strImaginary + "*i";

                  return str;
              }
          }

          運(yùn)行結(jié)果:
          a=2*i
          b=3-5*i
          i=1*i
          a+b=3-3*i
          a-b=-3+7*i
          a*b=10+20*i
          a/b=-0.29+0.18*i
          a+i=3*i
          原創(chuàng)作品,轉(zhuǎn)載請(qǐng)注明出處。
          作者:Winty (wintys@gmail.com)
          博客:http://www.aygfsteel.com/wintys
          posted on 2009-06-16 22:52 天堂露珠 閱讀(989) 評(píng)論(0)  編輯  收藏 所屬分類: Pattern
          主站蜘蛛池模板: 介休市| 行唐县| 扎赉特旗| 逊克县| 巫山县| 廉江市| 上栗县| 和政县| 巴里| 镇沅| 视频| 横山县| 静海县| 高清| 紫阳县| 云安县| 安国市| 乌拉特后旗| 平武县| 南康市| 墨竹工卡县| 多伦县| 大竹县| 巩义市| 阿坝县| 繁昌县| 鸡东县| 天峨县| 淮南市| 上杭县| 柯坪县| 大田县| 中西区| 如皋市| 昭觉县| 灵武市| 弥渡县| 环江| 班戈县| 修文县| 宜昌市|