Boxes
General
- All submissions must include a work history to be worth credit.
- Make sure to name files exactly as specified, including capitalization.
- Make sure your program accepts input in exactly the order and format specified. Do not ask for extra input or change the order of inputs. (If you want to make an expanded version of a program and get feedback on it, email it to me or show me in person.)
- Output the requested information in the specified order. Slight variations in output wording/formatting are usually fine, as long as formatting isn't part of the assignment. If you cannot get the right final output, print out some evidence of the work your program did to demonstrate your progress.
- Readability and maintainability of your code counts. Poor formatting, confusing variable names, unnecessarily complex code, etc. can result in deductions from your score.
Objective
Upon completion of this assignment, the student will be able to write functions and use unit tests to verify their functionality.
Background
You will be functions to help calculate information about the boxes that can be made from a sheet of cardboard. Say we have a sheet of cardboard that has a width of $y$ and a length of $z$. We can make cuts of size $x$ as shown below in the leftmost picture, then fold the sides up to make a box with the dimensions $y - 2x$ by $z - 2x$ by $x$.
Assignment Instructions
Use the UnitTestProject template folder.
Then open tests.cpp and replace it with the starter code from this copy of tests.cpp.
If a given TEST_CASE
fails to compile, or crashes while running, comment
it back out before submission so I can see the rest of the tests run. Leave in your
code that you tried to write for the test, you can still get partial credit for untested
code. If a TEST_CASE
does compile and run, but fails, leave it uncommented. A
failed test is better than a commented out one - it represents working code that clearly
identifies a problem that could be worked on more.
In terms of scoring for each function:
Passing Test > Failing Test > Commented-Out Test > Does not Compile OR Missing
See below for tips on specific functions.
Submission
Submit file: assign6.zip
I should be able to compile and run your program by doing:
g++ -std=c++17 tests.cpp -o program.exe
.\program.exe (./program.exe on a mac)
Functions Guide
The following functions are what you need to actually implement. There is one or more unit test for each function.
Each function must be preceded by a doxygen comment with a brief description of the function overall, and descriptions of its parameters and return value.
It is strongly recommended you attack them by working on one unit test at a time. Uncomment the next unit test in the file and build a new function (or modify existing functions) until you pass the test.
int getArea(int width, int length)
For a given width and length, return area of the corresponding rectangle.
int getVolume(int width, int length, int height)
For a given width, length and height, return volume of corresponding
box. This must use getArea
as a helper for full credit.
int leftoverCardboard(int cutoutSize)
For a given size cutout ($x$), return the number of square inches of leftover cardboard (total area cut out at the corners).
For example, leftoverCardboard(3)
should return 36.
int getMaxHeight(int width, int length)
For a given width and length, returns the maximum height (whole number) that results in a valid box (all dimensions greater than 0).
int getBestHeight(int width, int length)
For a sheet of cardboard of the given width and length, return the height of the "best" box to make from it. The "best" box is the one that has the maximum possible volume, and if there are multiple with the same volume, the one of those with the greatest height. (It will always have more leftover cardboard, which we can recycle to make more boxes with.)
For example the possible boxes given a 16 x 14 sheet are shown below. The "best" box has a height of 3. It is tied for the most volume with height 2, but results in more leftover cardboard.
Height Width Length Volume Leftover Amount 1 14 12 168 4 2 12 10 240 16 3 10 8 240 36 4 8 6 192 64 5 6 4 120 100 6 4 2 48 144
Hint: Notice that as the height increases, volume will increase as well until it reaches a point at which it starts decreasing. The last height that does not produce a smaller volume is the best one. In the fable above, height 1 produces a volume of 168; height 2 produces a volume of 240, which is not smaller than the 168 we saw; height 3 produces a volume of 240, which is again not smaller than the value of 240 we saw with height 2; height 4 produces a volume of 192, which is smaller than the 240 we saw at the last step. Therefore, the best height is 3 - there is no need to even consider the other possible heights.
For full credit, you must use your other functions and not duplicate their logic.
Only worry about integer heights. You do not have to worry about length or width being less than 3.
string createBoxStatsString(int w, int l, int h)
Return a string that for a box of the given dimensions, builds a string containing the following information:
Height Width Length Volume LeftoverAmount
The information should be formatted so that it is left justified in columns of width 8, except for the last value (LeftoverAmount), which should just be added without any specific width.
For inputs of 8, 6, 2
, this should look like:
2 8 6 96 16
Shown with .'s instead of spaces, that is:
2.......8.......6.......96......16
To do this well (and easily), you will want to
use left
and setw(size)
and not manually count tabs or space characters.
To use those, you have to build up your string in a stringstream.
First, make sure to #include <sstream>
(and #include <iomanip>
for setw
).
Then do something like:
stringstream outStream;
outStream << left << setw(8) << variable1;
outStream << left << setw(8) << variable2;
...
string result = outStream.str();
If you are having problems seeing your spacing while debugging, try doing outStream << setfill('.')
before building your output.
void printBoxOptions(int width, int length)
For a sheet of cardboard of the given width and length, print out a table like the one below
of the possible boxes. Next to the best box (as defined in getBestHeight), print ***
.
For a sheet that is 20 x 15, you should print something like:
Height Width Length Volume Leftover Amount 1 18 13 234 4 2 16 11 352 16 3 14 9 378 36*** 4 12 7 336 64 5 10 5 250 100 6 8 3 144 144 7 6 1 42 196
The columns of output should be cleanly lined up with the headers.
For full credit, you must use your other functions and not duplicate their logic.
Note:
Because this function prints directly to the console, our unit test can't actually test that it is working correctly. All it does is call the function and display both what we expect to see and the results of the function. We have to manually verify that the function worked correctly.
If we wanted to make this function easier to test, we could redesign it to return a string with the result instead of printing it directly to the console. This would actually be a more flexible and thus powerful function than what we currently have.