遇到难题,求大神指导~~!!!!
编写一个飞机(Plane)类,包含以下属性:域:初始位置,初始速度,加速度
方法:到达某个位置需要的时间public double arrive(double 目标位置){return 时间}
两个飞机追及时间public double meet(Plane 另一个飞机){return 追及时间}
(注意:需要考虑可能永远无法到达、追及)
public class Plane { private double position;// 初始位置 private int speed;// 初始速度 private int acceleration;// 加速度 public Plane(double position, int speed, int acceleration) { this.position = position; this.speed = speed; this.acceleration = acceleration; } public Long arrive(double target) { double s = target - position; Long time = 0L; Double length = 0D; while (s > length) { length += speed + (0.5 + time) * acceleration;// 根据s=v*t+0.5*a*t^2,得出t+1比t多前进v+(0.5+t)*a time++; } return time; } public Long meet(Plane plane2) { Double pl1 = 0D, pl2 = plane2.position - position; int currentSpeed1 = speed, currentSpeed2 = plane2.speed; Long time = 0L; while (pl1 < pl2) {// 当后面的飞机的位移超过前面的飞机,表示已经追上 pl1 += speed + (0.5 + time) * acceleration; pl2 += plane2.speed + (0.5 + time) * plane2.acceleration; currentSpeed1 += acceleration; currentSpeed2 += plane2.acceleration; if (currentSpeed1 < currentSpeed2 && acceleration < plane2.acceleration) {//当速度和加速度都小于被追赶者,表示不可能追上 return -1L; } time++; } return time; } public static void main(String[] args) { Plane plane = new Plane(0L, 5, 5); Long time = 0L; time = plane.arrive(100); System.out.println("到达目的地所需时间:" + time + "s"); Plane plane2 = new Plane(100L, 5, 4); time = plane.meet(plane2); if (time >= 0) { System.out.println("相遇所需时间:" + time + "s"); } else { System.out.println("无法相遇"); } } public double getPosition() { return position; } public void setPosition(double position) { this.position = position; } public int getSpeed() { return speed; } public void setSpeed(int speed) { this.speed = speed; } public int getAcceleration() { return acceleration; } public void setAcceleration(int acceleration) { this.acceleration = acceleration; } }