A supply depot packs four filters per full box. Its program assigns two courier dispatches when at least four full boxes are ready, and one dispatch otherwise. For the code below, find the number of full boxes and the number of dispatches. Enter both counts as exact integers.
static int packBoxes(int stock) { int boxes = 0; while (stock >= 4) { stock = stock - 4; boxes = boxes + 1; } return boxes; } public static void main(String[] args) { int boxes = packBoxes(19); int dispatches; if (boxes >= 4) { dispatches = 2; } else { dispatches = 1; } System.out.println(boxes); System.out.println(dispatches); }
Unlock full course notes by signing up
Follow control through a method call
The method below adds the whole numbers from 1 to n with a while loop, and main calls it once and prints the result.
Put the events in the order in which they happen when main reaches the line int result = sumTo(2 * 3);.
Measure the effect of an early return
A reading challenge should award one coupon per day, except that every fourth day earns three coupons instead. The method below is called as coupons(15).
A programmer corrects the method by removing only the return total; inside the loop, leaving the final return after the loop. By how many coupons does this correction increase the returned result? Answer in coupons, to zero decimal places.
Trace continue and break in one loop
The method below scans the whole numbers from 1 to limit, skipping some and possibly stopping early.
Trace the call process(10). For every value of i from 1 to 10, write the action as one word: add (the statement score += i; runs), continue, break, or skipped (the loop body never starts with that value of i). Then give the value score holds after that row: at the end of the pass, at the moment the loop is left, or unchanged when the row is skipped. The first row is filled in as an example. In the last row, give the value the call returns.