1.2.11 Develop an implementation SmartDate of our Date API that raises an exception if the date is not legal.
1.2.12 Add a method dayOfTheWeek() to SmartDate that returns a String value Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, or Sunday, giving the appropriate day of the week for the date. You may assume that the date is in the 21st century.
public class E1211and1212 {
public static class SmartDate {
private final int [ ] Days = { 31 , 29 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 } ;
private final int m;
private final int d;
private final int y;
private final String [ ] week = { "Mongday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday" , "Saturday" , "Sunday" } ;
public SmartDate ( int m, int d, int y)
{
if ( ! isValid ( m, d, y) ) throw new IllegalArgumentException ( "Invalid date" ) ;
this . m = m;
this . d = d;
this . y = y;
}
public SmartDate ( String date) {
String [ ] fields = date. split ( "/" ) ;
if ( fields. length != 3 ) {
throw new IllegalArgumentException ( "Invalid date" ) ;
}
m = Integer . parseInt ( fields[ 0 ] ) ;
d = Integer . parseInt ( fields[ 1 ] ) ;
y = Integer . parseInt ( fields[ 2 ] ) ;
if ( ! isValid ( m, d, y) ) throw new IllegalArgumentException ( "Invalid date" ) ;
}
public boolean isValid ( int m, int d, int y) {
if ( m < 1 || m > 12 ) return false ;
if ( d < 1 || d > Days [ m- 1 ] ) return false ;
if ( m == 2 && ! isLeapYear ( y) ) return d <= 28 ;
return true ;
}
public boolean isLeapYear ( int y) {
if ( y % 400 == 0 ) return true ;
if ( y % 100 == 0 ) return false ;
return y% 4 == 0 ;
}
public int getMonth ( ) {
return this . m;
}
public int getYear ( ) {
return this . y;
}
public int getday ( ) {
return this . d;
}
public String dayOfTheWeek ( ) {
SmartDate startDate = new SmartDate ( 1 , 1 , 2000 ) ;
int indexStart = 5 ;
int daysNum = 0 ;
if ( this . y < startDate. getYear ( ) ) throw new IllegalArgumentException ( "请输入21世纪的日期!" ) ;
for ( int startYear = startDate. getYear ( ) ; startYear < this . y; startYear++ )
{
if ( isLeapYear ( startYear) ) daysNum += 366 ;
else daysNum += 365 ;
}
for ( int startMonth = startDate. getMonth ( ) ; startMonth < this . m; startMonth++ )
{
daysNum += Days [ startMonth- 1 ] ;
if ( startMonth == 2 && ! isLeapYear ( this . y) ) daysNum-- ;
}
daysNum += this . d;
daysNum-- ;
int leftdaysNum = daysNum % 7 ;
int indexCurrent = ( leftdaysNum + indexStart) % 7 ;
return week[ indexCurrent] ;
}
public String toString ( ) {
return this . m + "/" + d + "/" + y;
}
}
public static void main ( String [ ] args) {
SmartDate smartDate = new SmartDate ( "8/8/2021" ) ;
System . out. println ( smartDate) ;
System . out. println ( smartDate. dayOfTheWeek ( ) ) ;
}
}
1.2.13 Using our implementation of Date as a model (page 91), develop an implementation of Transaction.
1.2.14 Using our implementation of equals() in Date as a model (page 103), develop implementations of equals() for Transaction
import edu. princeton. cs. algs4. Date ;
public class E1213and1214 {
public static class Transaction {
private String who;
private Date when;
private double amount;
public Transaction ( String who, Date when, double amount)
{
this . who = who;
this . when = when;
this . amount = amount;
}
public String getwho ( ) {
return this . who;
}
public Date getwhen ( ) {
return this . when;
}
public double getamount ( ) {
return this . amount;
}
@Override
public String toString ( ) {
return this . who + " " + this . when + " " + this . amount;
}
@Override
public boolean equals ( Object other) {
if ( other == this ) return true ;
if ( other == null ) return false ;
if ( other. getClass ( ) != this . getClass ( ) ) return false ;
Transaction that = ( Transaction ) other;
return ( this . who == that. who) && ( this . when == that. when) && ( this . amount == that. amount) ;
}
}
public static void main ( String [ ] args) {
Date date = new Date ( 8 , 8 , 2021 ) ;
Transaction transaction1 = new Transaction ( "张三" , date, 11.12 ) ;
Transaction transaction2 = new Transaction ( "李四" , date , 11.12 ) ;
if ( transaction2. equals ( transaction1) ) System . out. println ( "true" ) ;
else System . out. println ( "false" ) ;
}
}