初级 Java 计数器代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18819066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:20:28  来源:igfitidea点击:

Beginner Java Counter Code

javacounter

提问by stburnish

My Professor wants me to this:

我的教授要我这样做:

Write a number of interchangeable counters using the Counter interface below

使用下面的 Counter 接口编写多个可互换的计数器

public interface Counter {
/** Current value of this counter. */
int value();
/** Increment this counter. */
void up();
/** Decrement this counter. */
void down();
}

Develop the following:

开发以下内容:

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

除了 Counter 之外,还支持消息 void reset() 的接口 ResetableCounter。

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.

一个称为 BasicCounter 的 ResetableCounter 实现,它从值 0 开始,分别向上和向下计数 +1 和 -1。

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

一个称为 SquareCounter 的 ResetableCounter 实现,它从值 2 开始,通过对其当前值进行平方来向上计数,并通过对其当前值的平方根进行计数(总是向上取整,即 1.7 被四舍五入为 2,就像 1.2 被四舍五入一样)到 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.

一种称为 FlexibleCounter 的 ResetableCounter 实现,它允许客户端在创建计数器时指定起始值以及附加增量(用于向上计数)。例如 new FlexibleCounter(-10, 3) 将产生一个当前值为 -10 的计数器;在调用 up() 后,它的值为 -7。

I haven't figured this out.

我还没有弄清楚这一点。

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

您的所有实现都应该是可重置的,并且每个实现都应该包含一个主要方法,该方法使用我们在讲座中所做的断言来测试实现是否按预期工作(这是一种简单的单元测试方法,我们将在后面详细讨论)。

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.

到目前为止,我需要对我的工作发表评论。你认为它足够吗?我如何在可重置计数器上工作?我对 Java 非常陌生,而且自从我使用 C++ 以来已经很久了。

回答by Ben

  1. Extending
    SquareCounter could extend ResetableCounter, wheras ResetableCounter could extend SimpleCounter. The benefit would be that you would not need to reimplement methods like reset();or value();.

  2. Thread-Safetiness
    If you intend to have your classes used in multiple occasions, you need to have those methods synchronized, or operate on an AtomicInteger. Otherwise you could have two threads up()ing your value at the same time, but have returned an old value in one case.

  3. Main Class
    Do not have your main class in a counter class. Have a seperate .javafile for that method.

  1. 扩展
    SquareCounter 可以扩展 ResetableCounter,而 ResetableCounter 可以扩展 SimpleCounter。好处是您不需要重新实现像reset();或 之类的方法value();

  2. 线程安全性
    如果您打算在多个场合使用您的类,您需要同步这些方法,或者在AtomicInteger. 否则,您可能有两个线程同时up()处理您的值,但在一种情况下返回了旧值。

  3. 主班
    不要在柜台班上有你的主班。.java该方法有一个单独的文件。

回答by BNeumann

What Ben said about extending is definitely a good idea, although the ResettableCounter interface can actually extend the Counter interface too and then reset() would be the only new method you need to declare in the ResettableCounter interface.

Ben 所说的扩展绝对是一个好主意,尽管 ResettableCounter 接口实际上也可以扩展 Counter 接口,然后 reset() 将是您需要在 ResettableCounter 接口中声明的唯一新方法。