Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Lab01;
- import java.awt.*;
- /**
- * The abstract class Counter implements a counter that will roll over to 0
- * when it hits the maximum value.
- *
- * @author Daniel Vournazos<--- your name goes here
- * @version 08/30/2011
- */
- public abstract class Counter //this is where the private member variables go aka to Private: in c++
- {
- // DECLARE INSTANCE VARIABLES HERE
- //you want to have these be created to emulate the UML given in the notes.
- private int Value;
- private int MaxValue;
- private boolean RolledOver;
- /* if i were to do this.Value it would refer to the value up here
- to remove ambiguity between int value below and int Value*/
- /**
- * The secondary constructor for objects of class Counter. The value and maxValue are given as parameters,
- * the rolledOver is false
- * Throws an exception if the value is greater than the maximum allowed
- */
- public Counter(int value, int maxValue) throws CounterInitializationException
- { // the int value and int maxValue are the parameters i have to put in the private variables
- // this public counter is the constructor for the abstract class counter.
- // ADD CODE FOR THE CONSTRUCTOR
- //I want to put value and max value from the constructor into the variables i created
- this.Value = value;
- this.MaxValue = maxValue;
- this.RolledOver = false;
- if(Value > MaxValue)
- { /* The point of an exception is to stop the program with an error and display an error message.
- To throw an exception, i have to tell it to throw a new exception using the exception object which
- in this case is called CounterInitializationException*/
- throw new CounterInitializationException("The value entered is outside the parameters, the world is blown up");
- }
- //this ends the constructor portion because it initialized the object&instance variables.
- }
- /**
- * Increases the value of the counter by one. Rolls over the value to 0 if needed.
- * Sets rolledOver boolean accordingly
- *
- * @return true if the roll over was performed
- */
- public boolean increment()
- {
- // YOUR CODE GOES HERE
- /*rolled over will be false for every increment until
- it rolls over then it becomes true, only to go back to false afterwards*/
- Value = Value++;
- RolledOver = false;
- if (Value > MaxValue)
- {
- Value = 0;
- RolledOver = true;
- }
- if (RolledOver == true) {return true;}
- else {return false;}
- }
- /**
- * Determine if two counters are in the same state
- *
- * @param other the Counter object to test against for equality
- * @return true if the objects are in the same state
- */
- public boolean equals(Counter other)
- {
- boolean Equals;
- Equals = false;
- //i want to compare the native object's value to an other object's value.
- if(this.getValue() == other.getValue())
- {
- Equals = true;
- }
- return Equals; // REPLACE WITH THE APPROPRIATE STATEMENT
- }
- /**
- * Accessor to get the value of the counter
- *
- * @return the current value of the counter
- */
- public int getValue()
- {
- // CHANGE THE RETURN TO GIVE THE CURRENT VALUE OF THE COUNTER
- // this is the accessor the accesses the instance variables and returns the value
- return this.Value;
- }
- /**
- * Accessor to get the value of the counter
- *
- * @return the current value of the counter
- */
- public int getMaxValue()
- {
- // CHANGE THE RETURN TO GIVE THE MAXIMUM VALUE OF THE COUNTER
- return this.MaxValue;
- }
- /**
- * Accessor that allows the client to determine if the counter
- * rolled over on the last count
- *
- * @return true if the counter rolled over
- */
- public boolean getRolledOver()
- {
- // CHANGE THE RETURN TO THE ROLLOVER STATUS OF THE COUNTER
- return this.RolledOver;
- }
- /**
- * Override the toString method to provide a more informative
- * description of the counter
- *
- * This is an abstract method. Each subclass must implement it.
- */
- public abstract String toString();
- }
Add Comment
Please, Sign In to add comment