Reputation: 43
My Professor wants me to this:
Write a number of interchangeable counters using the Counter interface below
public interface Counter {
/** Current value of this counter. */
int value();
/** Increment this counter. */
void up();
/** Decrement this counter. */
void down();
}
Develop the following:
An interface ResetableCounter that supports the message void reset() in addition to those of Counter.
Here's what I did:
public interface ResetableCounter {
void reset();
int value();
void up();
void down();
}
An implementation of ResetableCounter called BasicCounter that starts at the value 0 and counts up and down by +1 and -1 respectively.
Here's what I did:
public class BasicCounter implements ResetableCounter
{
int counterVariable = 0;
public static void main(String[] args)
{
BasicCounter cnt = new BasicCounter();
cnt.up();
cnt.down();
System.out.printf("The value is %d", cnt.counterVariable);
}
public void reset() {
this.counterVariable = 0;
}
public int value() {
return this.counterVariable;
}
public void up() {
++this.counterVariable;
}
public void down() {
--this.counterVariable;
}
}
An implementation of ResetableCounter called SquareCounter that starts at the value 2, counts up by squaring its current value, and counts down by taking the square root of its current value (always rounding up, i.e. 1.7 is rounded to 2, just like 1.2 is rounded to 2).
Here's what I did:
public class SquareCounter implements ResetableCounter {
int counterVariable = 2;
public static void main(String[] args) {
SquareCounter cnt = new SquareCounter();
cnt.up();
cnt.down();
double d = Math.ceil(cnt.counterVariable);
System.out.printf("The value is %f", d);
}
public void reset() {
this.counterVariable = 0;
}
public int value() {
return this.counterVariable;
}
public void up() {
Math.pow(this.counterVariable, 2);
}
public void down() {
Math.sqrt(this.counterVariable);
}
}
An implementation of ResetableCounter called FlexibleCounter that allows clients to specify a start value as well as an additive increment (used for counting up) when a counter is created. For example new FlexibleCounter(-10, 3) would yield a counter with the current value -10; after a call to up() its value would be -7.
I haven't figured this out.
All of your implementations should be resetable, and each should contain a main method that tests whether the implementation works as expected using assert as we did in lecture (this is a simple approach to unit testing which we'll talk about more later).
I NEED COMMENTS ON MY WORK SO FAR. DO YOU THINK IT SUFFICES? HOW DO I WORK ON THE RESETABLE COUNTER? I'M VERY NEW TO JAVA AND IT'S BEEN LONG SINCE I DID C++ ANYWAY.
Upvotes: 4
Views: 26211
Reputation: 76
What Ben said about extending is definitely a good idea, although the ResettableCounter interface can actually extend the Counter interface too and then reset() would be the only new method you need to declare in the ResettableCounter interface.
Upvotes: 1
Reputation: 1246
Extending
SquareCounter could extend ResetableCounter, wheras ResetableCounter could extend SimpleCounter. The benefit would be that you would not need to reimplement methods like reset();
or value();
.
Thread-Safetiness
If you intend to have your classes used in multiple occasions, you need to have those methods synchronized, or operate on an AtomicInteger
. Otherwise you could have two threads up()
ing your value at the same time, but have returned an old value in one case.
Main Class
Do not have your main class in a counter class. Have a seperate .java
file for that method.
Upvotes: 1