Advertisement
Guest User

Untitled

a guest
Nov 15th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. //In this program i build a type of an object called "Date"
  2. public class Date {
  3.  
  4. private int dd;
  5. private int mo;
  6. private int yy;
  7.  
  8. //These are the creating actions
  9. //This is a creating action for the "Date" object that gets parameters (day, month, year)
  10.  
  11. public Date(int dd,int mo,int yy)
  12. {
  13. this.dd = dd;
  14. this.mo = mo;
  15. this.yy = yy;
  16. }
  17.  
  18. //This is a creating action for the "Date" object that doesn't get parameters
  19. //it automatically declares the year to be 1900, the month January and the day the first
  20.  
  21. public Date()
  22. {
  23. this.dd = 1;
  24. this.mo = 1;
  25. this.yy = 1900;
  26. }
  27.  
  28. //This is a copying building action, that gets another date as a parameter
  29. //it builds another object from the type of date with the same parameters(day,month,year)
  30.  
  31. public Date(Date d1)
  32. {
  33. this.dd = d1.getDay();
  34. this.yy = d1.getYear();
  35. this.mo = d1.getMonth();
  36. }
  37.  
  38. //These are the setting actions
  39. //This is a setting operation that determines the day
  40.  
  41. public void setDay(int nd)
  42. {
  43. this.dd = nd;
  44. }
  45. //This is a setting operation that determines the month
  46. public void setMonth(int nm)
  47. {
  48. this.mo =nm;
  49. }
  50. //This is a setting operation that determines the year
  51. public void setYear(int ny)
  52. {
  53. this.yy =ny;
  54. }
  55. /*
  56. These are the returning operations
  57. This is a returning operation that returns the day
  58. */
  59. public int getDay()
  60. {return this.dd;
  61. }
  62. //This is a returning operation that returns the month
  63. public int getMonth()
  64. {return this.mo;
  65. }
  66. //This is a returning operation that returns the year
  67. public int getYear()
  68. {return this.yy;
  69. }
  70. //This is a command that allows the programmer to print the values of the date object
  71. public String toString()
  72. {return("Day: "+this.dd+" Month: "+this.mo+" Year: "+this.yy);
  73. }
  74. /*
  75. This is a calcualting operation that returns the number of days passed from another date that is
  76. given as a parameter
  77. */
  78. public int difference(Date d2)
  79. {return((d2.getYear()-this.yy)*360+(d2.getMonth()-this.mo)*30+d2.getDay()-this.dd);
  80. }
  81. /*
  82. This is a calcualting operation that returns if a date is the same date
  83. as the one given as a parameter
  84. */
  85. public boolean equal(Date d3)
  86. {boolean equal=true;
  87. if(this.dd==d3.getDay()&&this.mo==d3.getMonth()&&this.yy==d3.getYear())
  88. {return equal;
  89. }
  90. else
  91. {equal = false;
  92. return equal;
  93. }
  94. }
  95. /*
  96. This is a calculating operation that returns if a date given as a parameter
  97. is after an original date
  98. */
  99. public boolean isAfter(Date d4)
  100. {boolean after=false;
  101. if(this.yy<d4.getYear())
  102. after = true;
  103. if(this.yy==d4.getYear())
  104. {if(d4.getMonth()>this.mo)
  105. after = true;
  106. if(d4.getMonth()==this.mo)
  107. {if(d4.getDay()>this.dd)
  108. after = true;
  109. if(d4.getDay()==this.dd)
  110. after = false;
  111. }
  112. }
  113. return after;
  114. }
  115.  
  116. }
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement