-2

Im currently trying to do an exercise for upcoming exam. But unfortunately he is already asleep and I have an exam tomorrow.

this is my code that I have done. Im not sure what to do with resetSpeed and I think there is also something wrong in trying to calculate timeToTravel method

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

the error that i got is this

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

updated: test case

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 );
        }
    }
Yusuf Ning
  • 65
  • 1
  • 2
  • 9

2 Answers2

2

You are dividing long by an int and storing that value in an int type variable. Java will round down as you've told the JVM you want type int to hold time. You should use double instead.

Plane Class

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

Unit Test

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

Update:

The unit test that was provided doesn't really make sense in real world. There is no guarantee that the division operation would not result in a non whole number. Using int makes no sense as in the real world you would not always being going whole number distance or speeds.

greyfox
  • 6,426
  • 23
  • 68
  • 114
  • I dont think thats the problem tho. because the result is way different – Yusuf Ning Apr 20 '17 at 16:49
  • It may not be your only problem but it is certainly a problem – greyfox Apr 20 '17 at 16:50
  • I tried using double. but the test result is in int. "lossy conversion from double to integer" – Yusuf Ning Apr 20 '17 at 16:57
  • You should probably post the unit test code as well then. I have a feeling this is a homework assignment based on the package name in your code snippet. If you are doing any mathematical operations in a statically typed language like Java where you might have non whole numbers and you care about those values you need to use appropriate data types. – greyfox Apr 20 '17 at 17:02
  • Did you write the unit test code or is this something that was provided to you? – greyfox Apr 20 '17 at 17:13
  • it was provided. could it be the actual operation that is wrong in timeToTravel? because error message is always like this `java.lang.AssertionError: Expected time of '23', but got '0'` so im assuming the result of `time = distance/speed` is always 0? – Yusuf Ning Apr 20 '17 at 17:14
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/142202/discussion-between-yusuf-ning-and-greyfox). – Yusuf Ning Apr 20 '17 at 17:15
-1

It turns out to be really simple

the answer is

time = (60*distance)/speed
Yusuf Ning
  • 65
  • 1
  • 2
  • 9
  • wow, cool, I would have never thought that would fix it. does this actually pass the unit tests? ...well I'm glad you figured it out, and thanks for posting the answer. Good luck on your exam tomorrow. – chickity china chinese chicken Apr 20 '17 at 17:28