我正在编写一个方法,一旦调用该方法,就会在mSize
数组中向上移动一个空格(通过meals
变量计算)。
class Dog {
String mSize[] = {"tiny", "small", "average", "large"};
int dogSize;
int meals;
final float WEIGHT_GAIN = 0.25f;
float mWeight;
/*
* feed
*
* Feeds the Dog.
*
* Side-effect: 1. The Dog gains weight, specifically WEIGHT_GAIN
* 2. Every 3 meals, the Dog grows to a larger size, if
* possible
* i.e. "tiny" (3 meals later ->) "small" (3 meals later ->)
* "average" (3 meals later ->) "large"
* the Dog cannot exceed the "large" size or shrink smaller than
* "tiny"
* @return nothing
*/
void feed(){
mWeight += WEIGHT_GAIN;
if (++meals == 3 && getSize()!= mSize[mSize.length-1]){
dogSize ++;
getSize();
}
}
编辑:
meals
必须是成员变量,而我没有使用getSize
方法,该方法返回mSize[dogSize]
不确定我是否应该在某处使用setSize
/*
* getSize
*
* @return the size of the dog (String)
*/
/************************************************
* ASSIGNMENT:
* Create the getSize method
/************************************************/
String getSize(){
return mSize[dogSize];
}
/*
* setSize
*
* Sets the size of the Dog
*
* @param size the new size of the Dog (String)
* @return nothing
*/
/************************************************
* ASSIGNMENT:
* Create the setSize method
/************************************************/
void setSize(String size){
mSize[dogSize] = size;
}
主要方法测试:
// Test feed
try {
float weight = (Float) getWeight.invoke(dog, new Object[0]);
feed.invoke(dog, new Object[0]);
if (((Float) getWeight.invoke(dog, new Object[0])) <= weight) {
System.out.println("Your Dog should have gained some weight");
System.exit(1);
}
feed.invoke(dog, new Object[0]);
feed.invoke(dog, new Object[0]);
if (!"average".equals(getSize.invoke(dog, new Object[0]))) {
System.out.println("Your Dog didn't grow to average after 3 meals :(");
System.exit(1);
}
feed.invoke(dog, new Object[0]);
feed.invoke(dog, new Object[0]);
feed.invoke(dog, new Object[0]);
if (!"large".equals(getSize.invoke(dog, new Object[0]))) {
System.out.println("Your Dog didn't grow to large after 3 more meals :(");
System.exit(1);
}
feed.invoke(dog, new Object[0]);
feed.invoke(dog, new Object[0]);
feed.invoke(dog, new Object[0]);
if (!"large".equals(getSize.invoke(dog, new Object[0]))) {
System.out.println("Your Dog should stay large if it keeps feeding like this");
System.exit(1);
}
} catch (Exception e) {
System.out.println("Something went wrong with " + feed.getName());
System.exit(1);
}
答案 0 :(得分:1)
我相信你想要像
这样的东西class Dog {
static String mSize[] = { "tiny", "small", "average", "large" };
int dogSize = 0;
int meals = 0;
void feed() {
if (++meals % 3 == 0) {
dogSize++;
}
}
@Override
public String toString() {
return String.format("Weight = %.2f, Meals = %d, Size = %s",
meals / 3.0, meals, mSize[dogSize]);
}
}
然后测试它,比如
public static void main(String[] args) {
Dog d = new Dog();
for (int i = 0; i < 12; i++) {
System.out.println(d);
d.feed();
}
}
输出
Weight = 0.00, Meals = 0, Size = tiny
Weight = 0.33, Meals = 1, Size = tiny
Weight = 0.67, Meals = 2, Size = tiny
Weight = 1.00, Meals = 3, Size = small
Weight = 1.33, Meals = 4, Size = small
Weight = 1.67, Meals = 5, Size = small
Weight = 2.00, Meals = 6, Size = average
Weight = 2.33, Meals = 7, Size = average
Weight = 2.67, Meals = 8, Size = average
Weight = 3.00, Meals = 9, Size = large
Weight = 3.33, Meals = 10, Size = large
Weight = 3.67, Meals = 11, Size = large