これは試験の過去問からの問題です。私は質問を完了し、それは動作します。ただし、実装が弱い可能性があると感じています。たとえば、グレゴリオ クラス全体で static を使用しています。
それぞれのシナリオを考慮して、(グレゴリオ語のクラスで)適切と思われる方法で書くための3つの方法が与えられました。Gregorian クラスの 3 つのメソッドで static を使用したのは正しかったでしょうか。
また、日、月、年のフィールドは不変であることを意図していますが、それらを非公開として設定していますか? (一度作成すると、フィールド値は変更できません)
public class Date {
private int day;// needs to be immutable?
private String month;// needs to be immutable?
private int year;// needs to be immutable?
public Date(int theDay, String theMonth, int theYear) {
this.day = theDay;
this.month = theMonth;
this.year = theYear;
}
public int getDay() {
return day;
}
public String getMonth() {
return month;
}
public int getYear() {
return year;
}
}
public class Gregorian {
public static Date d;
public static boolean leapYear(){
if(d.getYear() %400==0 || (d.getYear()%4==0 && d.getYear()%100!=0)){
return true;
}else{
return false;
}
}
public static int getGregorianDateNumber(){
int a = (d.getYear()*384)*(32+d.getDay());
return a;
}
public static int getISO8601Date(){
int b = (d.getYear()*367)+d.getDay();
return b;
}
public static void main (String[] args){
d = new Date(9, "June", 8);
System.out.println(getGregorianDateNumber());
System.out.println(getISO8601Date());
System.out.println(leapYear());
}
}