Java 创建一个狗类和测试器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20053874/
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
Create a dog class and tester
提问by user3005572
Hello I was asked to create the following:
Create a Dog class that keeps track of a dog's
你好,我被要求创建以下内容:
创建一个跟踪狗的狗类
name
age
- Weight
姓名
年龄
- 重量
Have the following methods available to use:
有以下方法可供使用:
1. getName(): String
2. setName(String): void
3. getAge():int
4. getDogYears():int (assume 1 human year = 5 dog years)
5. addAYear():void
6. setAge(int):void
7. getWeight(): double
8. setWeight(double): void
9. getDogData():String
Create a DogTester class to test each of the methods above
创建一个 DogTester 类来测试上面的每个方法
I am following a template of this done before but I am getting confused, this is what I have so far Dog class:
我正在遵循之前完成的模板,但我感到困惑,这是我迄今为止的 Dog 课程:
public class Dog
{
//Instance vars
private String name;
private int age;
private double weight;
//constructor
public Dog(String initName)
{
name = initName;
age = 0;
weight = 0;
}
//methods
public String getName()
{
return name;
}
public void setName()
{
return name;
}
public int getAge()
{
return age;
}
public int getDogYears()
{
return age*5;
}
public void addAYear()
{
age = age + 1;
}
public void setAge()
{
return age;
}
public double getWeight()
{
return weight;
}
public void setWeight()
{
weight = weight;
}
public String getData()
{
String data = "";
data = name;
data = data + "\n\tAge: " + age;
data = data + "\n\tWeight: " + weight;
return data;
}
}
Tester:
测试员:
import java.util.Scanner;
public class DogTester
{
public static void main(String[] args)
{
Scanner in = new Scanner( System.in );
//Create two dogs to keep track of
Dog s1 = new Dog("Balto");
//get the dogs age
System.out.print("Enter the dog's age: "); //prompt
double grade = in.nextDouble(); //read
s1.addAge( years ); //store
//get the dogs weight
System.out.print("Enter the dog's weight: "); //prompt
s1.addWeight( in.nextDouble() ); //read AND store
//output their data
System.out.println( s1.getData() );
}
}
I am confused as what it would want from the public void constructors and also as to how to set up a tester for these
我对 public void 构造函数想要什么以及如何为这些设置测试器感到困惑
回答by Jason C
A constructor is not "void", nor does it really "return" anything. When you do:
构造函数不是“void”,也不是真正“返回”任何东西。当你这样做时:
new Dog("Balto");
This allocates space for a Dog
object and calls the constructor to initialize it.
这为Dog
对象分配空间并调用构造函数来初始化它。
To test the constructor, all you really need to do is test to make sure that the constructor is behaving as it should. Define some clear rules for the state the constructor needs to leave a Dog
in (these are commonly called "postconditions"). In your case, looking at the code, it seems that for a new Dog
:
要测试构造函数,您真正需要做的就是测试以确保构造函数的行为正常。为构造函数需要保留的状态定义一些明确的规则Dog
(这些通常称为“后置条件”)。在您的情况下,查看代码,似乎对于 new Dog
:
- The name must be set to the parameter passed to the constructor.
- The age and weight are initialized to 0.
- 该名称必须设置为传递给构造函数的参数。
- 年龄和体重初始化为0。
So all you need to do then is, after constructing a new Dog
, verify that those conditions are true. Get the dogs name, see if it is the expected value. Get the age and weight, make sure they are zero. If so, then your constructor stuck to the "contract" you defined, and your test passed.
所以你需要做的就是,在构造一个 new 之后Dog
,验证这些条件是否为真。获取狗的名字,看看是否是期望值。获取年龄和体重,确保它们为零。如果是这样,那么您的构造函数坚持您定义的“合同”,并且您的测试通过了。
回答by DanK
Your public statement simply states that those methods can be accessed from outside the class. If on the other hand you did not want anyone to change a dog's weight after initially constructing the dog, you would set it to private.
您的 public 声明只是声明可以从类外部访问这些方法。另一方面,如果您在最初构建狗后不希望任何人改变狗的体重,您可以将其设置为私有。
You only have one constructor here, the other statements are methods to change your instance variables:
这里只有一个构造函数,其他语句是更改实例变量的方法:
public Dog(String initName)
{
name = initName;
age = 0;
weight = 0;
}
To test you would need to create a dog and then run some assert statements against it to verify that your methods are doing what you would expect them to:
要进行测试,您需要创建一条狗,然后针对它运行一些断言语句,以验证您的方法正在执行您期望它们执行的操作:
Dog d = new Dog("Pooch");
assertEquals("The name is incorrect", "Pooch", d.getName());