马家河子java,java上机任务(四)———CSDN我又回来了

public class Text {

/**

* 程序入口

*/

public static void main(String[] args) {

Vehicle car1,car2;

//使用new运算符和默认的构造方法创建对象car1

car1=new Vehicle();

//使用new运算符和默认的构造方法创建对象car2

car2=new Vehicle();

car1.setPower(128);

car2.setPower(76);

System.out.println("car1的功率是:"+car1.getPower());

System.out.println("car2的功率是:"+car2.getPower());

//car1调用speedUp()方法将自己的speed的值增加80

car1.speedUp(80);

//car2调用speedUp()方法将自己的speed的值增加80

car2.speedUp(80);

System.out.println("car1目前的速度:"+car1.getSpeed());

System.out.println("car1目前的速度:"+car2.getSpeed());

car1.speedDown(10);

car2.speedDown(20);

System.out.println("car1目前的速度:"+car1.getSpeed());

System.out.println("car1目前的速度:"+car2.getSpeed());

//家中的电视

Tv haierTv=new Tv();

//haierTv调用setChannel(int m),并向参数m传递5

haierTv.setChannal(5);

System.out.println("haierTv的频道是"+haierTv.getChannel());

Family zhangsanFamily=new Family();

//zhangsanFamily调用void buyTv(Tv tv)方法,并将haierTv传递给参数Tv

zhangsanFamily.buyTv(haierTv);

System.out.println("zhangsanFamily开始看电视节目");

zhangsanFamily.seeTv();

int m=2;

System.out.println("zhangsanFamily将电视更换到"+m+"频道");

zhangsanFamily.remoteComtrol(m);

System.out.println("haierTv的频道是"+haierTv.getChannel());

System.out.println("zhangsanFamily再看电视节目");

zhangsanFamily.seeTv();

//共饮同井水

//用类名调用setWaterAmount(int m),并向参数传值200

Village.setWaterAmount(200);

int leftWater=Village.waterAmount; //用Village类的类名访问waterAmount

System.out.println("水井中有"+leftWater+"升水");

Village zhaoZhuang,maJiaHeZi;

zhaoZhuang=new Village("赵庄");

maJiaHeZi=new Village("马家河子");

zhaoZhuang.setPeopleNumber(80);

maJiaHeZi.setPeopleNumber(120);

//zhaoZhuang调用drinkWater(int n),并向参数传值50

zhaoZhuang.drinkWater(50);

leftWater=maJiaHeZi.lookWaterAmount(); //maJiaHeZi调用lookWaterAmount()

String name=maJiaHeZi.name;

System.out.println(name+"发现水井中有"+leftWater+"升水");

maJiaHeZi.drinkWater(100);

leftWater=zhaoZhuang.lookWaterAmount();//zhaozhuang调用lookWaterAmount()方法

name=zhaoZhuang.name;

System.out.println(name+"发现水井中有"+leftWater+"升水");

int peopleNumber=zhaoZhuang.getPeopleNumber();

System.out.println("赵庄的人口:"+peopleNumber);

peopleNumber=maJiaHeZi.getPeopleNumber();

System.out.println("马家河子的人口:"+peopleNumber);

// TODO Auto-generated method stub

}

}

public class Vehicle {

//声明double型变量speed,刻画速度

double speed;

//声明int型变量power,刻画功率

int power;

void speedUp(int s){

//将参数s的值与成员变量speed的和赋值给成员变量speed

speed=speed+s;

}

void speedDown(int d){

//将成员变量speed与参数d的差赋值给成员变量speed

speed=speed-d;

}

void setPower(int p){

//将参数p的值赋值给成员变量power

power=p;

}

int getPower(){

//返回成员变量power的值

return power;

}

double getSpeed(){

return speed;

}

}

public class Tv {

int channel;//电视频道

void setChannal(int m){

if(m>=1){

channel=m;

}

}

int getChannel(){

return channel;

}

void showProgram(){

switch(channel){

case 1:System.out.println("综合频道");

break;

case 2:System.out.println("经济频道");

break;

case 3:System.out.println("文艺频道");

break;

case 4:System.out.println("国际频道");

break;

case 5:System.out.println("体育频道");

break;

default :System.out.println("不能收看"+channel+"频道");

}

}

}

public class Family {

Tv homeTv;

void buyTv(Tv tv){

//将参数tv赋值给homeTv

homeTv=tv;

}

void remoteComtrol(int m){

homeTv.setChannal(m);

}

void seeTv(){

homeTv.showProgram();//homeTv调用showProgram()方法

}

}

public class Village {

static int waterAmount;//模拟水井的水量

int peopleNumber;//村庄的人数

String name;

Village(String s){

name=s;

}

static void setWaterAmount(int m){

if(m>0)

waterAmount=m;

}

void drinkWater(int n){

if(waterAmount-n>=0){

waterAmount=waterAmount-n;

System.out.println(name+"喝了"+n+"升水");

}

else

waterAmount=0;

}

static int lookWaterAmount(){

return waterAmount;

}

void setPeopleNumber(int n){

peopleNumber=n;

}

int getPeopleNumber(){

return peopleNumber;

}

}

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
编写程序模拟两个村庄共同拥有一片森林。编写一个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
最新发布
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值