Project 1: Exception/Namespace TrashCan The purpose of this assignment is to wor
ID: 3766570 • Letter: P
Question
Project 1: Exception/Namespace TrashCan
The purpose of this assignment is to work with exceptions. As you may recall, I have provided you with a sample class named TrashCan which has been diagrammed below. You can acquire the source to the FlashDrive class here (.NET 2012 or XCode) 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 <stdexcept>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 can than it can safely hold (due to operator + or calls to addItem or bad values being sent to the constructor call)
a negative number is potentially used as a my_Size value (due to operator – or bad values being sent to the constructor call or setSize)
a negative number is potentially used as a my_Contents 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 TrashCan. And finally, I'd like you to place TrashCan 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 <stdexcept> 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.
#include <iostream>
#include <stdexcept>
#include "TrashCan.h"
int main( ) {
using namespace std;
using namespace cs52;
cout << "Welcome to Howie's TrashCan Program!" << endl;
TrashCan myCan;
TrashCan yourCan;
TrashCan empty( 0, 0 );
yourCan.setSize( 12 );
myCan.setSize( 12 );
yourCan.addItem( );
yourCan.addItem( );
myCan.addItem( );
myCan.printCan();
yourCan.printCan();
// read in a TrashCan...
// the class designer for TrashCan (that's you!)
// gets to decide which fields matter and should be read in
cs52::TrashCan sample;
cin >> sample;
// print out a TrashCan...
// the class designer for TrashCan (that's you!)
// gets to decide which fields matter and should be printed
cout << sample << endl;
TrashCan combined = yourCan + myCan;
cout << "this drive's filled to " << combined.getContents( ) << endl;
TrashCan other = combined – myCan;
cout << "the other cup's filled to " << other.getContents( ) << endl;
if (combined > other) {
cout << "looks like combined is bigger..." << endl;
}
else {
cout << "looks like other is bigger..." << endl;
}
if (myCan > other) {
cout << "looks like myCan is bigger..." << endl;
}
else {
cout << "looks like other is bigger..." << endl;
}
if (yourCan < myCan) {
cout << "looks like yourCan is smaller..." << endl;
}
else {
cout << "looks like myCan 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 above...
cout << "exception was caught. moving on... << endl;
}
try {
empty.addItem( );
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 above...
cout << "exception was caught. moving on... << endl;
}
try {
cs52::TrashCan t( -1, -1 );
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 above...
cout << "exception was caught. moving on... << endl;
}
return( 0 );
}
void setSize( int size );
int getSize( );
int getContents( );
void addItem( );
void empty( );
void cover( );
void uncover( );
void printCan( );
Sample Driver Code TrashCan Class#include <iostream>
#include <stdexcept>
#include "TrashCan.h"
int main( ) {
using namespace std;
using namespace cs52;
cout << "Welcome to Howie's TrashCan Program!" << endl;
TrashCan myCan;
TrashCan yourCan;
TrashCan empty( 0, 0 );
yourCan.setSize( 12 );
myCan.setSize( 12 );
yourCan.addItem( );
yourCan.addItem( );
myCan.addItem( );
myCan.printCan();
yourCan.printCan();
// read in a TrashCan...
// the class designer for TrashCan (that's you!)
// gets to decide which fields matter and should be read in
cs52::TrashCan sample;
cin >> sample;
// print out a TrashCan...
// the class designer for TrashCan (that's you!)
// gets to decide which fields matter and should be printed
cout << sample << endl;
TrashCan combined = yourCan + myCan;
cout << "this drive's filled to " << combined.getContents( ) << endl;
TrashCan other = combined – myCan;
cout << "the other cup's filled to " << other.getContents( ) << endl;
if (combined > other) {
cout << "looks like combined is bigger..." << endl;
}
else {
cout << "looks like other is bigger..." << endl;
}
if (myCan > other) {
cout << "looks like myCan is bigger..." << endl;
}
else {
cout << "looks like other is bigger..." << endl;
}
if (yourCan < myCan) {
cout << "looks like yourCan is smaller..." << endl;
}
else {
cout << "looks like myCan 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 above...
cout << "exception was caught. moving on... << endl;
}
try {
empty.addItem( );
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 above...
cout << "exception was caught. moving on... << endl;
}
try {
cs52::TrashCan t( -1, -1 );
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 above...
cout << "exception was caught. moving on... << endl;
}
return( 0 );
}
TrashCan( );TrashCan(int size );
TrashCan(int size, int contents );
void setSize( int size );
int getSize( );
int getContents( );
void addItem( );
void empty( );
void cover( );
void uncover( );
void printCan( );
bool myIsCovered;int my_Size;
int my_Contents; stdlogic error logic_error( std:string message ) std: string what), std::string message;
Explanation / Answer
#include <iostream>
#include <stdexcept>
#include "TrashCan.h"
#include<flashdrive.h>
flshdrive ::flashdrive()
my_setsize=0;
my_item=0;
my_plugin=false;
flashdrive:: flashdrive(int setsize,int item,bool plugin)
{
my_setsieze=capacity;
my_item=add;
my_plugin=plugin;
}
void flashdrive::plugin()
{
my_plugin=true;
}
void flashdrive::pullout()
{
my_plaugin=fasle;
}
int main( ) {
using namespace std;
using namespace cs52;
cout << "Welcome to Howie's TrashCan Program!" << endl;
TrashCan myCan;
TrashCan yourCan;
TrashCan empty( 0, 0 );
yourCan.setSize( 12 );
myCan.setSize( 12 );
yourCan.addItem( );
yourCan.addItem( );
myCan.addItem( );
myCan.printCan();
yourCan.printCan();
cin >> sample;
cout << sample << endl;
TrashCan combined = yourCan + myCan;
cout << "this drive's filled to " << combined.getContents( ) << endl;
TrashCan other = combined – myCan;
cout << "the other cup's filled to " << other.getContents( ) << endl;
if (combined > other) {
cout << "looks like combined is bigger..." << endl;
}
else {
cout << "looks like other is bigger..." << endl;
}
if (myCan > other) {
cout << "looks like myCan is bigger..." << endl;
}
else {
cout << "looks like other is bigger..." << endl;
}
if (yourCan < myCan) {
cout << "looks like yourCan is smaller..." << endl;
}
else {
cout << "looks like myCan is smaller..." << endl;
}
try {
empty = empty – combined;
cout << "something not right here..." << endl;
} catch( std::logic_error ) {
cout << "exception was caught. moving on... << endl;
}
try {
empty.addItem( );
cout << "something not right here..." << endl;
} catch( std::logic_error ) {
cout << "exception
}
try {
cs52::TrashCan t( -1, -1 );
cout << "something not right here..." << endl;
} catch( std::logic_error ) {
cout << "exception was caught. moving on... << endl;
}
return( 0 );
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.