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

C++ Exception Handling Program - Need this done in Xcode. The TrashCan class cod

ID: 3577362 • Letter: C

Question

C++ Exception Handling Program - Need this done in Xcode.

The TrashCan class code is provided at the bottom of this post.

Class Details

Sample Driver

TrashCan

void setSize( int amount );
void addItem( );
void empty( );
void cover( );
void unconver( );

void printCan( );

TrashCan yours;
TrashCan mine;
TrashCan test;

yours.setSize( 10 );
mine.setSize( 10 );
test.setSize( 5 );

yours.addItem( );
mine.addItem( );
mine.addItem( );
mine.addItem( );
mine.addItem( );
mine.addItem( );
test.addItem( );
test.addItem( );
test.addItem( );
test.addItem( );
test.addItem( );


cout << test << endl;

try {
// contents will become negative...
// an exception should get thrown
test = yours - mine;
} catch( std::logic_error le ) {
cout << "subtraction failed" << endl;
}


/// test should be unchanged
cout << test << endl;

try {
// how can you have a negative size
// an exception should get thrown
test = TrashCan( -100 );
} catch( std::logic_error le ) {
cout << "constructor failed" << endl;
}

/// test should be unchanged
cout << test << endl;

try {
// how can you have 5 pieces of
// trash in a can that
// can only hold 3??
// an exception should get thrown
test.setSize( 3 );
} catch( std::logic_error le ) {
cout << "setSize failed" << endl;
}

/// test should be unchanged
cout << test << endl;

try {
// how can you have a negative
// contents value??
test = TrashCan( 10, -100 );
} catch( std::logic_error le ) {
cout << "constructor failed" << endl;
}

TrashCan.cpp:

#include "TrashCan.h"

TrashCan::TrashCan( ) {
   myIsCovered = false;
   my_Size = 0;
   my_Contents = 0;
}

TrashCan::TrashCan( int size ) {
   myIsCovered = false;
   my_Size = size;
   my_Contents = 0;
}

TrashCan::TrashCan( int size, int contents ) {
   myIsCovered = false;
   my_Size = size;
   my_Contents = contents;
}

void TrashCan::setSize( int size ) {
   my_Size = size;
}

int TrashCan::getSize( ) {
   return( my_Size );
}

int TrashCan::getContents( ) {
   return( my_Contents );
}

void TrashCan::addItem( ) {
   my_Contents = my_Contents + 1;
}
  
void TrashCan::empty( ) {
   my_Contents = 0;
}

void TrashCan::cover( ) {
   myIsCovered = true;
}

void TrashCan::uncover( ) {
   myIsCovered = false;
}

void TrashCan::printCan( ) {
   cout << "A TrashCan with a size=" << my_Size << " and containing " << my_Contents << " piece";
   if (my_Contents != 1) {
       cout << "s";
   }
   cout << " of trash" << endl;
}

Trashcan.h:

#ifndef TRASHCAN_H
#define TRASHCAN_H
#include <iostream>
using namespace std;


class TrashCan {
public:
   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( );

private:
   bool myIsCovered;
   int my_Size;
   int my_Contents;
};

#endif

Class Details

Sample Driver

TrashCan

TrashCan();
TrashCan( int size );
TrashCan( int size, int contents );

void setSize( int amount );
void addItem( );
void empty( );
void cover( );
void unconver( );

void printCan( );

bool myIsCovered;
int mySize;
int myContents;

TrashCan yours;
TrashCan mine;
TrashCan test;

yours.setSize( 10 );
mine.setSize( 10 );
test.setSize( 5 );

yours.addItem( );
mine.addItem( );
mine.addItem( );
mine.addItem( );
mine.addItem( );
mine.addItem( );
test.addItem( );
test.addItem( );
test.addItem( );
test.addItem( );
test.addItem( );


cout << test << endl;

try {
// contents will become negative...
// an exception should get thrown
test = yours - mine;
} catch( std::logic_error le ) {
cout << "subtraction failed" << endl;
}


/// test should be unchanged
cout << test << endl;

try {
// how can you have a negative size
// an exception should get thrown
test = TrashCan( -100 );
} catch( std::logic_error le ) {
cout << "constructor failed" << endl;
}

/// test should be unchanged
cout << test << endl;

try {
// how can you have 5 pieces of
// trash in a can that
// can only hold 3??
// an exception should get thrown
test.setSize( 3 );
} catch( std::logic_error le ) {
cout << "setSize failed" << endl;
}

/// test should be unchanged
cout << test << endl;

try {
// how can you have a negative
// contents value??
test = TrashCan( 10, -100 );
} catch( std::logic_error le ) {
cout << "constructor failed" << endl;
}

Background: The purpose of this assignment is to practice dealing with exception handling and textual data. Exception handling is a very important part of being an object-oriented programming. Rather returning some kind of int return value every time you tickle an object, C++ programmers expect methods to focus on their task at hand. If something bad happens, C++ programmers expect methods to throw exceptions. When caught, exceptions can be processed. When uncaught, they cause a program to terminate dead in its tracks. The typical pattern in this course is to have class code throw exceptions and driver code catch them. Because of exceptions, when writing client driver code, our try blocks will not be polluted with error processing statements. Our other topic in this unit is string handling. In C++, string is a class and is much much easier to work with as compared to the typical way a C programmers deal with char The string class gives us many different methods and the second programming project will ask you to learn some of these new capabilities Project 13: Exception TrashCan The purpose of this assignment is to work with exceptions. As you may recall, l have provided you with a sample class named TrashCan 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

