Pass a loop result into a later decision

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.


Java
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);
}
Your answers

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Methods, Conditionals, and Loops in Java Questions: