-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
  • 2
  • 9
  • `resetSpeed` is just a wierd name for a simple `getter`. – PM 77-1 Apr 20 '17 at 16:43
  • What unit is `speed` in? – Kayaman Apr 20 '17 at 16:46
  • its in km/h @kayaman – Yusuf Ning Apr 20 '17 at 16:47
  • @PM77-1 I updated the code. so set getter as 'void resetSpeed(int speed) {this.speed = speed;}' – Yusuf Ning Apr 20 '17 at 16:49
  • Then dividing `distance` (km) with `speed` (km/h) gives you hours. The method is required to return minutes (also see @greyfox's answer, as it's relevant). – Kayaman Apr 20 '17 at 16:52
  • can you check the updated version? including the error that i got – Yusuf Ning Apr 20 '17 at 16:53
  • @kayaman so all should be use double? time, distance, speed? – Yusuf Ning Apr 20 '17 at 16:55
  • You don't need to have everything in double, especially if you don't understand what that means. Read this: http://stackoverflow.com/questions/4685450/why-is-the-result-of-1-3-0 and/or http://stackoverflow.com/questions/7220681/division-of-integers-in-java – Kayaman Apr 20 '17 at 16:58
  • @downshift yes you are actually correct. i tried changing it to double and got an error message "lossy conversion from double to int' – Yusuf Ning Apr 20 '17 at 16:59
  • Thanks @YusufNing, I don't know if I'm correct, but it seems like the evaluator is Asserting results of type `int`, I'm just as confused as you are :-/ – davedwards Apr 20 '17 at 17:00
  • Maybe the result of type `double` should get cast to an integer after the calculation, before being called in the `toString()` method -- as in, cast `speed` to an `int` -> `int(speed);` – davedwards Apr 20 '17 at 17:01
  • even more puzzling why we would want to use datatype `double` when the test case uses all `int`s. – davedwards Apr 20 '17 at 17:10
  • yes thats correct @downshift its actually a simple question but for some reason its hard hahaha – Yusuf Ning Apr 20 '17 at 17:11
  • You can cast one of your variables to `double` to avoid integer division, and cast the result back to `int` for returning. I suppose you ignored the links I gave? – Kayaman Apr 20 '17 at 17:15
  • i did, i change it to `time = double (distance/time)` but still not working. i always get time equals to 0. maybe its the actual operation in timeToTravel that is wrong? – Yusuf Ning Apr 20 '17 at 17:19

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
  • 5,608
  • 15
  • 58
  • 103
  • 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
  • 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. – davedwards Apr 20 '17 at 17:28