初学Java计数器代码 [英] Beginner Java Counter Code

查看:137
本文介绍了初学Java计数器代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的教授要我这样:

使用下面的计数器界面写一些可互换的计数器

Write a number of interchangeable counters using the Counter interface below

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

开发以下内容:

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

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

这是我做的:

public interface ResetableCounter {
void reset();
int value();
void up();
void down();
}

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

An implementation of ResetableCounter called BasicCounter that starts at the value 0 and counts up and down by +1 and -1 respectively.

这是我做的:

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

一个名为SquareCounter的ResetableCounter实现,从值开始2,通过平方其当前值来计数,并通过取其当前值的平方根来计数(总是向上舍入,即1.7舍入为2,就像1.2舍入为2)。

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

这就是我的所作所为:

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

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

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.

我没有想到这一点。

你的所有实现都应该是可重置的,并且每个实现应该包含一个main方法,用于测试实现是否按照预期使用assert,就像我们在讲座中所做的那样(这很简单我们将在稍后讨论的单元测试方法。

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

我需要对我的工作发表评论。你觉得它有什么问题吗?我如何在可重置计数器上工作?我对JAVA非常新,而且自从C ++开始以来它已经很长了。

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.

推荐答案

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

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.

这篇关于初学Java计数器代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