【发布时间】:2014-11-18 23:30:54
【问题描述】:
我有一个填充界面,它将获取当前人口、设置人口和增加人口。我有一个将我的行星设置在数组列表中的主节点,所以现在我需要做的是实现代码,该代码将帮助我根据该行星方法增加该行星的人口。这是人口界面。
/**
* This interface models the behavior of planets when travelers arrive
* and when they try to leave.
* @author
*
*/
public interface Populate {
/**
* Get the current population of the planet
* @return the current population of the planet
* @throws Exception if the value of the currentPopulation is none of your business
*/
public int getCurrentPopulation() throws Exception;
/**
* Initialize the current population to a value
* @param currentPopulation The value to initialize to
* @return The new value of the current population, will be currentPopulation
* @throws Exception if the value of the currentPopulation argument is negative
*/
public int setCurrentPopulation(int currentPopulation) throws Exception;
/***
* Increase the population of the planet as travelers arrive.
* @param populationIncrease The amount to increase the current population. Can be negative.
* @return The new current population
*/
public int IncreasePopulation(int populationIncrease);
}
这里是主要的:
public class Main {
public static void main(String[] args) {
//this class will run calculations for planets
ArrayList<WorldOfAdams> myPlanets = new ArrayList<WorldOfAdams>();
myPlanets.add(new AllosimaniusSyneca());
myPlanets.add(new BlagulonKappa());
myPlanets.add(new Damogran());
myPlanets.add(new Traal());
//Get current population for each of the planets
for (int i=0; i<myPlanets.size(); i++) {
myPlanets.get(i).getCurrentPopulation();
每个星球都有自己的一套指令。有 Allosimanius Syneca、Blagulon Kappa、Damogra 和 Traal。
Allosimanius Syneca 的说明
/**
* This class models the planet Allosimanius Syneca. On this planet travelers are not
*welcome. Anyone landing on the planet at any of the 3 spaceports is immediately put
*to work in the cinnamon mines. No one is ever allowed to leave.
*/
根据这个星球的方法,当前人口似乎将被设置为 0,因为没有人被允许访问这个星球,但在增加的人口中,我需要反映存在增加,但旅行者将直接前往肉桂矿。我该如何实施?我知道我需要创建将增加人口的代码,然后打印显示人口增加“进入肉桂矿”的声明。我要把这段代码放在main中吗?
Blagulon Kappa 的说明:
/***
* All travelers who arrive on this planet are treated with kindness. They may stay as
* long as they like and leave whenever they like. However, they will not reveal the
*current population to anyone. All inquiries are ignored.
根据这些信息,我需要打印一份声明,说明人口可能会增加,但产出“不关他们的事”。这个星球会不会有一个简单的打印语句并且没有代码来增加人口,因为不允许知道输出?
达莫格兰的说明:
/***
* Damogran is a mostly peaceful planet that has completely run out of livable land.
*They do not allow anyone to visit the planet or leave it. Ever.
所以对于这个星球,我会将当前人口设置为 0,并且人口不会增加。
最后是 Traal 的说明:
/***
* 10% of the travelers arriving on this planet are immediately fed to the Ravenous
* Bugblatter Beast. Survivors are allowed to come and go as they wish.
对于这个星球,我需要增加人口,但除以 10% 并打印一份声明,其中包含“喂给 Bugblatter Beast”的剩余数字
刚刚学习如何使用接口和抽象类,很难理解如何实现所有这些类以一起运行并能够给出不同的结果。非常感谢您提供的任何帮助或指导!
【问题讨论】:
-
您在什么变量中存储人口?为什么不增加那个变量呢?
-
检查你的超类
WorldOfAdams,它可能有一个变量来存储currentPopulation。你会想要增加那个变量,可能是currentPopulation += populationIncrease -
增加值的能力意味着您正在存储所述值。添加一个类成员来存储人口。将增加的数量添加到存储的人口中。
-
正如 Miche 所说,您确实没有提供足够的信息来充分回答您。
-
@NewbieGirl Miche 要求你提供作业全文的原因是这里没有足够的信息可以提供帮助。有一小段代码没有上下文,没有超类来帮助我们确定人口是如何存储的,等等。不要让人们猜测,如果我们无法读懂你的想法或弄清楚你的想法,也不要吝啬家庭作业应该是。
标签: java arrays methods interface abstract-class