Advertisement
Guest User

Untitled

a guest
Mar 30th, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1. public class HelloWorld{
  2.  
  3. public static void main(String[] args) {
  4. date d = new date(3, 6, 1973);
  5. d.print();
  6. date d1 = new date(3, 6, 1973);
  7. date d2 = new date(3, 24, 2015);
  8. date d3 = new date(1, 3, 2000);
  9. date d4 = new date(7, 4, 1973);
  10. date d5 = new date(3, 6, 1991);
  11. date current = new date();
  12. System.out.println(d1.ageAsOf(d2));
  13. System.out.println(d1.ageAsOf(d3));
  14. System.out.println(d1.ageAsOf(d4));
  15. System.out.println(d1.ageAsOf(d5));
  16.  
  17.  
  18. System.out.println(d1.compareTo(d2));
  19. System.out.println(d2.compareTo(d1));
  20. System.out.println(d1.compareTo(d1));
  21.  
  22.  
  23. date bday = new date(6, 23, 1998);
  24. Pet fifi = new Pet("Fifi", "cat", bday);
  25.  
  26.  
  27. }
  28. }
  29.  
  30.  
  31.  
  32. -----------------------------------------------------------------
  33.  
  34. import java.util.Calendar;
  35.  
  36. public class date {
  37. //properties
  38. private int year;
  39. private int month;
  40. private int day;
  41.  
  42. //constructors (part a)
  43. public date(int m, int d, int y){
  44. month = m;
  45. day = d;
  46. year = y;
  47. }
  48. public date(int m, int d){
  49. month = m;
  50. day = d;
  51. Calendar now = Calendar.getInstance();
  52. year = now.get(Calendar.YEAR);
  53. }
  54. public date(){
  55. Calendar now = Calendar.getInstance();
  56. year = now.get(Calendar.YEAR);
  57. month = now.get(Calendar.MONTH) + 1; //see note below
  58. day = now.get(Calendar.DATE);
  59. }
  60.  
  61. //getters and setters (part b)
  62. public int getYear(){
  63. return year;
  64. }
  65. public int getMonth(){
  66. return month;
  67. }
  68. public int getDay(){
  69. return day;
  70. }
  71. public void setYear(int y){
  72. y = year;
  73. }
  74. public void setMonth(int m){
  75. m = month;
  76. }
  77. public void setDay(int d){
  78. d = day;
  79. }
  80.  
  81. //other methods (parts c-e)
  82. public void print(){
  83. String[] mo = {"", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
  84. System.out.println(mo[month] + " " + day + ", " + year);
  85. }
  86. public int compareTo(date other){
  87. if(other.year > this.year){
  88. return -1;
  89. }
  90. if(other.year < this.year){
  91. return 1;
  92. }
  93. if(other.year == this.year){
  94. if(other.month > this.month){
  95. return -1;
  96. }
  97. if(other.month < this.month){
  98. return 1;
  99. }
  100. if(other.month == this.month){
  101. if(other.day > this.day){
  102. return -1;
  103. }
  104. if(other.month < this.month){
  105. return 1;
  106. }
  107. }
  108. }
  109. return 0;
  110. }
  111. public int ageAsOf(date d){
  112. if(d.month < this.month){
  113. return d.year-this.year-1;
  114. }
  115. if(d.month == this.month){
  116. if(d.day < this.day){
  117. return d.year-this.year-1;
  118. }
  119. if(d.day >= this.day){
  120. return d.year-this.year;
  121. }
  122. }
  123.  
  124.  
  125. if(d.month > this.month){
  126. return d.year - this.year;
  127.  
  128. }
  129. return d.year - this.year;
  130. }
  131. public int ageAsOf(date current){
  132. if(current.month < this.month){
  133. return current.year-this.year-1;
  134. }
  135. if(current.month == this.month){
  136. if(current.day < this.day){
  137. return current.year-this.year-1;
  138. }
  139. if(current.day >= this.day){
  140. return current.year-this.year;
  141. }
  142. }
  143.  
  144.  
  145. if(current.month > this.month){
  146. return current.year - this.year;
  147.  
  148. }
  149. return current.year - this.year;
  150. }
  151. }
  152.  
  153.  
  154.  
  155. -------------------------------------------------------------------
  156.  
  157. public class Pet {
  158. private String name;
  159. private String type;
  160. private date birthday; //see, I told you!
  161.  
  162. public Pet(String n, String t, date bd){
  163. n = name;
  164. t = type;
  165. bd = birthday;
  166. }
  167. public Pet(String n, String t, int month, int day, int year){
  168. this(n, t, new date(month, day, year));
  169. }
  170. //getters and setters (part b)
  171. public String getName(){
  172. return name;
  173. }
  174.  
  175. public String getType(){
  176. return type;
  177. }
  178.  
  179. public date getBirthday(){
  180. return birthday;
  181. }
  182.  
  183. public void setName(String n){
  184. name = n;
  185. }
  186. //other methods (parts c and d)
  187. public boolean isBirthday() {
  188. date current = new date();
  189. return birthday.getMonth() == current.getMonth() && birthday.getDay() == current.getDay();
  190. }
  191. public int getAge(){
  192. return birthday.ageAsOf(currentDate);
  193. }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement