Question

Project 2 - Flashdrive 1.0 I have provided you with a sample class named FlashDrive which...

Project 2 - Flashdrive 1.0

I have provided you with a sample class named FlashDrive which has been diagrammed below and described in the online course content. For this unit, I'd like you to identify additional member variables (data) that would be appropriate for the class FlashDrive. I would also like you to identify additional methods (functions) that would be appropriate for the class FlashDrive. Submit a .h file for FlashDrive with the new possible methods and members you have identified commented and explained. YOU ARE NOT REQUIRED TO CODE UP ANYTHING MORE THAN THE .H FILE, BUT I MAY AWARD YOU EXTRA CREDIT IF YOU DO...

FlashDrive methods:

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 data members:

int my_StorageCapacity;
int my_StorageUsed;
bool my_IsPluggedIn;

Sample driver code

#include <iostream>
#include "FlashDrive.h"

void main( )
{
  int i;
  cout << "Welcome to Howie's Flash USB Drive Store!"
       << endl;
  cout << "How much memory do you need? ";
  cin >> i;

  FlashDrive f(i, 0, false);
  cout << "Let's format the drive..." << endl;
  f.formatDrive();
  cout << "Let's plug in the drive..." << endl;
  f.plugIn();
  cout << "Let's write some data..." << endl;
  f.writeData( 10 );
  cout << "Let's unplug the drive..." << endl;
  f.pullOut();

  cout << "Enjoy your drive!!" << endl;
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER :

/main.cpp

#include <iostream>

#include <string>

#include "FlashDrive.h"

using namespace std;

using namespace cs52;

int main()

{

cs52::FlashDrive drive1(10, 0, false);

cs52::FlashDrive drive2(20, 0, false);

drive1.plugIn();

std :: cout << "This should return as TRUE: " << drive1.isPluggedIn() << std::endl;

drive1.formatDrive();

drive1.writeData(5);

std :: cout << "This should return as 5: " << drive1.getUsed() << std::endl;

drive1.pullOut();

std :: cout << "This should return as FALSE: " << drive1.isPluggedIn() << std::endl << std::endl;

drive2.plugIn();

drive2.formatDrive();

drive2.writeData(1);

drive2.pullOut();

cs52::FlashDrive combined = drive1 + drive2;

cout << "combined drive's filled to " << combined.getUsed() << endl;

cout << "its capacity is " << combined.getCapacity() << endl;

cout << "Is it plugged in by default? " << combined.isPluggedIn() << endl << endl;

cs52::FlashDrive other = combined - drive1;

cout << "other(diff)'s filled to " << other.getUsed() << endl;

cout << "its capacity is " << other.getCapacity() << endl;

cout << "Is it plugged in by default? " << other.isPluggedIn() << endl << endl;

if (combined > other) {

cout << "looks like combined is bigger than other..." << endl;

}

else {

cout << "this should not print" << endl;

cout << "looks like other is bigger..." << endl;

}

if (drive2 > other) {

cout << "looks like drive2 is bigger than other..." << endl;

}

else {

cout << "looks like other is bigger than drive 2..." << endl;

}

if (drive2 < drive1) {

cout << "looks like drive2 is smaller than drive1..." << endl;

}

else {

cout << "looks like drive1 is smaller than drive2..." << endl;

}

}//END OF MAIN

==============================================================================

//FlashDrive.cpp

#include <iostream>

#include <string>

#include "FlashDrive.h"

using namespace std;

using namespace cs52;

namespace cs52 {

//constructors:

//DEFAULT:

FlashDrive::FlashDrive() {

my_IsPluggedIn = 0;

my_StorageCapacity = 0;

my_StorageUsed = 0;

}

FlashDrive::FlashDrive(int capacity, int used, bool pluggedIn) {

bool okInput(1);

//potentially unwanted stuff:

if (capacity < 0) {

cout << "ERROR: Capacity can only be positive." << endl;

okInput = 0;

}

if (used < 0) {

cout << "ERROR: Used data can only be positive " << endl;

okInput = 0;

}

if (used > capacity) {

cout << "ERROR: The amount of used data cannot exceed the capacity." << endl;

okInput = 0;

}

if (okInput) {

my_IsPluggedIn = pluggedIn;

my_StorageCapacity = capacity;

my_StorageUsed = used;

}

else {

my_IsPluggedIn = 0;

my_StorageCapacity = 0;

my_StorageUsed = 0;

}

}

//***************************************************************************************

//PLUG IN/PULL OUT

void FlashDrive::plugIn() {

if (my_IsPluggedIn)

cout << "The FlashDrive is already plugged in! " << endl;

else

my_IsPluggedIn = 1;

}

void FlashDrive::pullOut() {

if (my_IsPluggedIn)

my_IsPluggedIn = 0;

else

cout << "The FlashDrive is already pulled out! " << endl;

}

void FlashDrive::writeData(int amount) {

if (my_IsPluggedIn) {

if ((amount + my_StorageUsed) > my_StorageCapacity)

cout << "ERROR: Adding desired data will exceed storage capacity. " << endl;

else

my_StorageUsed += amount;

}

else

cout << "The FlashDrive must be plugged in in order to write to it! " << endl;

}

void FlashDrive::eraseData(int amount) {

if (my_IsPluggedIn) {

if ((my_StorageUsed - amount) < 0)

cout << "ERROR: Deleting desired data results in negative storage. " << endl;

else

my_StorageUsed -= amount;

}

else

cout << "The FlashDrive must be plugged in in order to erase from it! " << endl;

}

//******************************************************************************************

//This deletes all data from the flashdrive.

void FlashDrive::formatDrive() {

my_StorageUsed = 0;

}

int FlashDrive::getCapacity() {

return(my_StorageCapacity);

}

/****************************************************************************************/

void FlashDrive::setCapacity(int amount) {

if (amount >= my_StorageUsed)

my_StorageCapacity = amount;

else

cout << "ERROR: The formatted storage capacity must exceed or equate to the storage used." << endl;

}

/*****************************************************************************************/

int FlashDrive::getUsed() {

return(my_StorageUsed);

}

/*******************************************************************************************/

void FlashDrive::setUsed(int amount) {

if (amount > my_StorageCapacity)

cout << "ERROR: The amount of used data cannot exceed the storage capacity." << endl;

else

my_StorageUsed = amount;

}

//this returns true if the flashdrive is plugged in.

bool FlashDrive::isPluggedIn() {

return(my_IsPluggedIn);

}

/**************************************************************************************************/

//This overloads the " + " operator. This intakes two flashdrives and returns a new instance spawned with

//combined capacities and combined storages, that is plugged in.

FlashDrive operator +(const FlashDrive& flash1, const FlashDrive& flash2) {

FlashDrive temp = FlashDrive((flash1.my_StorageCapacity + flash2.my_StorageCapacity), (flash1.my_StorageUsed + flash2.my_StorageUsed), 1);

return(temp);

}

FlashDrive operator -(const FlashDrive& flash1, const FlashDrive& flash2) {

bool okInput(1);

if ((flash1.my_StorageCapacity - flash2.my_StorageCapacity) < 0) {

cout << "ERROR: Capacity can only be positive." << endl;

okInput = 0;

}

if ((flash1.my_StorageUsed - flash2.my_StorageUsed) < 0) {

cout << "ERROR: Used data can only be positive " << endl;

okInput = 0;

}

// if used > capacity :

if ((flash1.my_StorageUsed - flash2.my_StorageUsed) > (flash1.my_StorageCapacity - flash2.my_StorageCapacity)) {

cout << "ERROR: The amount of used data cannot exceed the capacity." << endl;

okInput = 0;

}

//if none of the potential pitfalls occured, return the appropriate "differentiated" FlashDrive. Else, return an empty one.

if (okInput) {

FlashDrive temp = FlashDrive((flash1.my_StorageCapacity - flash2.my_StorageCapacity), (flash1.my_StorageUsed - flash2.my_StorageUsed), 1);

return(temp);

}

else

{

FlashDrive temp = FlashDrive();

return(temp);

}

}

bool operator >(const FlashDrive& flash1, const FlashDrive& flash2) {

if (flash1.my_StorageCapacity > flash2.my_StorageCapacity)

return(1);

else

return(0);

}

bool operator <(const FlashDrive& flash1, const FlashDrive& flash2) {

if (flash1.my_StorageCapacity < flash2.my_StorageCapacity)

return(1);

else

return(0);

}

} //end of NAMESPACE definition

================================================================================

//FlashDrive.h

#ifndef FLASHDRIVE

#define FLASHDRIVE

#include <iostream>

#include <string>

using namespace std;

namespace cs52 {

class FlashDrive {

public:

//constructors:

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

//OVERLOADED OPERATORS:

friend FlashDrive operator +(const FlashDrive& flash1, const FlashDrive& flash2);

friend FlashDrive operator -(const FlashDrive& flash1, const FlashDrive& flash2);

friend bool operator >(const FlashDrive& flash1, const FlashDrive& flash2);

friend bool operator <(const FlashDrive& flash1, const FlashDrive& flash2);

private:

int my_StorageCapacity;

int my_StorageUsed;

bool my_IsPluggedIn;

}; //end of class declaration

} //end of namespace inclusion

#endif

==========================================================================

sample output :-

9 TDM-GCC 4.9.2 32-bi - X File Edit Search View Project Execute Tools AStyle Window Help 5. (globals) Project Classes C:\Prog

Hope it helps... please give an upvote. It's very important to me... thank you:)

Add a comment
Know the answer?
Add Answer to:
Project 2 - Flashdrive 1.0 I have provided you with a sample class named FlashDrive which...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I have provided you with a sample class named FlashDrive which has been diagrammed below. Using...

    I have provided you with a sample class named FlashDrive which has been diagrammed below. Using the FlashDrive class provided earlier, upgrade the class so that it supports various operators. Make operator+ combine together the contents of two FlashDrive, as long as the contents does not exceed the size. Make operator- subtract one FlashDrive contents from another, as long as the size or contents don't go negative. Support the >> and << operators to allow instances to be read from...

  • Please zoom in so the pictures become high resolution. I need three files, FlashDrive.cpp, FlashDrive.h and...

    Please zoom in so the pictures become high resolution. I need three files, FlashDrive.cpp, FlashDrive.h and user_main.cpp. The programming language is C++. I will provide the Sample Test Driver Code as well as the codes given so far in text below. Sample Testing Driver Code: 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(2); drive2.pullOut(); cs52::FlashDrive combined = drive1 + drive2; // std::cout << "this drive's filled to " << combined.getUsed( )...

  • Background: The purpose of this assignment is to practice dealing with exception handling and textual data....

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

  • Background: This assignment deals with inheritance. Inheritance is one of the major principles of object-oriented programming....

    Background: This assignment deals with inheritance. Inheritance is one of the major principles of object-oriented programming. In C++, one of the biggest goals is "code reuse". Inheritance accomplishes this. In order to get inheritance working in C++, you must get both the structure of your .h files as well as the implementation of your constructor correct. Constructor implementations must use an initialization list. Please review the book and the online content on these issues so you know how it works...

  • Project 1 - Operator Overloading (FlashDrive 2.0) In C++, many of the keyboard symbols that are...

    Project 1 - Operator Overloading (FlashDrive 2.0) In C++, many of the keyboard symbols that are used between two variables can be given a new meaning. This feature is called operator overloading and it is one of the most popular C++ features. Any class can choose to provide a new meaning to a keyboard symbol. Not every keyboard letter is re-definable in this way, but many of the ones we have encountered so far are like +, -, *, /,...

  • Help please with a Project in C++ In C++, many of the keyboard symbols that are used between two ...

    Help please with a Project in C++ In C++, many of the keyboard symbols that are used between two variables can be given a new meaning. This feature is called operator overloading and it is one of the most popular C++ features. Any class can choose to provide a new meaning to a keyboard symbol. Not every keyboard letter is re-definable in this way, but many of the ones we have encountered so far are like +, -, *, /,...

  • Requirements I have already build a hpp file for the class architecture, and your job is to imple...

    Requirements I have already build a hpp file for the class architecture, and your job is to implement the specific functions. In order to prevent name conflicts, I have already declared a namespace for payment system. There will be some more details: // username should be a combination of letters and numbers and the length should be in [6,20] // correct: ["Alice1995", "Heart2you", "love2you", "5201314"] // incorrect: ["222@_@222", "12306", "abc12?"] std::string username; // password should be a combination of letters...

  • C++ Language I have a class named movie which allows a movie object to take the...

    C++ Language I have a class named movie which allows a movie object to take the name, movie and rating of a movie that the user inputs. Everything works fine but when the user enters a space in the movie's name, the next function that is called loops infinetly. I can't find the source of the problem. Down below are my .cpp and .h file for the program. #include <iostream> #include "movie.h" using namespace std; movie::movie() { movieName = "Not...

  • Hello, I am working on a project for my C++ class, I have the vast majority...

    Hello, I am working on a project for my C++ class, I have the vast majority of the code figured out but am running into a few issues. Mainly updating each * to an X. The problem is as follows:(Airplane Seating Assignment) Write a program that can be used to assign seats for a commercial airplane. The airplane has 13 rows, with six seats in each row. Rows 1 and 2 are first class, rows 3 through 7 are business...

  • A library maintains a collection of books. Books can be added to and deleted from and...

    A library maintains a collection of books. Books can be added to and deleted from and checked out and checked in to this collection. Title and author name identify a book. Each book object maintains a count of the number of copies available and the number of copies checked out. The number of copies must always be greater than or equal to zero. If the number of copies for a book goes to zero, it must be deleted from the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT