Guest User

Untitled

a guest
Oct 17th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. package Lab01;
  2.  
  3. import java.awt.*;
  4.  
  5. /**
  6. * The abstract class Counter implements a counter that will roll over to 0
  7. * when it hits the maximum value.
  8. *
  9. * @author Daniel Vournazos<--- your name goes here
  10. * @version 08/30/2011
  11. */
  12. public abstract class Counter //this is where the private member variables go aka to Private: in c++
  13. {
  14. // DECLARE INSTANCE VARIABLES HERE
  15.  
  16. //you want to have these be created to emulate the UML given in the notes.
  17. private int Value;
  18. private int MaxValue;
  19. private boolean RolledOver;
  20.  
  21. /* if i were to do this.Value it would refer to the value up here
  22. to remove ambiguity between int value below and int Value*/
  23.  
  24. /**
  25. * The secondary constructor for objects of class Counter. The value and maxValue are given as parameters,
  26. * the rolledOver is false
  27. * Throws an exception if the value is greater than the maximum allowed
  28. */
  29. public Counter(int value, int maxValue) throws CounterInitializationException
  30. { // the int value and int maxValue are the parameters i have to put in the private variables
  31. // this public counter is the constructor for the abstract class counter.
  32. // ADD CODE FOR THE CONSTRUCTOR
  33.  
  34. //I want to put value and max value from the constructor into the variables i created
  35.  
  36. this.Value = value;
  37. this.MaxValue = maxValue;
  38. this.RolledOver = false;
  39.  
  40. if(Value > MaxValue)
  41. { /* The point of an exception is to stop the program with an error and display an error message.
  42. To throw an exception, i have to tell it to throw a new exception using the exception object which
  43. in this case is called CounterInitializationException*/
  44. throw new CounterInitializationException("The value entered is outside the parameters, the world is blown up");
  45. }
  46.  
  47. //this ends the constructor portion because it initialized the object&instance variables.
  48.  
  49. }
  50.  
  51. /**
  52. * Increases the value of the counter by one. Rolls over the value to 0 if needed.
  53. * Sets rolledOver boolean accordingly
  54. *
  55. * @return true if the roll over was performed
  56. */
  57. public boolean increment()
  58. {
  59. // YOUR CODE GOES HERE
  60.  
  61. /*rolled over will be false for every increment until
  62. it rolls over then it becomes true, only to go back to false afterwards*/
  63.  
  64. Value = Value++;
  65. RolledOver = false;
  66. if (Value > MaxValue)
  67. {
  68. Value = 0;
  69. RolledOver = true;
  70. }
  71. if (RolledOver == true) {return true;}
  72. else {return false;}
  73.  
  74. }
  75.  
  76.  
  77. /**
  78. * Determine if two counters are in the same state
  79. *
  80. * @param other the Counter object to test against for equality
  81. * @return true if the objects are in the same state
  82. */
  83. public boolean equals(Counter other)
  84. {
  85. boolean Equals;
  86. Equals = false;
  87. //i want to compare the native object's value to an other object's value.
  88. if(this.getValue() == other.getValue())
  89. {
  90. Equals = true;
  91. }
  92. return Equals; // REPLACE WITH THE APPROPRIATE STATEMENT
  93. }
  94.  
  95. /**
  96. * Accessor to get the value of the counter
  97. *
  98. * @return the current value of the counter
  99. */
  100. public int getValue()
  101. {
  102. // CHANGE THE RETURN TO GIVE THE CURRENT VALUE OF THE COUNTER
  103. // this is the accessor the accesses the instance variables and returns the value
  104. return this.Value;
  105. }
  106.  
  107. /**
  108. * Accessor to get the value of the counter
  109. *
  110. * @return the current value of the counter
  111. */
  112. public int getMaxValue()
  113. {
  114. // CHANGE THE RETURN TO GIVE THE MAXIMUM VALUE OF THE COUNTER
  115. return this.MaxValue;
  116. }
  117.  
  118. /**
  119. * Accessor that allows the client to determine if the counter
  120. * rolled over on the last count
  121. *
  122. * @return true if the counter rolled over
  123. */
  124. public boolean getRolledOver()
  125. {
  126. // CHANGE THE RETURN TO THE ROLLOVER STATUS OF THE COUNTER
  127. return this.RolledOver;
  128. }
  129.  
  130. /**
  131. * Override the toString method to provide a more informative
  132. * description of the counter
  133. *
  134. * This is an abstract method. Each subclass must implement it.
  135. */
  136. public abstract String toString();
  137. }
Add Comment
Please, Sign In to add comment