<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆 - 117  文章 - 72  trackbacks - 0

    聲明:原創(chuàng)作品(標有[原]字樣)轉載時請注明出處,謝謝。

    常用鏈接

    常用設置
    常用軟件
    常用命令
     

    訂閱

    訂閱

    留言簿(7)

    隨筆分類(130)

    隨筆檔案(123)

    搜索

    •  

    積分與排名

    • 積分 - 155667
    • 排名 - 391

    最新評論

    [關鍵字]:java,design pattern,設計模式,《Java與模式》學習,Immutable Pattern,不變模式
    [環(huán)境]:StarUML5.0 + JDK6
    [作者]:Winty (wintys@gmail.com) http://m.tkk7.com/wintys/
    [正文]:

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

    /**
     * 不變模式:Immutable Pattern
     *
     * 復數的四則運算
     * @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;//實部
        private double imaginary;//虛部
        public static final Complex i;//虛數單位

        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;
        }

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

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

        /**
         * 復數相乘:(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);
        }

        /**
         * 復數除法:
         * (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){//實部不為0時才顯示實部
                str = strReal;

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

            return str;
        }
    }

    運行結果:
    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)作品,轉載請注明出處。
    作者:Winty (wintys@gmail.com)
    博客:http://m.tkk7.com/wintys
    posted on 2009-06-16 22:52 天堂露珠 閱讀(990) 評論(0)  編輯  收藏 所屬分類: Pattern
    主站蜘蛛池模板: 久久国产乱子伦精品免费一| 99re6在线精品免费观看| 97视频免费在线| 亚洲最大福利视频网站| 最近免费中文字幕mv电影| 亚洲经典在线中文字幕| 色片在线免费观看| 亚洲一区二区三区高清在线观看| 欧洲精品成人免费视频在线观看| 亚洲中文字幕无码mv| 无码国模国产在线观看免费| 羞羞网站免费观看| 中文字幕在亚洲第一在线| 久久黄色免费网站| 亚洲美女视频网站| 无人在线直播免费观看| 亚洲国产精品无码久久98| 五月天婷亚洲天综合网精品偷| 老司机精品免费视频| 亚洲色欲色欲综合网站| 99久久免费国产香蕉麻豆| 精品国产亚洲AV麻豆| 人人狠狠综合久久亚洲88| 精品成在人线AV无码免费看 | 59pao成国产成视频永久免费 | 成人av免费电影| 国产精品自拍亚洲| 亚洲av中文无码乱人伦在线咪咕| 8x8×在线永久免费视频| 亚洲国产精品综合久久20| 免费国产成人高清在线观看麻豆| 老司机精品免费视频| 亚洲免费观看网站| 四虎影视在线永久免费观看| 成年私人影院免费视频网站| 久久久久久亚洲精品无码 | 亚洲综合久久1区2区3区| 日本媚薬痉挛在线观看免费| 水蜜桃视频在线观看免费播放高清| 亚洲综合亚洲国产尤物| 免费成人av电影|