2009年3月31日 星期二

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?
執行過程

程式碼

class程式碼
------------------------------------------------<程式碼開始>--------------------------------------

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 "February";
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 void readInput(){
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter month, day and year.");
System.out.println("Do not use a comma.");
month = keyboard.next();
day = keyboard.nextInt();
year = keyboard.nextInt();
}
}

------------------------------------------------<程式碼結束>--------------------------------------
主程式碼
------------------------------------------------<程式碼開始>--------------------------------------
import java.util.*;
public class class_definition_2 {
    public static void main (String args[]) {
        DateThirdTry date = new DateThirdTry();
int year = 1882;
//date.setDate(6, 17, year);
date.readInput();
date.writeOutput();
//System.out.println(date.month);
    }
}
------------------------------------------------<程式碼結束>--------------------------------------
附註:

沒有留言:

張貼留言