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

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

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

    隨筆 - 117  文章 - 72  trackbacks - 0

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

    常用鏈接

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

    訂閱

    訂閱

    留言簿(7)

    隨筆分類(130)

    隨筆檔案(123)

    搜索

    •  

    積分與排名

    • 積分 - 156642
    • 排名 - 390

    最新評論

    [關鍵字]:java,design pattern,設計模式,《Java與模式》學習,Immutable Pattern,不變模式
    [環境]:StarUML5.0 + JDK6
    [作者]:Winty (wintys@gmail.com) http://www.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
    原創作品,轉載請注明出處。
    作者:Winty (wintys@gmail.com)
    博客:http://www.tkk7.com/wintys
    posted on 2009-06-16 22:52 天堂露珠 閱讀(995) 評論(0)  編輯  收藏 所屬分類: Pattern
    主站蜘蛛池模板: 亚洲成av人片在线看片| 免费国产在线观看不卡| 久久精品亚洲中文字幕无码网站| 国产精品无码亚洲精品2021| 免费鲁丝片一级观看| 亚洲国产精品无码久久98 | A国产一区二区免费入口| 亚洲AV福利天堂一区二区三| 男女一边桶一边摸一边脱视频免费 | 精精国产www视频在线观看免费| 国产又大又长又粗又硬的免费视频| 亚洲精品中文字幕| 国产真人无遮挡作爱免费视频| 校园亚洲春色另类小说合集| 亚洲国产成人精品女人久久久 | 亚洲AV日韩AV永久无码色欲| A级毛片成人网站免费看| 亚洲人成精品久久久久| 国产.亚洲.欧洲在线| 麻豆最新国产剧情AV原创免费| 亚洲日韩精品国产一区二区三区 | 亚洲综合av永久无码精品一区二区| 毛片基地看看成人免费| 亚洲国产天堂在线观看| 猫咪社区免费资源在线观看 | 亚洲国产成人一区二区精品区| 免费网站看av片| 亚洲天堂中文字幕在线| 一个人免费视频观看在线www| 亚洲视频在线观看网站| 女人张开腿给人桶免费视频| 成年大片免费高清在线看黄| 亚洲av永久无码精品秋霞电影影院 | 亚洲一区二区三区久久久久| 国产不卡免费视频| 大地资源网高清在线观看免费 | 综合久久久久久中文字幕亚洲国产国产综合一区首| j8又粗又长又硬又爽免费视频| 国产麻豆免费观看91| 黄色网站软件app在线观看免费 | 久久精品国产亚洲AV大全|