将滚动计数器编辑为 运行 其他程序

Editing Rollover Counter to Run Other Program

所以我正在尝试编辑一个文件中的代码以影响另一个文件。这可能是编码中的常见做法,但我是新手。所以我对 RolloverCounter 文件(第二个文件)进行了一些编辑,以便第一个可以 运行 正确,没有问题。我是不是把这个复杂化了还是遗漏了什么?任何帮助表示赞赏。提前致谢!每个文件中都有注释,可以进一步详细解释。

/*
 * 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;
    }
}

您的 RolloverCounter 需要一些更正。

调用increment()方法时,需要检查当前count是否小于max,如果为真则increment。如果不是,则重置为 0。

public void increment() {

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

    }

调用decrement()方法时,需要检查当前计数是否大于零,如果为真则减量。如果不是,重置为最大值。

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

调用 getCount() 时,您需要 return 当前计数。

public int getCount() {
        return count;
    }