• 算法-第四版-练习1.2.11解答


    根据Date的API实现一个SmartDate类型,在日期非法时抛出一个异常。


    /**
     * Description : 
     * Author      : mn@furzoom.com
     * Date        : Sep 26, 2016 5:40:07 PM
     * Copyright (c) 2013-2016, http://furzoom.com All Rights Reserved.
     */
    package com.furzoom.lab.algs.ch102;
    
    /**
     * ClassName    : E10211 <br>
     * Function     : TODO ADD FUNCTION. <br>
     * date         : Sep 26, 2016 5:40:07 PM <br>
     * 
     * @version 
     */
    public class E10211
    {
        public static void main(String[] args)
        {
            try {
                SmartDate date = new SmartDate(13, 1, 1999);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            }
            
            try {
                SmartDate date = new SmartDate(2, 29, 2000);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            }
             
            try {
                SmartDate date = new SmartDate(2, 29, 1900);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            }
             
            try {
                SmartDate date = new SmartDate(2, 28, 2002);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            }
             
            try {
                SmartDate date = new SmartDate(4, 31, 2000);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            }
            
            try {
                SmartDate date = new SmartDate(5, 31, 2000);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            }
            
        }
    }
    
    class SmartDate
    {
        private final int month;
        private final int day;
        private final int year;
        
        public SmartDate(int m, int d, int y)
        {
            if (!validate(m, d, y))
                throw new IllegalArgumentException("Argument illegal " + m + "/" + d + "/" + y);
            this.month = m;
            this.day = d;
            this.year = y;
        }
        
        public int month()
        {
            return month;
        }
        
        public int day()
        {
            return day;
        }
        
        public int year()
        {
            return year;
        }
        
        public String toString()
        {
            return month + "/" + day + "/" + year;
        }
        
        private boolean validate(int m, int d, int y)
        {
            if (y == 0 || y < -1000 || y > 10000)
                return false;
            if (m < 1 || m > 12)
                return false;
            if (d < 1 || d > 31)
                return false;
            int[] months = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
            if (d > months[m])
                return false;
            if (!(y % 400 == 0 || (y % 4 == 0 && y % 100 != 0)))
            {
                if (d > 28)
                    return false;
            }
            return true;
        }
    }
    

    结果:


    java.lang.IllegalArgumentException: Argument illegal 13/1/1999
    	at com.furzoom.lab.algs.ch102.SmartDate.<init>(E10211.java:68)
    	at com.furzoom.lab.algs.ch102.E10211.main(E10211.java:21)
    java.lang.IllegalArgumentException: Argument illegal 2/29/1900
    	at com.furzoom.lab.algs.ch102.SmartDate.<init>(E10211.java:68)
    	at com.furzoom.lab.algs.ch102.E10211.main(E10211.java:33)
    java.lang.IllegalArgumentException: Argument illegal 4/31/2000
    	at com.furzoom.lab.algs.ch102.SmartDate.<init>(E10211.java:68)
    	at com.furzoom.lab.algs.ch102.E10211.main(E10211.java:45)
    


    算法-第四版-1.2 数据抽象-习题索引汇总

    算法-第四版习题索引汇总

    作者:马 岩Furzoom) (http://www.cnblogs.com/furzoom/
    版权声明:本文的版权归作者与博客园共同所有。转载时请在明显地方注明本文的详细链接,未经作者同意请不要删除此段声明,感谢您为保护知识产权做出的贡献。
  • 相关阅读:
    VC++ 之 文件操作
    Delphi7 API(5) 消息篇:WM_LBUTTONDOWN、WM_LBUTTONUP、WM_MOUSEMOVE
    VC++ 之 输入/输出类库(二)
    VB 访问控制面板
    Delphi7 API(4) 消息_重绘
    Lisp简明教程
    一次快速排序错误引发的思考(2)
    一次快速排序错误引发的思考(1)
    Common Lisp编译程序的小技巧
    暴风影音5免去广告的小技巧
  • 原文地址:https://www.cnblogs.com/furzoom/p/7710218.html
Copyright © 2020-2023  润新知