编写程序模拟两个村庄共同拥有一片森林。编写一个Village类,该类有一个int型成员变量用于模拟林森中树木的数量。(参见5.14上机实践)。两个村庄的名字分别为:Red River 和 Whiterun。在森林中已经存在200棵树,在主类的主方法里面创建两个村庄,一个村庄进行了植树活动,改变了treeAmount的值,另外一个村庄调查了森林中的树木情况,并把观察到的树木的总数显示了出来。 请按下面的方式对书上程序的一些改动。 System.out.println(name + "植树" + n + "棵");=> System.out.println(n + " trees are planted in the " + name); System.out.println(name + "砍伐" + n + "棵");=> System.out.println(n + " trees are cut down in the " + name); System.out.println("无树木可发");=> System.out.println(" There is no tree for felling" ); System.out.println(name + "增加了" + n + " 人");=> System.out.println(n + "persons are add in the " + name ); System.out.println("森林中有" + leftTree + " 棵树");=> System.out.println("There are " + leftTree + " trees in the forest" ); System.out.println("赵庄的人口:" + zhaoZhuang.peopleNumber);=> System.out.println("The population of the Red River is " + zhaoZhuang.peopleNumber); System.out.println("马家河子的人口:" + maJiaHeZi.peopleNumber);=> System.out.println("The population of the Whiterun is " + maJiaHeZi.peopleNumber); 【输入形式】请输入赵庄种植树木的数量。 【输出形式】书上的模版显示下列信息: There are 200 trees in the forest 50 trees are planted in the Red River There are 250 trees in the forest 70 trees are cut down in the Whiterun There are 180 trees in the forest The population of the Red River is 100 12 persons are add in the Red River The population of the Red River is 112 The population of the Whiterun is 150 10 persons are add in the Whiterun The population of the Whiterun is 160
时间: 2024-03-08 07:47:07 浏览: 165
根据题目要求,我们需要定义一个 `Village` 类,并在主方法中创建两个村庄对象,分别进行植树和砍树的操作,以及人口增加的操作,最后输出结果。下面是实现代码:
```java
class Village {
private int treeAmount;
private int peopleNumber;
public Village(int treeAmount, int peopleNumber) {
this.treeAmount = treeAmount;
this.peopleNumber = peopleNumber;
}
public void plantTrees(int n) {
treeAmount += n;
System.out.println(n + " trees are planted in the Red River");
}
public void cutDownTrees(int n) {
if (treeAmount >= n) {
treeAmount -= n;
System.out.println(n + " trees are cut down in the Whiterun");
} else {
System.out.println("There is no tree for felling");
}
}
public void addPeople(int n) {
peopleNumber += n;
System.out.println(n + " persons are add in the Red River");
}
public int getTreeAmount() {
return treeAmount;
}
public int getPeopleNumber() {
return peopleNumber;
}
}
public class Main {
public static void main(String[] args) {
Village zhaoZhuang = new Village(200, 100);
Village maJiaHeZi = new Village(200, 150);
System.out.println("There are " + zhaoZhuang.getTreeAmount() + " trees in the forest");
zhaoZhuang.plantTrees(50);
System.out.println("There are " + zhaoZhuang.getTreeAmount() + " trees in the forest");
maJiaHeZi.cutDownTrees(70);
System.out.println("There are " + maJiaHeZi.getTreeAmount() + " trees in the forest");
zhaoZhuang.addPeople(12);
System.out.println("The population of the Red River is " + zhaoZhuang.getPeopleNumber());
maJiaHeZi.addPeople(10);
System.out.println("The population of the Whiterun is " + maJiaHeZi.getPeopleNumber());
}
}
```
程序中,我们定义了一个 `Village` 类,其中包含了森林中的树木数量和村庄的人口数量。`Village` 类中还定义了种植树木、砍伐树木、增加人口等操作,并且通过 `getTreeAmount()` 和 `getPeopleNumber()` 方法获取森林中的树木数量和村庄的人口数量。
在主方法中,我们创建了两个村庄对象 `zhaoZhuang` 和 `maJiaHeZi`,并分别进行了植树、砍树和增加人口的操作,然后输出了结果。其中,输出语句根据题目要求做了相应的修改。
阅读全文