The problem
Bob is working as a bus driver. Nonetheless, he has change into extraordinarily in style amongst the town’s residents. With so many passengers desirous to get aboard his bus, he typically has to face the issue of not having sufficient area left on the bus! He needs you to write down a easy program telling him if he’ll have the ability to match all of the passengers.
The duty:
It’s important to write a operate that accepts three parameters:
capis the quantity of individuals the bus can maintain excluding the motive force.onis the variety of folks on the bus excluding the motive force.waitis the variety of folks ready to get on to the bus excluding the motive force.
If there’s sufficient area, return 0, and if there isn’t, return the variety of passengers he can’t take.
Examples:
cap = 10, on = 5, wait = 5 --> 0 # He can match all 5 passengers
cap = 100, on = 60, wait = 50 --> 10 # He cannot match 10 of the 50 ready
The answer in Java code
Choice 1:
public class Bob {
public static int sufficient(int cap, int on, int wait){
return Math.max(0, on + wait - cap);
}
}
Choice 2:
public class Bob {
public static int sufficient(int cap, int on, int wait){
return (cap-on-wait>=0)? 0: on+wait-cap;
}
}
Choice 3:
public class Bob {
public static int sufficient(int cap, int on, int wait) wait<pusto) return 0;
else return wait-pusto;
}
Check instances to validate our resolution
import org.junit.Check;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
public class SolutionTest {
@Check
public void testBob() {
assertEquals("Ought to return 0.", 0, Bob.sufficient(10, 5, 5));
assertEquals("Ought to return 10.", 10, Bob.sufficient(100, 60, 50));
assertEquals("Ought to return 0.", 0, Bob.sufficient(20, 5, 5));
}
}