Skip to content
the poor coder

Hackerrank Box It! Solution

Poor Man
Published
Updated
Reading time
4 min read

Design a class named Box whose dimensions are integers and private to the class. The dimensions are labelled: length , breadth , and height .

The default constructor of the class should initialize , , and  to .

The parameterized constructor Box(int length, int breadth, int height) should initialize Box's  and  to length, breadth and height.

The copy constructor BoxBox ) should set  and  to 's  and , respectively.

Apart from the above, the class should have  functions:

  • int getLength()  - Return box's length
  • int getBreadth() - Return box's breadth
  • int getHeight()  - Return box's height
  • long long CalculateVolume() - Return the volume of the box

Overload the operator  for the class Box. Box   Box   if:

  1. <
  2. <  and ==
  3. <  and == and ==

Overload operator  for the class Box().
If  is an object of class Box:

should print ,  and  on a single line separated by spaces.

Constraints


Two boxes being compared using the  operator will not have all three dimensions equal.

Solution in cpp#

Approach 1.

Python
#include <tuple>

class Box {
    int l = 0, b = 0, h = 0;

public:
    Box() {
        BoxesCreated += 1;
    }
    Box(int il, int ib, int ih)
    : l{il}, b{ib}, h{ih}
    {
        BoxesCreated += 1;
    }
    Box(const Box& other) {
        l = other.l;
        b = other.b;
        h = other.h;
        BoxesCreated += 1;
    }
    
    ~Box() {
        BoxesDestroyed += 1;
    }

    int getLength() const { return l; }
    int getBreadth() const { return b; }
    int getHeight() const { return h; }
    long long CalculateVolume() const {
        return (long long)(l) * b * h;
    }
};

bool operator <(const Box& a, const Box& b) {
    int al = a.getLength(), ab = a.getBreadth(), ah = a.getHeight();
    int bl = b.getLength(), bb = b.getBreadth(), bh = b.getHeight();
    return std::tie(al, ab, ah) < std::tie(bl, bb, bh);
}

ostream& operator <<(ostream& out, Box b) {
    return out << b.getLength() << ' ' << b.getBreadth() << ' ' << b.getHeight();
}

Approach 2.

Python

//Implement the class Box  
class Box
{
    private:
    int l, b, h; //l,b,h are integers representing the dimensions of the box

    public:
// The class should have the following functions : 
// Constructors: 
    Box():l(0),b(0),h(0) 
    {
        ++BoxesCreated;
    }
    Box(int x, int y, int z):l(x),b(y),h(z) 
    {
        ++BoxesCreated;
    }
    Box(const Box &M)
    {
        l = M.l;
        b = M.b;
        h = M.h;
        ++BoxesCreated;
    }

    // Destructor
    ~Box()
    {
        ++BoxesDestroyed;
    }

    inline int getLength(){ return l;}// Return box's length
    inline int getBreadth (){ return b;} // Return box's breadth
    inline int getHeight (){ return h;}  //Return box's height
    long long CalculateVolume() // Return the volume of the box
    {
        long long k = l;
        k = k*h;
        k = k*b;
        return (k);
    }

//Overload operator < as specified
  bool operator<(Box &X)
      { 
      if((l < X.getLength()) || 
         ((b < X.getBreadth()) && (l == X.getLength())) ||
         ((h == X.getHeight()) && (b == X.getBreadth()) && (l == X.getLength())))
          return true;
      else
          return false;
  }

    //Overload operator << as specified
    friend ostream& operator<<(ostream& out, const Box B)
    {
        out << B.l << " " << B.b << " " << B.h;
        return out;
    }
};

Approach 3.

Python
class Box{
    private:
        int l, b, h;
    public:
        Box() {
            this->l = 0;
            this->b = 0; 
            this->h = 0;
            BoxesCreated++;
        }
        Box(int a, int b, int c) {
            this->l = a;
            this->b = b;
            this->h = c;
            BoxesCreated++;
        }
        Box(Box &box) {
            this->l = box.getLength();
            this->b = box.getBreadth();
            this->h = box.getHeight();
            BoxesCreated++;
        }
        ~Box() {
            BoxesDestroyed++;
        }
        int getLength() {
            return this->l;
        }
        int getBreadth() {
            return this->b;
        }
        int getHeight() {
            return this->h;
        }
        long long CalculateVolume() {
            long long volume = long(this->l)*long(this->b)*long(this->h);
            return volume;
        }
        bool operator<(Box &B) {
            if (this->l < B.getLength()) return true;
            else if ((this->b < B.getBreadth())&&(this->l == B.getLength())) return true;
            else if ((this->h < B.getHeight())&&(this->b == B.getBreadth())&&(this->l == B.getLength())) return true;
            else return false;
        }
        
};

ostream& operator<<(ostream& out, Box B) {
            out << B.getLength() << ' ' << B.getBreadth() << ' ' << B.getHeight();
            return out;
        }


Keep going

2020

Hackerrank Virtual Functions Solution

This problem is to get you familiar with virtual functions. Create three classes Person, Professor and Student. The class Person should have data members name and age. The classes Professor and Student should inherit…

2020

Hackerrank Exceptional Server Solution

Your friend set up a small computational server that performs complex calculations. It has a function that takes large numbers as its input and returns a numeric result. Unfortunately, there are various exceptions that…

2020

Hackerrank Inherited Code Solution

You inherited a piece of code that performs username validation for your company's website. The existing function works reasonably well, but it throws an exception when the username is too short. Upon review, you…

ESC