Background: The purpose of this assignment is to practice dealing with exception
ID: 3665829 • Letter: B
Question
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 11: Exception 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 ( VS2010 or VS2012 or XCode5 or XCode6 or XCode7 ) 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 <stdexcept> to begin working with logic_error, but Visual Studio (being a badly behaved child...) let's you get away with it.
Although the sample driver code might not code for all these circumstances, I would like you to throw exceptions whenever:
a negative number is potentially stored in myStorageCapacity (due to calls to operator - or setCapacity)
a negative number is potentially stored in myStorageUsed (due to calls to operator - or setUsed)
having a myStorageUsed that exceeds myStorageCapacity (due to calls to operator + or writeData or setUsed)
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.
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.
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( );
FlashDrive drive1( 10, 0, false );
FlashDrive drive2( 20, 0, false );
drive1.plugIn( );
drive1.formatDrive( );
drive1.writeData( 5 );
drive1.pullOut( );
drive2.plugIn( );
drive2.formatDrive( );
drive2.writeData( 1 );
drive2.pullOut( );
FlashDrive combined = drive1 + drive2;
cout << "this drive's filled to " << combined.getUsed( ) << endl;
FlashDrive other = combined – drive1;
cout << "the other drives'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;
}
cout << "Here is drive1...." << endl;
cout << drive1 << endl;
cout << "Let's set drive1...." << endl;
cin >> drive1;
cout << "Here is drive1. Notice the changes..." << endl;
cout << drive1 << endl;
// Let's try to get some exceptions...
try {
drive1.writeData( -1000 );
cout << "This should not print out..." << endl;
} catch (std::logic_error) {
cout << "Exception correctly caught..." << endl;
}
try {
drive1.setUsed( -1000 );
cout << "This should not print out..." << endl;
} catch (std::logic_error) {
cout << "Exception correctly caught..." << endl;
}
try {
drive1.setCapacity( -1000 );
cout << "This should not print out..." << endl;
} catch (std::logic_error le) {
cout << "Exception correctly caught..." << le.what( ) << endl;
}
try {
FlashDrive f( 10, 20, false );
cout << "This should not print out..." << endl;
} catch (std::logic_error) {
cout << "Exception correctly caught..." << endl;
}
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;
FlashDrive drive1( 10, 0, false );
FlashDrive drive2( 20, 0, false );
drive1.plugIn( );
drive1.formatDrive( );
drive1.writeData( 5 );
drive1.pullOut( );
drive2.plugIn( );
drive2.formatDrive( );
drive2.writeData( 1 );
drive2.pullOut( );
FlashDrive combined = drive1 + drive2;
cout << "this drive's filled to " << combined.getUsed( ) << endl;
FlashDrive other = combined – drive1;
cout << "the other drives'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;
}
cout << "Here is drive1...." << endl;
cout << drive1 << endl;
cout << "Let's set drive1...." << endl;
cin >> drive1;
cout << "Here is drive1. Notice the changes..." << endl;
cout << drive1 << endl;
// Let's try to get some exceptions...
try {
drive1.writeData( -1000 );
cout << "This should not print out..." << endl;
} catch (std::logic_error) {
cout << "Exception correctly caught..." << endl;
}
try {
drive1.setUsed( -1000 );
cout << "This should not print out..." << endl;
} catch (std::logic_error) {
cout << "Exception correctly caught..." << endl;
}
try {
drive1.setCapacity( -1000 );
cout << "This should not print out..." << endl;
} catch (std::logic_error le) {
cout << "Exception correctly caught..." << le.what( ) << endl;
}
try {
FlashDrive f( 10, 20, false );
cout << "This should not print out..." << endl;
} catch (std::logic_error) {
cout << "Exception correctly caught..." << endl;
}
Explanation / Answer
main.cpp
//header files
#include <iostream>
#include "ExceptionFlashDrive.h"
#include <stdexcept> // supports Linux exceptions
using namespace std;
// define main function
int main()
{
FlashDrive drive1(10, 0, false);
try
{
FlashDrive drive2(10, 20, false);
cout << "This should not print out..." << endl;
}
catch (std::logic_error)
{
cout << "Exception correctly caught..." << endl;
}
// call function
FlashDrive drive2(20, 0, false);
// call function
drive1.plugIn();
drive1.formatDrive();
drive1.writeData(5);
drive1.pullOut();
drive2.plugIn();
drive2.formatDrive();
drive2.writeData(1);
drive2.pullOut();
try
{
FlashDrive combined = drive1 - drive2;
cout << "this drive's filled to " << combined.getUsed() << endl;
cout << "This should not print out..." << endl;
}
catch (std::logic_error)
{
cout << "Exception correctly caught..." << endl;
}
FlashDrive combined = drive1 + drive2;
cout << "this drive's filled to " << combined.getUsed() << endl;
FlashDrive other = combined - drive1;
cout << "the other drives's filled to " << other.getUsed() << endl;
cout << "the other drives'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;
}
cout << "Here is drive1...." << endl;
cout << drive1 << endl;
cout << "Let's set drive1...." << endl;
try
{
cin >> drive1;
}
catch (std::logic_error)
{
cout << "Exception correctly caught..." << endl;
}
cin >> drive1;
cout << "Here is drive1. Notice the changes..." << endl;
cout << drive1 << endl;
// Let's try to get some exceptions...
try
{
drive1.writeData(-1000);
cout << "This should not print out..." << endl;
}
catch (std::logic_error)
{
cout << "Exception correctly caught..." << endl;
}
try
{
drive1.setUsed(-1000);
cout << "This should not print out..." << endl;
}
catch (std::logic_error)
{
cout << "Exception correctly caught..." << endl;
}
try
{
drive1.setCapacity(-1000);
cout << "This should not print out..." << endl;
}
catch (std::logic_error le)
{
cout << "Exception correctly caught..." << le.what() << endl;
}
try
{
FlashDrive f(10, 20, false);
cout << "This should not print out..." << endl;
}
catch (std::logic_error)
{
cout << "Exception correctly caught..." << endl;
}
}
ExceptionFlashDrive.cpp
/*
Purpose: Using FlashDrive.cpp from assignment 7, modify the code so that it
throw exceptions whenever:
- a negative number is potentially stored in my_StorageCapacity (due to calls
to operator - or setCapacity);
- a negative number is potentially stored in my_StorageUsed (due to calls to
operator - or setUsed);
- having a my_StorageUsed that exceeds my_StorageCapacity (due to calls to
operator + or writeData or setUsed)
*/
#include <iostream>
#include "ExceptionFlashDrive.h"
#include <stdexcept> // supports Linux exceptions
using namespace std;
FlashDrive::FlashDrive()
{
my_StorageCapacity = 0;
my_StorageUsed = 0;
my_IsPluggedIn = false;
}
FlashDrive::FlashDrive(int capacity, int used, bool pluggedIn)
{
if (capacity >= 0 && used >= 0 && capacity >= used)
{
my_StorageCapacity = capacity;
my_StorageUsed = used;
my_IsPluggedIn = pluggedIn;
}
else
{
//cerr << "Error in FlashDrive..." << endl;
throw logic_error("Bad input values");
}
}
void FlashDrive::plugIn()
{
my_IsPluggedIn = true;
}
void FlashDrive::pullOut()
{
my_IsPluggedIn = false;
}
void FlashDrive::writeData(int amount)
{
// throw exception if a negative number or having a value
// that exceeds my_StorageCapacity is potentially stored in my_StorageUsed
if (0 <= amount && (my_StorageUsed + amount) <= my_StorageCapacity)
{
my_StorageUsed += amount;
}
else {
//cerr << "Error in writeData..." << endl;
throw logic_error("Bad amount value");
}
}
void FlashDrive::eraseData(int amount)
{
my_StorageUsed -= amount;
}
void FlashDrive::formatDrive()
{
my_StorageUsed = 0;
}
int FlashDrive::getCapacity()
{
return(my_StorageCapacity);
}
void FlashDrive::setCapacity(int amount)
{
// throw exception if a negative number is potentially
// stored in myStorageCapacity
if (amount >= 0)
{
my_StorageCapacity = amount;
}
else
{
//cerr << "Error in setCapacity..." << endl;
throw logic_error("Bad amount value");
}
}
int FlashDrive::getUsed()
{
return(my_StorageUsed);
}
void FlashDrive::setUsed(int amount)
{
// throw exception if a negative number or having a value
// that exceeds myStorageCapacity is potentially stored in myStorageUsed
if ( 0 <= amount && amount <= my_StorageCapacity )
{
my_StorageUsed = amount;
}
else
{
//cerr << "Error in setUsed..." << endl;
throw logic_error("Bad amount value");
}
}
bool FlashDrive::isPluggedIn()
{
return(my_IsPluggedIn);
}
/**
* @Overload '+' operator.
* Creates and returns a new FlashDrive from the two arguments by combining
* their contents (add-up values of their capacities; used capacities, sets
* it to the unplugged condition). Throw exception if a negative number or
* value that exceeds myStorageCapacity is potentially stored in
* my_StorageCapacity or setUsed.
*/
FlashDrive operator + (const FlashDrive& drive1, const FlashDrive& drive2)
{
int new_my_StorageCapacity = drive1.my_StorageCapacity +
drive2.my_StorageCapacity;
int new_my_StorageUsed = drive1.my_StorageUsed + drive2.my_StorageUsed;
// here, it is enough to check that capacity is not less than the used
// space, the rest will be catched in setCapacity and setUsed.
if (new_my_StorageCapacity >= new_my_StorageUsed)
{
FlashDrive newFlashDrive = FlashDrive();
newFlashDrive.setCapacity(new_my_StorageCapacity);
newFlashDrive.setUsed(new_my_StorageUsed);
// or FlashDrive newFlashDrive = FlashDrive(new_my_StorageCapacity,
// new_my_StorageUsed, false);
// return the created FlashDrive
return newFlashDrive;
}
else
{
//cerr << "Error in +..." << endl;
throw logic_error("Bad input value");
}
}
/**
* @Overload '-' operator.
* Creates and returns a new FlashDrive from the two arguments by combining
* their contents (subtract values of their capacities; used capacities, sets
* it to the unplugged condition). Throw exception if a negative number or
* value that exceeds myStorageCapacity is potentially stored in
* my_StorageCapacity or setUsed.
*/
FlashDrive operator - (const FlashDrive& drive1, const FlashDrive& drive2)
{
int new_my_StorageCapacity = drive1.my_StorageCapacity -
drive2.my_StorageCapacity;
int new_my_StorageUsed = drive1.my_StorageUsed - drive2.my_StorageUsed;
// here, it is enough to check that capacity is not less than the used
// space, the rest will be catched in setCapacity and setUsed.
if (new_my_StorageCapacity >= new_my_StorageUsed)
{
FlashDrive newFlashDrive = FlashDrive();
newFlashDrive.setCapacity(new_my_StorageCapacity);
newFlashDrive.setUsed(new_my_StorageUsed);
// or FlashDrive newFlashDrive = FlashDrive(new_my_StorageCapacity,
// new_my_StorageUsed, false);
// return the created FlashDrive
return newFlashDrive;
}
else
{
//cerr << "Error in -..." << endl;
throw logic_error("Bad input value");
}
}
/**
* @Overload '<' operator.
* Returns true if the amount of free space on drive1 is less than on drive2,
* false otherwise.
*/
bool operator < (const FlashDrive& drive1, const FlashDrive& drive2)
{
return (drive1.my_StorageCapacity - drive1.my_StorageUsed <
drive2.my_StorageCapacity - drive2.my_StorageUsed);
/* The second option is to compare the flash drives by their total capacity
* In order to use it comment above return statement and uncomment return
* statement below
*/
//return(drive1.my_StorageCapacity < drive2.my_StorageCapacity);
}
/**
* @Overload '>' operator.
* Returns true if the amount of free space on drive1 is bigger than on drive2,
* false otherwise.
*/
bool operator > (const FlashDrive& drive1, const FlashDrive& drive2)
{
return (drive1.my_StorageCapacity - drive1.my_StorageUsed >
drive2.my_StorageCapacity - drive2.my_StorageUsed);
/* The second option is to compare the flash drives by their total capacity
* In order to use it comment above return statement and uncomment return
* statement below
*/
//return(drive1.my_StorageCapacity > drive2.my_StorageCapacity);
}
/**
* @Overload '<<' operator.
* Prints out values of the private member variables of the FlashDrive class.
* Uses iostream:
*/
ostream & operator << (ostream& outs, const FlashDrive& drive)
{
outs << "Capacity: " << drive.my_StorageCapacity << " kilobytes; " << endl;
outs << "Used space: " << drive.my_StorageUsed << " kilobytes; " << endl;
outs << "Free space: " << (drive.my_StorageCapacity - drive.my_StorageUsed)
<< " kilobytes; " << endl;
outs << "The flash drive is "
<< (drive.my_IsPluggedIn == true ? "plugged-in. " : "unplugged. ");
return outs;
}
/**
* @Overload '>>' operator.
* Reads-in and sets the values of the private member variables of
* the FlashDrive class.
* Uses iostream:
*/
istream & operator >> (istream& ins, FlashDrive& drive)
{
// Prompt the user to enter the capacity of the drive.
int capacity;
cout << "Enter the capacity of the flash drive in kilobytes ";
cout << "(positive integer): ";
ins >> capacity;
// Prompt the user to enter the used capacity of the drive.
int usedCapacity;
cout << "Enter the used capacity of the flash drive in kilobytes ";
cout << "(non-negative integer): ";
ins >> usedCapacity;
// Prompt the user to enter the state of the drive.
bool isPluggedIn;
do
{
cout << "Is it plugged-in [1=yes/0=no]?: ";
ins >> isPluggedIn;
if (isPluggedIn == 0 || isPluggedIn == 1)
{
break;
}
ins.clear();
} while (true);
// Assign the entered values to the private variables of FlashDrive.
if (capacity >= usedCapacity)
{
drive.setCapacity(capacity);
drive.setUsed(usedCapacity);
(isPluggedIn == 1) ? drive.plugIn() : drive.pullOut();
// return the created FlashDrive
return ins;
}
else
{
//cerr << "Error in operator >> ..." << endl;
throw logic_error("Bad input value");
}
}
ExceptionFlashDrive.h
#ifndef FLASHDRIVE_H
#define FLASHDRIVE_H
#include <iostream>
using namespace std;
class FlashDrive
{
public:
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( );
friend FlashDrive operator + ( const FlashDrive & drive1, const FlashDrive & drive2 );
friend FlashDrive operator - (const FlashDrive & drive1, const FlashDrive & drive2);
friend bool operator < (const FlashDrive & drive1, const FlashDrive & drive2);
friend bool operator > (const FlashDrive & drive1, const FlashDrive & drive2);
friend ostream & operator << (ostream & outs, const FlashDrive & drive);
friend istream & operator >> (istream & ins, FlashDrive & drive);
private:
int my_StorageCapacity; // in kilobytes
int my_StorageUsed; // in kilobytes
bool my_IsPluggedIn; // am I attached to a computer?
};
#endif
Sample Output
Exception correctly caught...
Exception correctly caught...
this drive's filled to 6
the other drives's filled to 1
the other drives's filled to 1
looks like combined is bigger...
looks like other is bigger...
looks like drive1 is smaller...
Here is drive1....
Capacity: 10 kilobytes;
Used space: 5 kilobytes;
Free space: 5 kilobytes;
The flash drive is unplugged.
Let's set drive1....
Enter the capacity of the flash drive in kilobytes
(positive integer): 50
Enter the used capacity of the flash drive in kilobytes
(non-negative integer): 25
Is it plugged-in [1=yes/0=no]?: 1
Enter the capacity of the flash drive in kilobytes
(positive integer): 10
Enter the used capacity of the flash drive in kilobytes
(non-negative integer): 2
Is it plugged-in [1=yes/0=no]?: 0
Here is drive1. Notice the changes...
Capacity: 10 kilobytes;
Used space: 2 kilobytes;
Free space: 8 kilobytes;
The flash drive is unplugged.
Exception correctly caught...
Exception correctly caught...
Exception correctly caught...Bad amount value
Exception correctly caught...
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.