• Algs4-1.3.16从文件中读取日期生成日期对象数组


     1.3.16使用1.3.1.5节中的readInts()作为模板为Date编写一个静态方法readDates(),从标准输入中读取由练习1.2.19的表格所指定的格式的多个日期并返回一个它们的数组。
    答:public static SmartDate[] readDates(String name)
    图片
    public class SmartDate
    {
       private final int myYear;
       private final int myMonth;
       private final int myDay;
      
       public SmartDate(String date)
       {
           String[] words=date.split("/");
           int month=Integer.parseInt(words[0]);
           int day=Integer.parseInt(words[1]);
           int year=Integer.parseInt(words[2]);
           //
          mySmartDate(month,day,year);
          //
           myYear=year;
           myMonth=month;
           myDay=day;
       }
      
        public SmartDate(int month,int day,int year)
        {
            mySmartDate(month,day,year);
            //
            myYear=year;
            myMonth=month;
            myDay=day;
            //
          }
       
        private void mySmartDate(int month,int day,int year)
        {
            if(year<0)       throw new IllegalArgumentException("year is error.");
            if(month<1 || month>12)    throw new IllegalArgumentException("month is error.");
            if(day<1 || day>31)    throw new IllegalArgumentException("day is error.");
            if(month%2==0 && day>30)  throw new IllegalArgumentException("day is error.");
            if(year%400!=0 && (!(year%4==0 && year%100!=0)) && month==2 && day>28) throw new IllegalArgumentException("day is error.");
            if((year%400==0 || (year%4==0 && year%100!=0)) && month==2 && day>29) throw new IllegalArgumentException("day is error.");
        }
       
        public int month()
        {
            return myMonth;
        }
       
        public int day()
        {
            return myDay;
        }
       
        public int year()
        {
            return myYear;
        }
       
        public String dayOfTheWeek()
        {
            int w=(myYear%100+(myYear%100)/4+(myYear-myYear%100)/4-2*(myYear-myYear%100)+26*(myMonth+1)/10+myDay-1)%7;
            if(w<0) w=w+7;
            String weekstr="";
            switch(w)
            {
                case 0: weekstr="Sunday"; break;
                case 1: weekstr="Monday"; break;
                case 2: weekstr="Tuesday"; break;
                case 3: weekstr="Wednesday"; break;
                case 4: weekstr="Thursday"; break;
                case 5: weekstr="Friday"; break;
                case 6: weekstr="Saturday"; break;
            }
          return weekstr;
        }
       
        public String toString()
        {
            return month()+"/"+day()+"/"+year();
        }
       
        public static SmartDate[] readDates(String name)
        {
            In in=new In(name);
            Queue<String> q=new Queue<String>();
            while(!in.isEmpty())
                q.enqueue(in.readString());
            int N=q.size();
            SmartDate[] a=new SmartDate[N];
            for(int i=0;i<N;i++)
                a[i]=new SmartDate (q.dequeue());
            return a;
        }
       
        public static void main(String[] args)
        {
            SmartDate[] b=SmartDate.readDates(args[0]);
            for(int i=0;i<b.length;i++)
                StdOut.printf("%s ",b[i].toString());
        }
    }
  • 相关阅读:
    FineUI第十三天---`列布局
    FineUI第十二天---锚点布局
    FineUI第十一天---布局概述
    FineUI第十天---AJax的特性
    FineUI第九天---表单验证
    FineUI第八天----下拉列表控件
    FineUI第七天---文件上传
    FineUI第六天---表单控件
    FineUI第五天---按钮控件
    快速隐写术的一个小程序
  • 原文地址:https://www.cnblogs.com/longjin2018/p/9849468.html
Copyright © 2020-2023  润新知