problem:① Perfect The set () method to ensure that the correct date value ( legitimacy )is obtained;
② combines the tomorrow () and yestoday () methods into the following daysafter ( ) methods and adds some methods:
public int Getweek ()// returns the day of the week for the current date, ranging from 0to6
public string toweekstring ()// returns the Chinese string for the day of the week for the current date
Public b Oolean before (mydate D)// determine whether before the specified date
public int Daysbetween (mydate D)//returns the number of days between the current date and the date D
Public b Oolean equals (Object obj)// compares two objects for equality
Import Java.util.calendar;import Java.util.scanner;public class MyDate2 {int year,month,day; Boolean iscorrect=true; private static int thisyear; static {Thisyear=calendar.getinstance (). get (Calendar.year);//Gets the year value in the current date object} public MyDate2 (int years, int mo nth, int day) {This.set (year, month, day); } public MyDate2 () {this (1970,1,1); Call other constructed methods declared by this class} public MyDate2 (MyDate2 d) {this.set (d); } public void set (int y, int. m, int d) {if (y<1) {System.out.println ("Input year is ERROR!!!"); This.iscorrect=false; return; }if (m<1| | M>12) {System.out.println ("Input Month is ERROR!!!"); This.iscorrect=false; return; } if (Day<=0 | | day>daysofmonth ()) {System.out.println ("Input Day is ERROR!!!"); This.iscorrect=false; return; } this.year = y; This.month = m; This.day = D; } public void Set (MyDate2 d) {set (D.year, D.month, D.day); Call member method with the same name, cannot use this ()} public String toString () {return this.year+ "year" +this.month+ "month" +this.day+ "Day"; } public static int getthisyear () {return thisyear; Access static member variable} public static Boolean isleapyear (int year) {return year%400==0 | | year%100!=0 && year% 4==0; } public boolean isleapyear () {return isleapyear (this.year); Call static Method} public Boolean equals (MyDate2 D) {//this refers to the current object calling this method return This==d | | D!=null && this.year==d.year && this.month==d.month && this.day==d.day; The public boolean equals (object obj) {if (this==obj)//this refers to the object that invokes the current method re Turn true; if (obj instanceof MyDate2) {MyDate2 d = (MyDate2) obj; Type cast return this.year==d.year&& this.month==d.month && This.day==d.day; } return false; } public static int daysofmonth (int year, int month) {switch (month)//calculates the number of days per month {Case 1:case 3:case 5:case 7:case 8:case 10:case 12:return 31; Case 4:case 6:case 9:case 11:return 30; Case 2:return isleapyear (year)? 29:28; Default:return 0; }} public int daysofmonth () {return daysofmonth (this.year, this.month); } public void Tomorrow () {this.day++; Changing the value of the current object through this, there is no return value if (Day>this.daysofmonth ()) {day=1; month++; if (month>12) {month=1; year++; }}} public MyDate2 Yestoday () {MyDate2 yes=new MyDate2 (this); Executes the copy construction method, creates a new instance, and does not change the current object this yes.day--; if (yes.day==0) { yes.month--; if (yes.month==0) {yes.month=12; yes.year--; } Yes.day = Daysofmonth (Yes.year, yes.month); } return yes; Return object reference} public MyDate2 dayafter (int n) {MyDate2 yes=new MyDate2 (this); Executes the copy construction method, creates a new instance, and does not change the current object this yes.day+=n; if (yes.day==0) {yes.month--; if (yes.month==0) {yes.month=12; yes.year--; } Yes.day = Daysofmonth (Yes.year, yes.month); SYSTEM.OUT.PRINTLN ("* *" +yes.year+ "" +yes.month+ "" +yes.day "); return yes; } if (Yes.day>this.daysofmonth ()) {yes.day=1; yes.month++; if (yes.month>12) {yes.month=1; yes.year++; }} return yes; Return object reference} public int getsumdays (MyDate2 d) {iNT Sum=0; for (int i=1;i<d.year;i++) {sum+=d.isleapyear (i)? 366:365; } for (int i=1;i<d.month;i++) {sum+=d.daysofmonth (d.year,i); } Sum+=d.day; return sum; } public int Getweek () {long sum=0; Int week; for (int i=1;i<this.year;i++) {sum+=this.isleapyear (i)? 366:365; } for (int i=1;i<this.month;i++) {sum+=this.daysofmonth (this.year,i); } Sum+=this.day; week= (int) sum%7; Return week; } public String toweekstring () {switch (Getweek ()) {case 0:return "Sunday"; Case 1:return "Monday"; Case 2:return "Tuesay"; Case 3:return "Wednesday"; Case 4:return "Thursday"; Case 5:return "Friday"; Default:return "Saturday"; }} public Boolean before (MyDate2 D) {if (this.year<d.year) {return true; }else if (this.year==d.year) {if (This.month<d.month) {return true; }else if (this.month==d.month) {if (This.day<d.day) {return true; }else{REturn false; }}else{return false; }}else{return false; }} public int daysbetween (MyDate2 d) {int a=getsumdays (d); int b=getsumdays (this); Return Math.Abs (A-B); }}class mydate_ex{public static void Main (String args[]) {Scanner sc=new Scanner (system.in); SYSTEM.OUT.PRINTLN ("Please input date:"); while (Sc.hasnext ()) {MyDate2 d= new MyDate2 (Sc.nextint (), Sc.nextint (), Sc.nextint ()); if (d.iscorrect) {System.out.println ("If you want to konw yestoday date,please input-1"); System.out.println ("or Tomorrow Date,please input 1"); System.out.println (D.dayafter (Sc.nextint ())); System.out.println ("Week:" +d.getweek () + "" +d.toweekstring ()); System.out.println ("Input a cmparative Date:"); MyDate2 r=new MyDate2 (Sc.nextint (), Sc.nextint (), Sc.nextint ()); System.out.println (D.before (R)); System.out.println ("Date_d between Date_r has" +d.daysbetween (r) + "Days"); } System.out.println (); SYSTEM.OUT.PRINTLN ("Please input date:"); } }}
Encapsulation, inheritance, and polymorphism of Java's------class (i)