Ask Question
13 December, 01:21

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.

+5
Answers (1)
  1. 13 December, 03:16
    0
    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
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “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 ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers