zoukankan      html  css  js  c++  java
  • 算法-第四版-练习1.3.16解答

    问题

    使用1.3.1.5节中的readInts()作为模板为Date编写一个静态方法readDates(),从标准输入中读取由练习1.2.19的表格所指定的格式的多个日期并返回一个它们的数组。

    解决思路

    思路参见模板。

    代码

    Date:

    /**
     * Description : 
     * Author      : mn@furzoom.com
     * Date        : Sep 26, 2016 10:50:43 AM
     * Copyright (c) 2013-2016, http://furzoom.com All Rights Reserved.
     */
    package com.furzoom.lab.algs;
    
    import com.furzoom.lab.algs.ch103.Queue;
    
    /**
     * ClassName    : Date <br>
     * Function     : TODO ADD FUNCTION. <br>
     * date         : Sep 26, 2016 10:50:43 AM <br>
     * 
     * @version 
     */
    public class Date
    {
        private final int month;
        private final int day;
        private final int year;
        
        public Date(int m, int d, int y)
        {
            month = m;
            day = d;
            year = y;
        }
        
        public Date(String date)
        {
            String[] s = date.split("\/");
            if (s.length != 3) {
                throw new IllegalArgumentException("Arguments illegal: " + date);
            }
            month = Integer.parseInt(s[0]);
            day = Integer.parseInt(s[1]);
            year = Integer.parseInt(s[2]);
        }
        
        public int month()
        {
            return month;
        }
        
        public int day()
        {
            return day;
        }
        
        public int year()
        {
            return year;
        }
        
        public String toString()
        {
            return month() + "/" + day() + "/" + year();
        }
        
        public boolean equals(Object x)
        {
            if (this == x) return true;
            if (x == null) return false;
            if (this.getClass() != x.getClass()) return false;
            Date that = (Date)x;
            if (this.day != that.day) return false; 
            if (this.month != that.month) return false;
            if (this.year != that.year) return false; 
            return true;
        }
        
        public static Date[] readDates(String s)
        {
            String[] dates = s.split(" ");
            int n = dates.length;
            Queue<Date> q = new Queue<Date>();
            for (int i = 0; i < n; i++) {
                q.enqueue(new Date(dates[i]));
            }
            
            Date[] result = new Date[n];
            for (int i = 0; i < n; i++) {
                result[i] = q.dequeue();
            }
               
            return result;
        }
    }
    

    测试:

    /**
     * Description : 
     * Author      : mn@furzoom.com
     * Date        : Oct 20, 2016 5:33:14 PM
     * Copyright (c) 2013-2016, http://furzoom.com All Rights Reserved.
     */
    package com.furzoom.lab.algs.ch103;
    
    import com.furzoom.lab.algs.Date;
    
    /**
     * ClassName    : E10316 <br>
     * Function     : TODO ADD FUNCTION. <br>
     * date         : Oct 20, 2016 5:33:14 PM <br>
     * 
     * @version 
     */
    public class E10316
    {
        public static void main(String[] args)
        {
            String s = "11/30/2009 1/12/2012";
            Date[] dates = Date.readDates(s);
            for (int i = 0; i < dates.length; i++) {
                System.out.println(dates[i]);
            }
        }
    }
    

    结果:

    11/30/2009
    1/12/2012



    算法-第四版-1.3 背包、队列和栈-习题索引汇总

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


  • 相关阅读:
    Oracle expdp 多表导出处理
    字符串
    Java设计模式
    多线程
    Java面向对象练习
    Java面向对象基础
    Java基础算法
    Java常识
    DOS基础命令(1)
    Java基础练习
  • 原文地址:https://www.cnblogs.com/furzoom/p/7710187.html
Copyright © 2011-2022 走看看