Exceptions and silent errors in parcel averages

A parcel service calculates the average number of labels per parcel when distributing 10 labels. A tester evaluates each Java method independently for every row below. A failure in one test does not prevent any other test from running.


Java
static double averageA(int parcels) {
    return 10 / parcels;
}

static double averageB(int parcels) {
    return (double) (10 / parcels);
}

static double averageC(int parcels) {
    return (double) 10 / parcels;
}


The (double) syntax is a cast. For nonzero parcel counts, compare returned values with the exact mathematical average of 10 divided by the parcel count. Ignore ordinary floating-point rounding.


Test run

Parcels

Cedar

0

Maple

2

Pine

3

Birch

4

Alder

0

Spruce

5

Willow

6

Fir

8


Report these four quantities as exact whole numbers of runs:

  • The number of averageA runs that throw ArithmeticException.
  • The number of averageA runs that finish but return an incorrect finite average for a nonzero parcel count.
  • The number of averageB runs that throw ArithmeticException.
  • The number of averageC runs that throw ArithmeticException.
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 Exceptions and Error Types Questions: