Description
Design a class named Box whose dimensions are integers and private to the class. The dimensions are labelled: length, width, and height.
Constructors:
- The default constructor of the class should initialize length, width, and height to 0.
- The parameterized constructor Box(int l, int w, int h) should initialize Box's length, width and height to l, w and h.
- The copy constructor Box(Box& b) should set length, width and height to b's length, width and height, respectively.
Methods:
- void setLength(int l) – Should set box's length to l
- void setWidth(int w) - Should set box's width to w
- void setHeight(int h) - Should set box's height to h
- int getLength() - Return box's length
- int getWidth() - Return box's width
- int getHeight() - Return box's height
- long long CalculateVolume() - Return the volume of the box (Hint: cout << volume << endl; )
ref: https://www.hackerrank.com/challenges/box-it/proble
main.cpp
#include <iostream>
#include <string>
using namespace std;
class Box
{
public:
Box() : length(0), width(0), height(0) {}
/* Todo */
void setLength(int _l) { length = _l; }
/* Todo */
int getLength() { return length; }
/* Todo */
/* Todo */
// 1. calculateVolume() 2. calculateSurfaceArea()
private:
int length, width, height;
};
// Don't touch main()
int main()
{
int testcase = 0;
cin >> testcase;
if (testcase == 1)
{
Box box = Box();
cout << "Length: " << box.getLength() << ", Width: " << box.getWidth() << ", Height: " << box.getHeight() << endl;
box.setLength(300);
box.setWidth(400);
box.setHeight(500);
cout << "Length: " << box.getLength() << ", Width: " << box.getWidth() << ", Height: " << box.getHeight() << endl;
}
else if (testcase == 2)
{
Box box = Box(17, 17, 34);
cout << "Length: " << box.getLength() << ", Width: " << box.getWidth() << ", Height: " << box.getHeight() << endl;
box.setLength(101);
box.setWidth(50);
box.setHeight(34);
cout << "Length: " << box.getLength() << ", Width: " << box.getWidth() << ", Height: " << box.getHeight() << endl;
}
else if (testcase == 3)
{
Box box = Box(100, 17, 17);
cout << "Length: " << box.getLength() << ", Width: " << box.getWidth() << ", Height: " << box.getHeight();
cout << ", Surface Area: " << box.calculateSurfaceArea() << ", Volume: " << box.calculateVolume() << endl;
}
else if (testcase == 4)
{
Box box = Box();
cout << "Length: " << box.getLength() << ", Width: " << box.getWidth() << ", Height: " << box.getHeight();
cout << ", Surface Area: " << box.calculateSurfaceArea() << ", Volume: " << box.calculateVolume() << endl;
box.setLength(44);
box.setWidth(200);
box.setHeight(1);
cout << "Length: " << box.getLength() << ", Width: " << box.getWidth() << ", Height: " << box.getHeight();
cout << ", Surface Area: " << box.calculateSurfaceArea() << ", Volume: " << box.calculateVolume() << endl;
}
else if (testcase == 5)
{
Box box = Box(10000, 20000, 30000);
cout << "Length: " << box.getLength() << ", Width: " << box.getWidth() << ", Height: " << box.getHeight();
cout << ", Surface Area: " << box.calculateSurfaceArea() << ", Volume: " << box.calculateVolume() << endl;
}
return 0;
}
Input
No need to handle input. (Input would be number 1~5 to represent the index of test-case)
Output
Depend on test case show in main.cpp.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
Design a class named Time. Data which are stored in Time class consists of year(int), month(int), day(int), hour(int), minute(int), second(int).
Constructors:
- The default constructor of the class should initialize year(int), month(int), day(int), hour(int), minute(int), second(int) to -1.
- The parameterized constructor Time(int h, int mi, int s) should initialize Time's hour, minute and second to h, mi and s.
- The parameterized constructor Time(int y, int mo, int d, int h, int mi, int s) should initialize Time's year, month, day, hour, minute and second to y, mo, d, h, mi and s.
Methods:
- void setAll(int y, int mo, int d, int h, int mi, int s) – Should set time's year, month, day, hour, minute, second to y, mo, d, h, mi and s, repectively.
- void setTime(int h, int mi, int s) – Should set time's hour, minute, second to h, mi and s, repectively.
- void setDate(int y, int mo, int d) – Should set time's year, month, day to y, mo, and d, repectively.
- void print() - Should print out time’s data.
- If all time’s data are set, print: h:mi:s d, mo, y. Example, time -> year(2020), month(11), day(29), hour(9), minute(2), second(49), print: 9:2:49 11 29 2020 (Hint: cout << hour << “:” << minute << “:” << second << “ ” << month << “ ” << day << “ ” << year << endl; )
- If one of year, month, day is not set, print: h:mi:s. Example, time -> year(-1), month(11), day(29), hour(9), minute(2), second(49), print: 9:2:49
- If one of hour, minute, second is not set, print: d, mo, y. Example, time -> year(2020), month(11), day(29), hour(-1), minute(-1), second(49), print: 11 29 2020
- If one of year, month, day is not set, and one of hour, minute, second is not set, print "Time is not set". Example, time -> year(2020), month(-1), day(29), hour(9), minute(2), second(-1), print: Time is not set
main.cpp
#include <iostream>
#include <string>
using namespace std;
class Time
{
public:
Time() : year(-1), month(-1), day(-1), hour(-1), minute(-1), second(-1) {}
/* Todo */
// Implement 2 parameterized constructors
/* Todo */
// Implment setAll(), setTime(), setDate(), print()
private:
int year, month, day, hour, minute, second;
};
// Don't touch main()
int main()
{
int testcase = 0;
cin >> testcase;
if (testcase == 1)
{
Time time = Time(2020, 12, 1, 12, 16, 34);
time.print();
}
else if (testcase == 2)
{
Time time1 = Time(15, 54, 31);
time1.print();
Time time2 = Time();
time2.print();
}
else if (testcase == 3)
{
Time time = Time();
time.print();
time.setDate(2021, 3, 24);
time.print();
time.setTime(7, 15, 17);
time.print();
}
else if (testcase == 4)
{
Time time = Time(17, -1, 45);
time.print();
time.setDate(2022, 5, -1);
time.print();
time.setAll(2024, -1, 4, -1, 23, 22);
time.print();
}
else if (testcase == 5)
{
Time time = Time(-1, -1, -1, 10, 10, 10);
time.print();
time.setAll(3031, 12, 25, -1, -1, -1);
time.print();
time.setTime(12, 31, 17);
time.print();
time.setDate(2020, 12, 1);
time.print();
}
return 0;
}
Input
No need to handle input. (Input would be number 1~5 to represent the index of test-case)
Output
Depend on test case show in main.cpp.