CS 52: Assignment #8 Solved

25.00 $

Category:
Click Category Button to View Your Next Assignment | Homework

You'll get a download link with a: zip solution files instantly, after Payment

Securely Powered by: Secure Checkout

Description

5/5 - (1 vote)

Background:
The purpose of this assignment is to practice dealing with exception handling.  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.

part_a: flashdrive 3.0 (exceptions)

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.  You can acquire the source to the FlashDrive class by clicking here.  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.  Once you get it all working correctly, the driver code should run as described in class.

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.

FlashDrive
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( );

int my_StorageCapacity;
int my_StorageUsed;
bool my_IsPluggedIn;
Sample Driver Code
#include <iostream>
#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 cup’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…
}
}

 

Overall

  • Your submission should follow this organization:
    • part_a
      • main.cpp
      • flashdrive_3_0.h
      • flashdrive_3_0.cpp
  • 08_exceptions.zip