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());
}
}
答: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());
}
}