Advertisement
Guest User

Tree.java

a guest
Sep 19th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. package lab01;
  2.  
  3. /**
  4. * Tree Class
  5. * Models a tree
  6. * @author Chad Humphries
  7. * @version Lab01
  8. * Date Created: Sept 14, 2019
  9. */
  10. public class Tree
  11. {
  12. //Constants
  13. public static final String DEFAULT_TYPE = "n/a";
  14. public static final int JANUARY = 1;
  15. public static final int DECEMBER = 12;
  16. public static final int CURRENT_YEAR = 2019;
  17.  
  18. //Variables
  19. private String type = DEFAULT_TYPE;
  20. private int month = JANUARY;
  21. private int year = CURRENT_YEAR;
  22.  
  23. /**
  24. * Default Constructor
  25. */
  26. public Tree()
  27. {
  28. }
  29.  
  30. /**
  31. * Overloaded Constructor
  32. * @param type Type of tree
  33. * @param month Month planted
  34. * @param year Year planted
  35. */
  36. public Tree(String type, int month, int year)
  37. {
  38. setType(type);
  39. setMonth(month);
  40. setYear(year);
  41. }
  42.  
  43. /**
  44. * Mutator for tree type
  45. * @param type Type of tree
  46. * If type is null, DEFAULT_TYPE will be assigned
  47. */
  48. public void setType(String type)
  49. {
  50. if(type != null && type.trim().length() > 0)
  51. {
  52. this.type = type;
  53. }
  54. else
  55. {
  56. this.type = DEFAULT_TYPE;
  57. }
  58. }
  59.  
  60. /**
  61. * Mutator for month planted
  62. * @param month Month planted
  63. * If month is less than JANUARY or greater than DECEMBER, JANUARY will be assigned
  64. */
  65. public void setMonth(int month)
  66. {
  67. if(month >= JANUARY && month <= DECEMBER)
  68. {
  69. this.month = month;
  70. }
  71. else
  72. {
  73. this.month = JANUARY;
  74. }
  75. }
  76.  
  77. /**
  78. * Mutator for year planted
  79. * @param year Year planted
  80. * if year is negative or greater than CURRENT_YEAR, CURRENT_YEAR will be assigned
  81. */
  82. public void setYear(int year)
  83. {
  84. if(year >= 0 && year <= CURRENT_YEAR)
  85. {
  86. this.year = year;
  87. }
  88. else
  89. {
  90. this.year = CURRENT_YEAR;
  91. }
  92. }
  93.  
  94. /**
  95. * Accessor for type
  96. * @return Type of tree
  97. */
  98. public String getType()
  99. {
  100. return type;
  101. }
  102.  
  103. /**
  104. * Accessor for month
  105. * @return Month planted
  106. */
  107. public int getMonth()
  108. {
  109. return month;
  110. }
  111.  
  112. /**
  113. * Accessor for year
  114. * @return Year planted
  115. */
  116. public int getYear()
  117. {
  118. return year;
  119. }
  120.  
  121. /**
  122. * Prints details about tree
  123. */
  124. public void showDetails()
  125. {
  126. System.out.println("Tree Info: " +
  127. "\nType: " + getType() +
  128. "\nMonth planted: " + getMonth() +
  129. "\nYear planted: " + getYear() +
  130. "\n");
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement