首页 > 解决方案 > 尝试创建错误检查方法,以便当计数器为 0 时计数器不会递减

问题描述

我正在尝试为以下说明创建一个自定义 Counter 类和 CounterDemo 程序,但我不确定如何进行错误检查(尝试编写一个不起作用的名为 counterOK 的方法)以确保出现错误消息如果计数器尝试从零递减。我想我还需要编写一个将值重置为零的方法,就像错误消息说的那样。我不应该使用我写的 Counter 方法来重置它吗?

说明:在自己的文件中编写一个计数器类。这将允许您的计数器被任何程序使用。

计数器必须能够递增和递减一。它绝不能低于零。toString 和 equals 必须正确实现和测试。System.out.println("”+c1) 必须工作,并且 if(c1.equals(c2)) 必须工作,其中 c1 和 c2 是计数器的实例。包括对将计数器设置为零的默认构造函数的覆盖和允许您设置计数的构造函数。

编写一个测试程序来测试计数器的所有功能。在课程文档中查看我的 farkleberry 示例。

这个作业测试你对课文内容的理解。它还为未来的分配提供了模板。它旨在为您提供编写商业类的基本工具,这些商业类写在单独的文件中,并且始终包含 toString 和 equals。这也是程序员在发布类之前如何对其进行测试的一个例子。

public class Counter {

    private int count;  //local counting variable

    public Counter()
    {
        count = 0;
    }

    public void setCounter(int newCount)
    {
        count = newCount;
    }

    public int getCounter()
    {
        return count;
    }

    public void increment1 ()
    {
        count++;
    }

    public void decrement1 ()
    {
        count--;
    }

    public String toString()
    {
        return "" + count;
    }

    public boolean equals(Counter c)
    {
        boolean result = false;
        if (this.getCounter() == c.getCounter())
        {
            result = true;
            return result;
        }
        return result;
    }

    public boolean counterOK(int check)
    {
        boolean result = true;
        if (check > 0)
        {
            return result;
        }
            else
                return false;

    }
}

public class CounterDemo {

    public static void main(String[] args) {

        //initiate objects
        Counter c1 = new Counter(), c2 = new Counter();

        //show initial state of counters
        System.out.println("Initial state");
        System.out.println("Counter1 is at " + c1.toString());
        System.out.println("Counter2 is at " + c2.toString());
        if (c1.equals(c2))
            System.out.println("Counter1 equals Counter2.");
        else
            System.out.println("Counter1 does not equal Counter2");

        c1.decrement1();
        if (c1.counterOK(c1))
        {
            System.out.println();
        }
        else
            System.out.println("Error - attempted to subtract 1 Counter from 0 Counter.\n" + "Number of Counter reset to 0.");
        System.out.println(c1.toString());
    }
}

可能的示例输出:

This program creates and uses Counters

Initial state  
counter1 is at 0.  
counter2 is at 0.  
counter1 equals counter2.  

Error - Attempted to subtract 1 Counter from 0 Counter.  
Number of Counter reset to 0.  

State after first test  
counter1 is at 1.  
counter2 is at 0.  
counter1 does not equal counter2.  

State after second test  
counter1 is at 2.  
counter2 is at 2.  
counter1 equals counter2.  

Process completed.

标签: javaclassmethodserror-handling

解决方案


您不能通过检查简单地减少到零以下。

public void decrement1 () {
    if (count==0) {
        System.out.println("Error- Attempted to subtract 1 Counter from 0 Counter. Number of Counter reset to 0." );
        return;
    }
    count--;
}

通过阅读说明,我认为您不需要显示错误,只需处理错误并重置为零即可。

此外,您错过了要求此构造函数的说明部分:

public Counter(int initialCount) {
    if (initialCount < 0) {
        count = 0;
    } else {
        count = initialCount;
    }
}

推荐阅读