Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Date.java */
- import java.io.*;
- class Date {
- /* Put your private data fields here. */
- int month;
- int day;
- int year;
- String m, d, y, s;
- /** Constructs a date with the given month, day and year. If the date is
- * not valid, the entire program will halt with an error message.
- * @param month is a month, numbered in the range 1...12.
- * @param day is between 1 and the number of days in the given month.
- * @param year is the year in question, with no digits omitted.
- */
- public Date(int month, int day, int year) {
- if( year >= 1 && year <= 9999 )
- {
- if( month >= 1 && month <= 12 )
- {
- switch(month)
- case 4:
- case 6:
- case 9:
- case 11: if( day >= 1 && day <= 30 )
- return day;
- case 2: if( day >= 1 && day <= 28 )
- return day;
- default: if( day >= 1 && day <= 31 )
- return day;
- }
- if( month == 2 && month%4 == 0 )
- {
- if( day >=1 && day <= 29 )
- return day;
- }
- }
- else
- {
- System.exit(0);
- }
- m = Integer.toString(month);
- d = Integer.toString(day);
- y = Integer.toString(year);
- }
- /** Constructs a Date object corresponding to the given string.
- * @param s should be a string of the form "month/day/year" where month must
- * be one or two digits, day must be one or two digits, and year must be
- * between 1 and 4 digits. If s does not match these requirements or is not
- * a valid date, the program halts with an error message.
- */
- public Date(String s) {
- System.out.println( m + "/" + d + "/" + y );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement