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

part_a: flashdrive 3.0 (exceptions) The purpose of this assignment is to work wi

ID: 3717547 • Letter: P

Question

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

}

}

main.cpp

flashdrive_3_0.h

flashdrive_3_0.cpp

Explanation / Answer

Answer :

//main.cpp

#include <iostream>
#include "ExceptionFlashDrive.h"
#include <stdexcept> // supports Linux exceptions

using namespace std;
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;
}
  
FlashDrive drive2(20, 0, false);

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


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");
}
}


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");
}
}


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);
}


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);
}


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;
}

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