Lab Class Definitions II
class-------
/**
* s9224205
*/
import javax.swing.*;
public class Lab0410
{
private String month ;
private int day ;
private int year ;
/** public void setDate (int newMonth , int newDay , int newYear)
{
day = newDay;
year = newYear ;
month = monthString(newMonth);
}
*/
public void writeOutput()
{
System.out.println(month +"/" + day + ", "+ year);
}
public void readInput()
{
int newMonth = Integer.parseInt(JOptionPane.showInputDialog("Enter a month")) ;
day = Integer.parseInt( JOptionPane.showInputDialog("Enter a day")) ;
year = Integer.parseInt(JOptionPane.showInputDialog("Enter a year")) ;
month = monthString(newMonth);
}
public String getMonth()
{
return month ;
}
public String monthString (int monthNumber)
{
switch (monthNumber)
{
case 1:
return "Jan";
case 2:
return "Feb";
case 3:
return "Mar";
case 4:
return "Apr";
case 5:
return "May";
case 6:
return "Jun";
case 7:
return "July";
case 8:
return "Aug";
case 9:
return "Sep";
case 10:
return "Oct";
case 11:
return "Nov";
case 12:
return "Dec";
default :
System.out.println("Fatal Error") ;
System.exit(0) ;
return "Error";
}
}
}
-------
application
-------
public class DemoLab0410 {
public static void main(String [] args)
{
Lab0410 date = new Lab0410() ;
// date.setDate(6,17,2000);
// date.writeOutput() ;
date.readInput(0);
date.writeOutput() ;
/** System.out.println(date.month);
* month has private access in Lab0410
* so write a method getMonth in Lab0410
*/
System.out.println(date.getMonth() );
System.exit(0) ;
}
}
-------
0 Comments:
張貼留言
<< Home