标签:package sed measure div main class inf 执行 eof
1、学生进入机房使用电脑
电脑有开机,关机,使用中的状
电脑类:
package com.stu_computer; public class Computer { //属性 private boolean isUsed = false;//若为false则处于关机状态;为true为开机状态 private int number; //方法 public boolean isUsed() { return isUsed; } public int getNumber() { return number; } //构造方法 public Computer(){} public Computer(int number){ this.number = number; } //被开机的方法 public void beOn(Computer computer){ System.out.println(this.number+"号电脑开机啦"); computer.isUsed = true; } //被关机的方法 public void beUsing(Computer computer){ System.out.println(this.number+"号电脑正在使用中"); } //被开机的方法 public void beOff(Computer computer){ System.out.println(this.number+"号电脑关机啦"); computer.isUsed = false; } }
机房类:
package com.stu_computer; public class MachineRoom { //属性 public Computer[] computer = new Computer[5]; //方法 public MachineRoom(){ init(); } public void init(){ for(int i=0;i<computer.length;i++){ computer[i] = new Computer(i+1); } } //欢迎学生进机房的方法 public void welStudent(Student student){ System.out.println("欢迎"+student.getName()+"进来机房"); for(int i=0;i<computer.length;i++){//通过遍历找到未被开机的电脑 供学生使用 if(!computer[i].isUsed()){ student.useComputer(computer[i]); break;//找到之后即结束查找 } } } }
学生类:
package com.stu_computer; public class Student { //学生和电脑是依赖的关系 //属性 private String name; private int RP = (int)(Math.random()*10); //方法 public String getName() { return name; } //构造方法 public Student(){} public Student(String name){ this.name = name; } //学生使用电脑的方法 public void useComputer(Computer computer){ System.out.println(this.name+"使用"+computer.getNumber()+"号电脑啦"); computer.beOn(computer); computer.beUsing(computer); //判断学生的RP,根据RP情况选择是否执行关机操作 if(this.RP<5){ computer.beOff(computer); }else{ System.out.println(this.name+"人品太差了,没关电脑"); } } }
测试类:
package com.stu_computer; public class Test { public static void main(String[] args) { //创建一个机房对象 MachineRoom machineRoom = new MachineRoom(); //创建学生对象 机房执行欢迎学生进来的方法 Student student1 = new Student("小花"); machineRoom.welStudent(student1); Student student2 = new Student("小ff"); machineRoom.welStudent(student2); Student student3 = new Student("小文件"); machineRoom.welStudent(student3); } }
2.测速器测量小汽车速度,警车追击小汽车
package com.car_policecar; public class Car { //属性 private int speed; //方法 //获取小汽车的速度 public int getSpeed() { return speed; } //构造方法 public Car(){} public Car(int speed){ this.speed = speed; } }
package com.car_policecar; public class PoliceCar { //属性 private int speed; //方法 //获取警车的速度 public int getSpeed() { return speed; } public PoliceCar(){} public PoliceCar(int speed){ this.speed = speed; } //追击小汽车方法 //比较警车和小汽车速度 判断是否能追上 public void catchCar(Car car){ if(this.speed>car.getSpeed()){ int time = 100/(this.speed - car.getSpeed()); System.out.println("经过"+time+"s追击成功"); }else{ System.out.println("追击失败"); } } }
package com.car_policecar; public class VelometerMachine { private int standardTime; public int getStandardTime() { return standardTime; } //方法 public VelometerMachine(){} public VelometerMachine(int time){ this.standardTime = time; } public void measureSpeed(Car car){ int carSpeed = car.getSpeed(); int carTime = 100/carSpeed; if(this.standardTime>carTime){//小汽车超速了 System.out.println("小汽车超速,马上进行追击"); PoliceCar pc = new PoliceCar(25); pc.catchCar(car); }else{ System.out.println("小汽车速度正常"); } } }
package com.car_policecar; public class Test { public static void main(String[] args) { VelometerMachine vm = new VelometerMachine(5); Car car = new Car(26); vm.measureSpeed(car); } }
标签:package sed measure div main class inf 执行 eof
原文地址:https://www.cnblogs.com/hsy-go/p/12423900.html