2008年4月28日 星期一

lab class definition 2

Study Display 4.4 (2nd ed. and 3rd ed.) or Display 4.2 & Display 4.3 (1st ed.) and then
1. Comment out date.setDate(6, 17, year); by // date.setDate(6, 17, year);
2. At the next line below, add date.readInput();
3. Run the program again. Fix any problems you may encouter along the way.
4. At the last line of your program, add System.out.println(date.month);and see what happens. Why?

System.out.println()非CLASS的METHOD 故此無法純取CLASS的MEMBER



import java.util.Scanner;

public class DateThirdTry
{
private String month;
private int day;
private int year;

public void setDate(int newMonth, int newDay, int newYear)
{
month = monthString(newMonth);
day = newDay;
year = newYear;
}
public String monthString(int monthNumber)
{
switch (monthNumber)
{
case 1:
return "January";
case 2:
return "Februay";
case 3:
return "March";
case 4:
return "April";
case 5:
return "May";
case 6:
return "June";
case 7:
return "July";
case 8:
return "August";
case 9:
return "September";
case 10:
return "October";
case 11:
return "November";
case 12:
return "December";
default:
System.out.println("Fatal Error");
System.exit(0);
return "Error";
}
}


public void writeOutput()
{
System.out.println(month + " " + day + ", "+ year);
}

}

-----------------------------------------------------------------------------------

public class DateThirdTryDemo

{
public static void main(String[] args)
{
DateThirdTry date = new DateThirdTry();
int year = 1882;
date.setDate(6, 17, year);

date.writeOutput();
}


}


-------------------------------------------------------------------------------------
public class DateThirdTryDemo

{
public static void main(String[] args)
{
DateThirdTry date = new DateThirdTry();

date.readInput();

System.out.println(date.month);

}

}

沒有留言: