实验要求:编写程序模拟两个村庄共用同一口井水。编写一个Village类,该类有一个静态的int型成员变量waterAmout,用于模拟井水的水量。在主类Land的main()方法中创建两个村庄,一个村庄改变了waterAmount的值,另一个村庄查看waterAmount的值。
class Village{
static int waterAmount;
String name;
int peopleNumber;
int water_used;
Village(String temp, int temp_num){
peopleNumber=temp_num;
name=temp;}
static int lookwaterAmout(){
return waterAmount;}
void drinkwater(int temp){
if(temp <= waterAmount){
waterAmount-=temp;
water_used+=temp;
System.out.println(name+"喝了"+temp+"升水");}
else{
water_used+=waterAmount;
waterAmount=0;
System.out.println(name+"将剩下的水喝完了");}}
int GetpeopleNumber(){
return peopleNumber;}}
public class land{
public static void main(String args[]){
Village.waterAmount=200;
System.out.println("水井中有"+Village.waterAmount+"升水");
Village zhaozhuang,majiahezi;
zhaozhuang =new Village("赵庄",80);
majiahezi =new Village("马家河子", 120);
zhaozhuang.drinkwater(50);
System.out.println("马家河子查看水井剩余水量"+majiahezi.lookwaterAmout()+
"升");
majiahezi.drinkwater(100);
System.out.println("赵庄查看水井剩余水量"+zhaozhuang.lookwaterAmout()+
"升");
System.out.println(zhaozhuang.name+"人口:"+zhaozhuang.peopleNumber);
System.out.println(majiahezi.name+"人口:"+majiahezi.peopleNumber);}}