C++ HELP Project 13: Exception/Namespace Flashdrive The purpose of this assignme
ID: 3835418 • Letter: C
Question
C++ HELP
Project 13: Exception/Namespace Flashdrive
The purpose of this assignment is to work with exceptions. As you may recall, I have provided you with a sample class named FlashDrive which has been diagrammed below. I'd like you to enhance this class so that invoking its methods or operators potentially throw exceptions, rather than just printing error messages to cout. Currently, our favorite exception class is std::logic_error. You can create a logic_error by passing a string value to its constructor. Officially, you should also say #include to begin working with logic_error, but Visual Studio (being a badly behaved child...) let's you get away without it.
Although the sample driver code might not code for all these circumstances, I would like you to throw exceptions when:
more stuff has been put onto the drive than it can safely hold (due to writeData)
a negative number is potentially used as a my_StorageUsed value (due to operator – or bad values being sent to the constructor call)
a negative number is potentially used as a my_StorageCapacity value (due to operator – or bad values being sent to the constructor call)
So carefully wind your way thru all the operators and methods of the class ensuring that logic_error gets thrown in each of these circumstances.
I'd also like you to get operator << and operator >> working for the class FlashDrive. And finally, I'd like you to place FlashDrive into the namespace cs52.
HINT: Recall that you can create a logic_error by passing a string message. For example,
std::logic_error error( "Bad News" );
While not required with Visual Studio, please #include when working with this class. Linux fans will require this include; its optional for Windows users but won't hurt anything if you do it. Here is a class diagram for logic_error. As I said, it already exists so please make use of it.
PLEASE DON'T SEND ME THE ENTIRE VISUAL STUDIO SOLUTION FOLDER. Instead, as you'll do with every homework assignment, please cherry-pick from your hard-drive. Select just the source (.cpp) file and the executable (.exe) file.
Windows users will find the executable file (known as an Application) in your Solution Folder's Debug/Release folder. Mac users will find the executable file listed under the Products folder of their XCode solution. Mac users should click the arrow associated with the "Full Path" in the right-hand side of the screen under "Identity and Type" to navigate to the folder where the executable file resides.
Copy these two files together into a folder named with your name and student ID. From Windows Explorer, select this folder and then right-click and select Send to->Compressed (zipped) Folder to create a .zip file containing this folder. From MacOS, select this folder and then select File->Compress "FolderName" to create a .zip file containing this folder. Then upload this file into the submission area below by clicking Submit Assignment. Then click on the Choose File button to select this file. Click on Submit Assignment to finalize this file submission. All these steps are described in the Electronic Submission Guidelines in the General top section of class.
Sample Driver#include
#include // for logic_error #include "FlashDrive.h"
void main( ) { using namespace cs52; cs52::FlashDrive empty; cs52::FlashDrive drive1( 10, 0, false ); cs52::FlashDrive drive2( 20, 0, false ); drive1.plugIn( ); drive1.formatDrive( ); drive1.writeData( 5 ); drive1.pullOut( ); drive2.plugIn( ); drive2.formatDrive( ); drive2.writeData( 1 ); drive2.pullOut( ); // read in a FlashDrive... // the class designer for FlashDrive (that's you!) // gets to decide which fields matter and should be read in cs52::FlashDrive sample; cin >> sample; // print out a FlashDrive... // the class designer for FlashDrive (that's you!) // gets to decide which fields matter and should be printed cout << sample << endl; cs52::FlashDrive combined = drive1 + drive2; cout << "this drive's filled to " << combined.getUsed( ) << endl; cs52::FlashDrive other = combined – drive1; cout << "the other drive's filled to " << other.getUsed( ) << endl; if (combined > other) { cout << "looks like combined is bigger..." << endl; } else { cout << "looks like other is bigger..." << endl; } if (drive2 > other) { cout << "looks like drive2 is bigger..." << endl; } else { cout << "looks like other is bigger..." << endl; } if (drive2 < drive1) { cout << "looks like drive2 is smaller..." << endl; } else { cout << "looks like drive1 is smaller..." << endl; } // let's throw some exceptions...
try { empty = empty – combined; cout << "something not right here..." << endl; } catch( std::logic_error ) { // an exception should get thrown... // so the lines of code here should // be run, not the cout statement... } try { drive2.writeData( 10000 ); cout << "something not right here..." << endl; } catch( std::logic_error ) { // an exception should get thrown... // so the lines of code here should // be run, not the cout statement... } try { cs52::FlashDrive f( -1, -1, false ); cout << "something not right here..." << endl; } catch( std::logic_error ) { // an exception should get thrown... // so the lines of code here should // be run, not the cout statement... } }
Explanation / Answer
#include #include // for logic_error #include "FlashDrive.h" void main( ) { using namespace cs52; cs52::FlashDrive empty; cs52::FlashDrive drive1( 10, 0, false ); cs52::FlashDrive drive2( 20, 0, false ); drive1.plugIn( ); drive1.formatDrive( ); drive1.writeData( 5 ); drive1.pullOut( ); drive2.plugIn( ); drive2.formatDrive( ); drive2.writeData( 1 ); drive2.pullOut( ); // read in a FlashDrive... // the class designer for FlashDrive (that's you!) // gets to decide which fields matter and should be read in cs52::FlashDrive sample; cin >> sample; // print out a FlashDrive... // the class designer for FlashDrive (that's you!) // gets to decide which fields matter and should be printed coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.