* * In the RolloverCounter.java file you will implement a RolloverCounter class
ID: 3571972 • Letter: #
Question
* * In the RolloverCounter.java file you will implement a RolloverCounter class that should include:
1) A private variable to store the current count.
2) Another private variable to store the maximum value this counter can count up to.
3) A constructor that takes a single integer as an argument and uses it to set the maximum counter value. The current count should be set to 0.
4) An increment() method that increases the count value by 1, but sets it back to 0 if the count goes above the maximum. no arguments, returns nothing
5) A decrement() method that decreases the count value by 1, but sets it to the maximum value if it goes below 0. no arguments, returns nothing
6) A toString() method that returns a String representing the current count value. no arguments.
7) A reset() method that sets the count value back to 0. no arguments, returns nothing.
Notes: + This class is meant to be like the values on a car's odometer readout that tick upwards through the digits 0 to 9 and then roll back to 0 (but this counter could have a max different than
9) + The count should always be a number between 0 and the maximum value that was set when the counter was created.
+ The constructor should throw an exception if the argument for the maximum value is not a positive number (0 or less) */
//Code starts here
public class RolloverCounter {
}
}//end
The code should compile with this code
* a program that uses the RolloverCounter class to test its functionality
* You don't need to change anything in the program...it is just here to help you test your code in the RolloverCounter
* When this program is run the output should be: ************************************************* creating new counters... creating counter c1 with max value = 5... creating counter c2 with max value = 3... creating counter c3 with max value = -2 (this should not work)...
Error creating counter: java.lang.RuntimeException: invalid maximum value incrementing the counts 10 times and printing counts...
c1: 1 c2: 1 c1: 2 c2: 2 c1: 3 c2: 3 c1: 4 c2: 0 c1: 5
c2: 1 c1: 0 c2: 2 c1: 1 c2: 3 c1: 2 c2: 0 c1: 3 c2: 1 c1: 4 c2: 2
decrementing the counts 7 times and printint counts...
c1: 3 c2: 1 c1: 2 c2: 0 c1: 1 c2: 3 c1: 0
c2: 2 c1: 5 c2: 1 c1: 4 c2: 0 c1: 3 c2: 3
resetting counters... c1: 0 c2: 0 ***************************************************** */
public class CounterTest { public static void main(String args[]) {
System.out.println("creating new counters...");
System.out.println("creating counter c1 with max value = 5...");
RolloverCounter c1 = new RolloverCounter(5)
System.out.println("creating counter c2 with max value = 3...");
RolloverCounter c2 = new RolloverCounter(3);
try { System.out.println("creating counter c3 with max value = -2 (this should not work)...");
RolloverCounter c3 = new RolloverCounter(-2); } catch (Exception e) {
System.out.println("Error creating counter: " + e); }
System.out.println(" incrementing the counts 10 times and printing counts...");
for (int i=1; i<=10; i++) { c1.increment(); c2.increment(); System.out.println("c1: " + c1 + " c2: " + c2); }
System.out.println(" decrementing the counts 7 times and printint counts...");
for (int i=1; i<=7; i++) {
c1.decrement();
c2.decrement();
System.out.println("c1: " + c1 + " c2: " + c2); }
System.out.println(" resetting counters...");
c1.reset();
c2.reset();
System.out.println("c1: " + c1 + " c2: " + c2);
} //end main
} //end CounterTest
Explanation / Answer
//Code starts here
public class RolloverCounter {
private int curcount;
private int maxcount;
public RolloverCounter(int maxcount) {
if (maxcount < 0) throw new RuntimeException("invalid maximum value");
this.maxcount = maxcount;
curcount = 0;
}
public void increment() {
if (curcount >= maxcount) {
curcount = 0;
}
else {
curcount++;
}
}
public void decrement() {
if (curcount <= 0) {
curcount = maxcount;
}
else {
curcount--;
}
}
public String toString() {
return " " + curcount;
}
public void reset() {
curcount = 0;
}
}//end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.