标题:遇到难题,求大神指导~~!!!!
只看楼主
advieking
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2015-7-26
结帖率:0
已结贴  问题点数:20 回复次数:2 
遇到难题,求大神指导~~!!!!
编写一个飞机(Plane)类,包含以下属性:
域:初始位置,初始速度,加速度
方法:到达某个位置需要的时间public double arrive(double 目标位置){return 时间}
两个飞机追及时间public double meet(Plane 另一个飞机){return 追及时间}
(注意:需要考虑可能永远无法到达、追及)
搜索更多相关主题的帖子: public double return 加速度 
2015-07-26 16:01
calix
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:28
帖 子:249
专家分:1442
注 册:2015-5-4
得分:10 
这个题目稍微涉及了一些物理知识
第一个问题比较简单,第二个稍微复杂点
下面代码仅供参考
程序代码:
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;
    }

}


2015-07-27 15:11
一大堆bug
Rank: 2
等 级:论坛游民
帖 子:4
专家分:20
注 册:2015-7-22
得分:10 
这是作业吗
2015-07-27 17:20



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-455803-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021798 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved