Note that there are some explanatory texts on larger screens.

plurals
  1. POBeginner Java Counter Code
    text
    copied!<p>My Professor wants me to this: </p> <p>Write a number of interchangeable counters using the Counter interface below</p> <pre><code>public interface Counter { /** Current value of this counter. */ int value(); /** Increment this counter. */ void up(); /** Decrement this counter. */ void down(); } </code></pre> <p>Develop the following:</p> <p>An interface ResetableCounter that supports the message void reset() in addition to those of Counter.</p> <p>Here's what I did:</p> <pre><code>public interface ResetableCounter { void reset(); int value(); void up(); void down(); } </code></pre> <p>An implementation of ResetableCounter called BasicCounter that starts at the value 0 and counts up and down by +1 and -1 respectively.</p> <p>Here's what I did:</p> <pre><code>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; } } </code></pre> <p>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).</p> <p>Here's what I did:</p> <pre><code>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); } } </code></pre> <p>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.</p> <p>I haven't figured this out. </p> <p>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).</p> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload