Repack crates through an array parameter

A bottling workshop uses Java arrays of Crate objects. Each object has a mutable integer field bottles; new Crate(n) creates a distinct object whose field equals n.


The workshop defines this method:


Java
static Crate[] repack(Crate[] crates) {
    crates[0].bottles += 2;
    crates[1] = new Crate(9);
    crates = new Crate[] {
        new Crate(14), crates[0]
    };
    crates[0].bottles -= 3;
    return crates;
}


The following fragment runs inside another method:


Java
Crate[] original = {
    new Crate(4), new Crate(6)
};
Crate[] repacked = repack(original);
repacked[1].bottles += 4;


After the fragment finishes, find the sum of the bottles fields accessed through each array: the original total and the repacked total. Give both answers as exact integer numbers of bottles. Count each array position once when calculating its total.

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 Arrays of Objects Questions: