编辑翻转计数器以运行其他程序

问题描述 投票:0回答:1

因此,我试图在一个文件中编辑代码以影响另一个文件。这可能是编码中的常见做法,但我是新手。因此,我对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;
    }
}
java rollover
1个回答
0
投票

您的RolloverCounter需要更正。

当调用crement()方法时,您需要检查当前计数是否小于最大值,如果为true,则递增。如果没有,请重置为0。

public void increment() {

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

    }

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

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

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

public int getCount() {
        return count;
    }
© www.soinside.com 2019 - 2024. All rights reserved.