Explanation / Answer

=======DRIVER========

#include "trashcan.h"
#include <iostream>
using namespace std;


int main( ) {
cout << "Welcome to TrashCan Program!" << endl;
TrashCan myCan;
TrashCan yourCan;
TrashCan nothing;

yourCan.setSize( 12 );
myCan.setSize( 12 );
nothing.setSize ( 0 );

yourCan.addItem( );
yourCan.addItem( );


myCan.addItem( );


myCan.printCan();
yourCan.printCan();

TrashCan combined = yourCan + myCan;
cout << "this can's filled to " << combined.getContents( ) << endl;

TrashCan other = combined - myCan;
cout << "the other can'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 {
TrashCan 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 {
nothing.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;

}


return( 0 );
}
=====trashcan.cpp========

#include "trashcan.h"
#include <iostream>
#include <cstdlib>

using namespace std;

TrashCan::TrashCan( ) {
myIsCovered = false;
my_Size = 0;
my_Contents = 0;
my_Empty = 0;
}

TrashCan::TrashCan( int size ) {
myIsCovered = false;
my_Size = size;

my_Contents = 0;
my_Empty = 0;
}

TrashCan::TrashCan( int size, int contents, int empty ) {
myIsCovered = false;
my_Size = size;
my_Contents = contents;
my_Empty = empty;
}

void TrashCan::setSize( int size ) {
if (size < 0 ){
throw logic_error("exception was caught. moving on...");
}
else{
my_Size = size;
}
}

int TrashCan::getSize( ) {
return( my_Size );

}

int TrashCan::getContents( ) {
return( my_Contents );
}

void TrashCan::addItem( ) {
my_Contents = my_Contents + 1;
if (my_Contents > my_Size || my_Contents < my_Size){
throw logic_error("exception was caught. moving on...");
}
}

void TrashCan::empties( ) {
my_Contents = 0;
}

void TrashCan::cover( ) {
myIsCovered = true;
}

void TrashCan::uncover( ) {
myIsCovered = false;
}

void TrashCan::printCan( ) {
cout << "A TrashCan with a size=" << my_Size << " and containing " << my_Contents << " piece";
if (my_Contents != 1) {
cout << "s";
}
cout << " of trash" << endl;
}

TrashCan operator+ ( const TrashCan& yourCan, const TrashCan& myCan ) { /* This is where I override the + and - operators... I put in the error messages for over filling and under filling the trashcan */

TrashCan combined;
combined.my_Contents = yourCan.my_Contents + myCan.my_Contents ;


return( combined );

}

TrashCan operator- ( const TrashCan& combined, const TrashCan& myCan ){
TrashCan other;
TrashCan empty;
empty.my_Contents = -1;

if ( empty.my_Contents < 0 ){
throw logic_error("exception was caught. moving on...");
}
else
{
other.my_Contents = combined.my_Contents - myCan.my_Contents;
return (other );
}
}

bool operator> ( const TrashCan& myCan, const TrashCan& yourCan) { /* MY bool override for the '< and > ' operators. */
TrashCan combined;
combined.my_Contents = yourCan.my_Contents + myCan.my_Contents ;

TrashCan empty;
empty.my_Empty = 0;


TrashCan other;
other.my_Contents = combined.my_Contents - myCan.my_Contents;


return combined.my_Contents > other.my_Contents && myCan.my_Contents > other.my_Contents;

}

bool operator< (const TrashCan& myCan, const TrashCan& yourCan ) {
TrashCan combined;
combined.my_Contents = yourCan.my_Contents + myCan.my_Contents ;

TrashCan other;
other.my_Contents = combined.my_Contents - myCan.my_Contents;

if (yourCan.my_Contents < myCan.my_Contents ){
return yourCan.my_Contents > myCan.my_Contents ; }
else {
return yourCan.my_Contents < myCan.my_Contents;
}

}


ostream& operator<< (ostream &out, TrashCan& myCan)
{
out <<myCan.my_Contents << " Contents in this trashcan";
return out;
}
======HEADER=========

#ifndef TRASHCAN_H
#define TRASHCAN_H
#include <iostream>
using namespace std;


class TrashCan {
public:
TrashCan( );
TrashCan( int size );
TrashCan( int size, int contents, int empty );

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

friend TrashCan operator + ( const TrashCan& yourCan, const TrashCan& myCan ); /*as the assignment asks... I override the +,-, and <,> operators. */
friend TrashCan operator - ( const TrashCan& combined, const TrashCan& myCan); /*I have used the bool statements for the <,> to compare sizes */
friend bool operator > ( const TrashCan& myCan, const TrashCan& yourCan);
friend bool operator < ( const TrashCan& myCan, const TrashCan& yourCan );

friend ostream& operator<< (ostream &out, TrashCan& myCan);


void printCan( );

private:
bool myIsCovered;
int my_Size;
int my_Contents;
int my_Empty;
};

#endif

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote