Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C++ please. Project 1: Exception/Namespace TrashCan The purpose of this assig

ID: 3764697 • Letter: I

Question

In C++ please.

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 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 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.

TrashCan Class

void setSize( int size );
int  getSize( );
int getContents( );
void addItem( );
void empty( );
void cover( );
void uncover( );

void printCan( );

#include
#include
#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 Sample Driver Code

TrashCan Class

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;

#include
#include
#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 );

}

Explanation / Answer

#ifndef FLASHDRIVE_H #define FLASHDRIVE_H #include #include namespace cs52 { class FlashDrive { friend FlashDrive operator+ (FlashDrive used1 , FlashDrive used2); friend FlashDrive operator- (FlashDrive used3, FlashDrive used4 ); FlashDrive.h public: FlashDrive& FlashDrive::operator=(int); FlashDrive::FlashDrive(int); FlashDrive& operator = (const FlashDrive& usedtotal){ my_StorageUsed= usedtotal.my_StorageUsed; return *this; } FlashDrive( ); FlashDrive( int capacity, int used, bool pluggedIn ); void plugIn( ); void pullOut( ); void writeData( int amount ); void eraseData( int amount ); void formatDrive( ); int getCapacity( ); void setCapacity( int amount ); int getUsed( ); void setUsed( int amount ); bool isPluggedIn( ); private: int my_StorageCapacity; // in kilobytes int my_StorageUsed; // in kilobytes bool my_IsPluggedIn; // am I attached to a computer? }extern drive1,drive2; inline FlashDrive operator+ (FlashDrive used1, FlashDrive used2 ) { FlashDrive plus; plus.my_StorageUsed = (used1.getUsed()+ used2.getUsed()); return plus; } inline bool operator< (FlashDrive &lhs,FlashDrive &rhs ) { return ( lhs.getUsed() (FlashDrive &lhs,FlashDrive &rhs ) { return ( operator