用Java计算平面速度和时间

时间:2017-04-20 16:39:53

标签: java class methods

我正在尝试为即将到来的考试做练习。但不幸的是他已经睡着了,我明天要参加考试。

这是我所做的代码。我不确定如何处理resetSpeed,我认为在尝试计算timeToTravel方法时也有问题

package comp6700.mse;
/**
 * COMP6700 Mid-Semester Exam, Question 3
 */
public class Q3Plane {
    private String name;
    private int speed;
    private int distance;
    private int time;
    /**
     * Constructor
     *
     * @param name The name of the plane
     * @param speed The speed of the plane (in km/h),
     */
    Q3Plane(String name, int speed) {
        this.name = name;
        this.speed = speed;
    }


    /** Return the speed of the plane */
    int getSpeed() {
        return this.speed;
    }
    /**
     * Reset the speed of the plane according to the argument speed
     * @param speed The new speed of the plane
     */
    void resetSpeed(int speed) {this.speed = speed;}

    /**
     * Calculate the time to travel the specified distance at the current speed.
     * @param distance The distance (in km)
     * @return The time to travel the distance (in minutes)
     */
    int timeToTravel(int distance) {
        this.distance = distance;
        this.time = time;

        time = distance/speed;

        return time ;
    }
    /**
     * Return a string describing the plane and its speed,
     * in the format
     *    "Plane NAME is travelling S km/h"
     * where NAME is replaced by the plane's name, and S is replaced by
     * the plane's speed.
     *
     * @return A string describing the plane and its speed
     */
    @Override
    public String toString() {
        return ("Plane"+" "+name+" "+ "is travelling" +" " +speed+ " " + "km/h");  
    }
}

我得到的错误就是这个

java.lang.AssertionError: Expected time of '42', but got '0'

更新:测试用例

public class Q3PlaneTest {
    static final int DEFAULT_ITERATIONS = 10;

    @Test
    public void testGetSpeed() {
        Random r = new Random();
        for (int i = 0; i < DEFAULT_ITERATIONS; i++) {
            String name = "NA"+r.nextInt(10000);
            int speed = 600+r.nextInt(400);
            Q3Plane p = new Q3Plane(name, speed);
            int s = p.getSpeed();
            assertTrue("Expected speed of '"+speed+"', but got '"+s+"'", s == speed );
        }
     }

    @Test
    public void testSetSpeed() {
        Random r = new Random();
        String name = "NA"+r.nextInt(10000);
        int speed = 600+r.nextInt(400);
        Q3Plane p = new Q3Plane(name, speed);
        for (int i = 0; i < DEFAULT_ITERATIONS; i++) {
            speed = 600+r.nextInt(400);
            p.resetSpeed(speed);
            int s = p.getSpeed();
            assertTrue("Expected speed of '"+speed+"', but got '"+s+"'", s == speed );
        }
    }

    @Test
    public void testTimeToTravel() {
        Random r = new Random();
        String name = "NA"+r.nextInt(10000);
        int s = 600+r.nextInt(400);
        Q3Plane p = new Q3Plane(name, s);
        for (int i = 0; i < DEFAULT_ITERATIONS; i++) {
            s = 600+r.nextInt(400);
            int d = 300+r.nextInt(500);
            p.resetSpeed(s);
            int t = p.timeToTravel(d);
            int rt = (60 * d) / s;
            assertTrue("Expected time of '"+rt+"', but got '"+t+"'", t == rt );
        }
    }

2 个答案:

答案 0 :(得分:2)

您将long除以int并将该值存储在int类型变量中。 Java将向下舍入,因为您已告诉JVM您希望类型int保持时间。你应该使用double。

平面类

public class Q3Plane {

  private String name;    // name of plane
  private double speed;   // km per hour

  Q3Plane(String name, double speed) {
    this.name = name;
    this.speed = speed;
  }

  public double getSpeed() {
    return this.speed;
  }

  public void setSpeed(double speed) {
    this.speed = speed;
  }

  public double timeToTravel(int distance) {
    return ((distance/this.speed) * 60);
  }
}

单元测试

public class Q3PlaneTest {

    @Test
    public void testTimeToTravel() {

        Q3Plane plane = new Q3Plane("TESTPLANE", 500);
        double timeInMinutes = plane.timeToTravel(1000);
        System.out.println(timeInMinutes);
        assertTrue(timeInMinutes == 120d);
    }
}

更新

提供的单元测试在现实世界中确实没有意义。无法保证除法运算不会导致非整数。使用int没有任何意义,因为在现实世界中,你不会总是使用整数距离或速度。

答案 1 :(得分:-1)

事实证明这很简单

答案是

time = (60*distance)/speed