Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2014
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. /* Date.java */
  2.  
  3. import java.io.*;
  4.  
  5. class Date {
  6.  
  7. /* Put your private data fields here. */
  8. int month;
  9. int day;
  10. int year;
  11. String m, d, y, s;
  12.  
  13. /** Constructs a date with the given month, day and year. If the date is
  14. * not valid, the entire program will halt with an error message.
  15. * @param month is a month, numbered in the range 1...12.
  16. * @param day is between 1 and the number of days in the given month.
  17. * @param year is the year in question, with no digits omitted.
  18. */
  19. public Date(int month, int day, int year) {
  20. if( year >= 1 && year <= 9999 )
  21. {
  22. if( month >= 1 && month <= 12 )
  23. {
  24. switch(month)
  25. case 4:
  26. case 6:
  27. case 9:
  28. case 11: if( day >= 1 && day <= 30 )
  29. return day;
  30. case 2: if( day >= 1 && day <= 28 )
  31. return day;
  32. default: if( day >= 1 && day <= 31 )
  33. return day;
  34. }
  35.  
  36. if( month == 2 && month%4 == 0 )
  37. {
  38. if( day >=1 && day <= 29 )
  39. return day;
  40. }
  41. }
  42.  
  43. else
  44. {
  45. System.exit(0);
  46. }
  47.  
  48. m = Integer.toString(month);
  49. d = Integer.toString(day);
  50. y = Integer.toString(year);
  51.  
  52. }
  53.  
  54. /** Constructs a Date object corresponding to the given string.
  55. * @param s should be a string of the form "month/day/year" where month must
  56. * be one or two digits, day must be one or two digits, and year must be
  57. * between 1 and 4 digits. If s does not match these requirements or is not
  58. * a valid date, the program halts with an error message.
  59. */
  60. public Date(String s) {
  61.  
  62. System.out.println( m + "/" + d + "/" + y );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement