宿題の手伝いが必要です。それは、独自のクラスを作成し、そのクラス内で作成したメソッドを別のクラスに使用して特定のタスクを実行することです。2 つのクラスを作成しました。私の結果を表示します。私の場合、プログラムに最初の日付をハードコーディングしたテキストを表示させ、新しい日付を表示する 2 行目のテキストを表示させたいと考えています。私の問題は、プログラムを実行すると、初期化された日付と新しい日付を示すテキストのみが表示され、実際の数字は出力されないことです。私のgetメソッドで何かをしなければならないかもしれないと思いますが、よくわかりません。私はこのウェブサイトも初めてなので、この投稿を非常に醜いものにするために私が犯した間違いを明らかにしてください.
授業日からのコード
public class Date {
//instance variables
private int month;
private int day;
private int year;
//constructor
public Date( int month, int day, int year){
this.month = month;
this.day = day;
this.year = year;
}
//set methods
public void setMonth (int month){
this.month = month;
}
public void setDay(int day){
this.day = day;
}
public void setYear(int year){
this.year = year;
}
//get methods
public int getMonth(){
return month;
}
public int getDay(){
return day;
}
public int getYear(){
return year;
}
//Display mehtod
public String displayDate(){
return "Date with new values is: " + month + "/" + day + "/" + year;
}
}//end of coding
DATETEST からのコード
public class DateTest {
public static void main(String[] args) {
Date date1 = new Date( 7, 4, 2004 );// create new date object
System.out.println( "The initial date is: ");//display initial value
date1.getMonth();
date1.getDay();
date1.getYear();
date1.displayDate();
// change date values
date1.setMonth( 11 );
date1.setDay( 1 );
date1.setYear( 2003 );
System.out.println( "Date with new values is: " );
date1.displayDate();
System.out.println(); // output a newline
}
}//end class DateTest
プログラムを実行するとどうなるか
The initial date is:
Date with new values is:
(日付の数字は表示されません)