Search code examples
javarollover

Editing Rollover Counter to Run Other Program


So I'm trying to edit a code in one file to affect another. This is probably a common practice in coding but I'm new to it. So I've made some edits to the RolloverCounter File (the second file), so that the first one can run correctly, with no problems. Am I over complicating this or missing something? Any help is appreciated. Thanks in advance! There are notes in each file to explain it further in detail.

/*
 * 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... 

 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 printing 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);

    System.out.println("\nincrementing the counts 10 times and printing counts...");
    for (int i=1; i<=10; i++) {
      c1.increment();
      c2.increment();
      System.out.println("c1: " + c1.getCount() + "\tc2: " + c2.getCount());
    }

    System.out.println("\ndecrementing the counts 7 times and printing counts...");
    for (int i=1; i<=7; i++) {
      c1.decrement();
      c2.decrement();
      System.out.println("c1: " + c1.getCount() + "\tc2: " + c2.getCount());
    }

    System.out.println("\nresetting counters...");
    c1.reset();
    c2.reset();
    System.out.println("c1: " + c1.getCount() + "\tc2: " + c2.getCount());

  } //end main
} //end CounterTest

/*
* Write the code for the RolloverCounter class below
* 
* In this 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 with a single integer parameter used to set the maximum counter value.  The 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 parameters, 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 parameters, returns nothing
6) A getCount() method that returns an integer of the current count value.  no parameters.
7) A reset() method that sets the count value back to 0.  no parameters, 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. In this case, the maximum can be any positive number.
+ The count should always be a number between 0 and the maximum value that was set when the counter was created.
*/

public class RolloverCounter {
  //TODO: Write the code for the class here.

  //private variables
 private int count;
 private int max;

  //constructor
 public RolloverCounter(int maxCount) {
   count = 0;
   max = maxCount; 
 }  
  //methods
  // increases the count value by 1, but sets it back to 0 if the count goes above the maximum. 
    // returns nothing

    public void increment() {
        for (int i=1; i<=4; i++) {
          count = count + 1;
        }
    }

    // decreases the count value by 1, but sets it to the maximum value if it goes below 0. 
    // returns nothing
    public void decrement() {
        for (int i=1; i<=4; i++) {
          count = count - 1;
        }
    }

    // returns an integer of the current count value.
    public void getCount() {
      count = (count + 1) % max;
    }

    // sets the count value back to 0.
    // returns nothing.
    public void reset() {
        count=0;
    }
}

Solution

  • Your RolloverCounter needs some corrections.

    When increment() method is called, you need to check if the current count is less than max and increment if true. If not, reset to 0.

    public void increment() {
    
            if(count < max) {
                count++;
            }else {
                count = 0;
            }
    
        }
    

    When decrement() method is called, you need to check if the current count is greater than zero and decrement if true. If not, reset to max value.

    public void decrement() {
            if(count > 0) {
                count--;
            }else {
                count = max;
            }
        }
    

    When getCount() is called, you need to return the current count.

    public int getCount() {
            return count;
        }