使用 Counter 类来递增和递减:java
我无法想象这个问题的最终结果:
定义一个名为 Counter 的类,其对象对事物进行计数。这个类的一个对象 记录一个非负整数的计数。包括设置计数器的方法 为 0,将计数加 1,将计数减 1。确保没有 方法允许计数器的值变为负数。包括一个访问器 返回当前计数值的方法和输出 数到屏幕上。不应该有输入法或其他变异方法。 可以设置计数器的唯一方法是将其设置为零。还, 包括一个 toString 方法和一个 equals 方法。编写一个程序(或多个程序) 测试类定义中的所有方法。
有人可以更好地为我解释这个问题吗?
I'm having trouble visualizing the end product of this problem:
Define a class called Counter whose objects count things. An object of this class
records a count that is a nonnegative integer. Include methods to set the counter
to 0, to increase the count by 1, and to decrease the count by 1. Be sure that no
method allows the value of the counter to become negative. Include an accessor
method that returns the current count value and a method that outputs the
count to the screen. There should be no input method or other mutator methods.
The only method that can set the counter is the one that sets it to zero. Also,
include a toString method and an equals method. Write a program (or programs)
to test all the methods in your class definition.
Can someone better explain this problem for me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编写定义类的代码。类的名称应为
Counter
。 (想一想:这对文件名意味着什么?请记住,我们希望它成为一个public
类,以便我们可以使用它进行测试。)Counter
用于计算事物(不足为奇)。为此,该类的每个实例都将包含一个“计数”值。 (想一想:这个值的好名字是什么?我们如何为属于实例的类的每个实例创建一个单独的值?)该值不应为负。 (显然;你怎么可能有负数?)你应该确保该值不会变成负数(如果你以远程正常方式做事,这应该自动发生)。
该类应提供以下功能:
将计数值设置为 0 的方法。(思考:该方法的一个好名称是什么?当然应该是
public
,因为它是其他代码将如何使用该类的一部分。思考:这个方法需要任何参数吗?如果需要,什么?)将计数增加 1 的方法。(思考:如上所述。 )
一种将计数减少 1 的方法。(思考:如上所述。另外,如果计数已经为 0,会发生什么情况,鉴于我们决不能让它变得消极?在这种情况下该怎么做。)
返回当前计数值的方法。 (这称为“访问器”)。
显示当前计数值的方法。 (注意:从设计角度来看,这是一个非常糟糕的想法,但您的作业需要它。提示:使用
System.out
的方法之一输出到屏幕。)一种名为
toString
返回计数器的字符串表示形式。这不应该接受任何参数,并且必须返回一个字符串。 (这是 Java 中大多数对象提供的标准功能。想一想:除了计数值之外,字符串中还应该包含其他内容吗?)名为
equals
将 Counter 与另一个 Counter 进行比较以确定是否相等。此方法必须仅接受一个参数(一个 Counter 实例),并返回一个布尔值,指示两个 Counter 是否相等。 (思考:什么决定两个计数器是否“相等”?)该类不应提供以下任何功能:
编写一个程序来创建一些
Counter
实例并使用它们的方法。该程序应该通过生成与您阅读主程序所期望的结果一致的输出来证明您的Counter
类的行为符合预期。 (提示:由于这是 Java,因此我们需要为该程序提供另一个类,并带有public static void main(String[] args)
方法。将其放在另一个文件中。您可能需要尝试一下使用package
和import
语句,以便主程序可以在其他文件中找到您的Counter
类。确保您了解如何编译和运行。 Java 类。)Write code that defines a class. The class' name shall be
Counter
. (Think: what does this imply about the file name? Keep in mind that we will want this to be apublic
class so that we can use it for testing.) ACounter
is used to count things (not surprising).To do this, each instance of the class will contain a "count" value. (Think: what would be a good name for this value? How do we make a separate value for each instance of a class, that belongs to the instances?) The value shall not be negative. (Obvious; how could you have a negative number of things?) You are expected to make sure that the value cannot become negative (this should happen automatically, if you are doing things in remotely normal ways).
The class should provide the following functionality:
A method that sets the count value to 0. (Think: what is a good name for this method? It should be
public
of course, since it is part of how other code will use the class. Think: do you need any parameters for this method? If so, what?)A method that increases the count by 1. (Think: as above.)
A method that decreases the count by 1. (Think: as above. Also, what happens if the count is already 0, given that we must not let it become negative? Decide what to do in this case.)
A method that returns the current count value. (This is called an "accessor").
A method that displays the current count value. (Note: this is a very bad idea design-wise, but your assignment requires it. Hint: use one of the methods of
System.out
to output to the screen.)A method called
toString
that returns a String representation of the counter. This should not take any parameters, and must return a String. (This is a standard piece of functionality that most objects in Java provide. Think: other than the count value, is there anything else that ought to be included in the string?)A method called
equals
that compares the Counter to another Counter for equality. This method must take exactly one parameter, a Counter instance, and return aboolean
indicating whether the two Counters are equal. (Think: what determines whether two counters are "equal"?)The class should NOT provide any of the following functionality:
Write a program that creates some
Counter
instances and uses their methods. The program should prove that yourCounter
class behaves as expected, by producing output that agrees with what you'd expect from reading the main program. (Hint: since this is Java, we will need another class for this program, with apublic static void main(String[] args)
method. Put it in another file. You may need to play around withpackage
andimport
statements so that the main program can find yourCounter
class in the other file. Make sure you understand how to compile and run Java classes.)最终产品是一个具有问题中描述的方法的类。您可能还应该提供一个测试程序来验证该程序是否按预期使用一些示例数据工作。
The end product is a class with the methods described in the problem. You should probably also supply a test program to verify that the program works as expected with some example data.