JAVA2实用教程上机作业4机动车,电视类,共饮水类

上机作业4
实验1 机动车

public class Vehicle {
	double speed;
	int power;
	void speedUp(int s){
		speed=speed+s;
		
	}
	void speedDown(int d){
		speed=speed-d;
		
	}
	void setPower(int p){
		power=p;
	}
	int getPower(){
		return power;
	}
	double getSpeed(){
		return speed;
	}
	

}

public class User {
	public static void main(String args[]){
		Vehicle car1,car2;
		car1=new Vehicle();
		car2=new Vehicle();
		car1.setPower(128);
		car2.setPower(76);
		System.out.println("car1的功率是:"+car1.getPower());
		System.out.println("car2的功率是:"+car2.getPower());
		car1.speedUp(80);
		car2.speedUp(100);
		System.out.println("car1目前的速度是:"+car1.getSpeed());
		System.out.println("car2目前的速度是:"+car2.getSpeed());
		car1.speedDown(10);
		car2.speedDown(20);
		System.out.println("car1目前的速度是:"+car1.getSpeed());
		System.out.println("car1目前的速度是:"+car2.getSpeed());}

}

实验2 家中的电视

public class TV {
	int channel;//电视频道
	void setChannel(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){
		homeTV=tv;
	}
	void remoteControl(int m){
		homeTV.setChannel(m);
	}
	void seeTV(){
		homeTV.showProgram();
	}
	
}

public class MainClass {
	public static void main(String args[]){
		TV haierTV=new TV();
		haierTV.setChannel(5);
		System.out.println("haierTV的频道是:"+haierTV.getChannel());
		Family zhangSanFamily=new Family;
		zhangSanFamily.buyTV(haierTV);
		int m=2;
		System.out.println("zhangSanFamily将电视更换到"+m+"频道");
		zhangSanFamily.remoteControl(m);
		System.out.println("haier");
	}
}

实验3共饮水

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;
	}

}
public class Land {
	public static void main(String args[]){
		Village.setWaterAmount(200);
		int leftWater=Village.waterAmount;
		System.out.println("水井中有"+leftWater+"升水");
		Village zhaoZhuang;
		Village maJiaheZhi;
		zhaoZhuang=new Village("赵庄");
		maJiaheZhi=new Village("马家河子");
		maJiaheZhi.setPeopleNumber(120);
		zhaoZhuang.setPeopleNumber(80);
		zhaoZhuang.drinkWater(50);
		leftWater=maJiaheZhi.lookWaterAmount();
		String name=maJiaheZhi.name;
		System.out.println(name+"发现水井中有"+leftWater+"升水");
		maJiaheZhi.drinkWater(100);
		leftWater=zhaoZhuang.lookWaterAmount();
		name=zhaoZhuang.name;
		System.out.println(name+"发现水井中有"+leftWater+"升水");
		int peopleNumber=zhaoZhuang.getPeopleNumber();
		
		System.out.println("赵庄的人口:"+peopleNumber);
		peopleNumber=maJiaheZhi.getPeopleNumber();
		System.out.println("马家河子的人口:"+peopleNumber);
	
	}

}

你可能感兴趣的:(JAVA2实用教程上机作业4机动车,电视类,共饮水类)