Wednesday, February 21, 2024
HomeSoftware EngineeringWill there be Sufficient Area in Java

# Will there be Sufficient Area in Java

## The problem#

Bob is working as a bus driver. Nonetheless, he has grow to be extraordinarily widespread amongst 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 desires you to put in writing a easy program telling him if he’ll be capable to match all of the passengers.

The duty:

It’s important to write a perform that accepts three parameters:

• `cap` is the quantity of individuals the bus can maintain excluding the driving force.
• `on` is the variety of individuals on the bus excluding the driving force.
• `wait` is the variety of individuals ready to get on to the bus excluding the driving force.

If there may be 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;

}
``````

## Take a look at circumstances to validate our answer#

``````import org.junit.Take a look at;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;

public class SolutionTest {
@Take a look at
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));
}
}
``````
RELATED ARTICLES