26.0k views
4 votes
Write a Box class whose init method takes three parameters and uses them to initialize the length, width and height of a Box. It should also have a method named volume that returns the volume of the Box. Write a function named box_sort (not part of the Box class) that uses insertion sort to sort a list of Boxes from greatest volume to least volume.

User AYETY
by
7.8k points

1 Answer

3 votes

Answer:

1.

class Box

{

double length, breadth, height;

// constructor used when all dimensions specified

Box(double w, double h, double d)

{

length = l;

breadth= b;

height = h;

}

// compute and return volume

double volume()

{

return width * height * depth;

}

}

2.

class box_sort {

/*Function to sort volumes using insertion sort*/

void sort(int vol[])

{

int n = vol.length;

for (int i = 1; i < n; ++i) {

int key = vol[i];

int j = i - 1;

/* Move elements of vol[0..i-1], that are greater than key, to one position ahead of their current position */

while (j >= 0 && vol[j] > key) {

vol[j + 1] = vol[j];

j = j - 1;

}

vol[j + 1] = key;

}

}

/* A utility function to print array of size n*/

static void printArray(int vol[])

{

int n = vol.length;

for (int i = 0; i < n; ++i)

System.out.print(vol[i] + " ");

System.out.println();

}

The 2 programs are written in Java Programming Language

Comments were used to explain difficult segments of the code

User JPZ
by
8.5k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.