2
\$\begingroup\$

My professor wants me to do 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();
}

I need comments on my work so far. Do you think it's sufficient? How do I work on the ResetableCounter? I'm very new to Java and it's been a long time since I've done C++.

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.

public class FlexibleCounter implements ResetableCounter
{
public static void main(String[] args)
{
    int start = Integer.parseInt(args[0]);
    int step = Integer.parseInt(args[1]);
    start.up();
    System.out.printf("The value is %d", count); 
}

public void reset() {
    this.count = 0;
}

public int value() {
    return this.count;
}

public void up() {
    this.count = args[0] + args[1];
}

public void down() {
    --this.count;
}
}

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).

\$\endgroup\$
1
\$\begingroup\$

Your BasicCounter is impeccable. (The indentation of the braces is a bit off, but maybe that's an artifact from pasting into this site.) I would suggest renaming counterVariable to just count, since it would be silly to name everything fooVariable and barVariable.

Your SquareCounter is buggy. Think about what its reset() should do. (Hint: it would be a good idea for the SquareCounter() constructor to call reset().) Also, in up() and down(), how is counterVariable being modified? (Hint: right now, it isn't being modified.) Note that Math.pow() and Math.sqrt() return double rather than int, so you'll have to cast to int at some point.

Unfortunately, by the rules of this website, we only improve code that has already been written. Therefore, I'll decline to comment on FlexibleCounter until you present something that at least compiles.

\$\endgroup\$
  • \$\begingroup\$ public class FlexibleCounter implements ResetableCounter { public static void main(String[] args) { int start = Integer.parseInt(args[0]); int step = Integer.parseInt(args[1]); start.up(); System.out.printf("The value is %d", count); } public void reset() { this.count = 0; } public int value() { return this.count; } public void up() { this.count = args[0] + args[1]; } public void down() { --this.count; } } \$\endgroup\$ – stburnish Sep 17 '13 at 0:01
  • \$\begingroup\$ what do you think of the FlexibleCounter I just added? \$\endgroup\$ – stburnish Sep 17 '13 at 1:40
  • 1
    \$\begingroup\$ It matters not what I think. The compiler doesn't like it. \$\endgroup\$ – 200_success Sep 17 '13 at 6:40
  • \$\begingroup\$ I know. How do I fix it? \$\endgroup\$ – stburnish Sep 17 '13 at 22:14
  • \$\begingroup\$ Sorry, that's out of scope for this website, where we only improve working code. We also don't hand out homework answers. I'm going to end this discussion on the following two hints. 1) start is an int; you can't call .up() on it. Should you construct an object? 2) args is not available within the scope of the up() method. What instance variables should you use instead? \$\endgroup\$ – 200_success Sep 17 '13 at 23:29
2
\$\begingroup\$

An interface ResetableCounter that supports the message void reset() in addition to those of Counter.

Your professor provides this statement to you as a hint. It is practically saying ResetableCounter should extends Counter interface.

make countVariable or count(as @200_success suggested) private. As no other outside class should have any permission to change the count.

\$\endgroup\$
1
\$\begingroup\$

I would suggest three changes:

  • All the advanced counter classes, SquareCounter, FlexibleCount, ResettableCounter should extend the Counter interface.
  • Do not add the main() method inside the counters. Instead , have a seperate class say Driver which creates and calls methods on the counters.
  • The counter variable should be private.
  • In your MathCounter, when calling Math.pow() or Math.sqrt() , please assign these value to the counter variable. E.g: counter= Math.pow();

  • e. In your flexible counter down instead of using --counter, you should be doing : counter=counter-step

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.