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