Project 1 – Wallet
Following the class diagram shown below, create the class Wallet. A Wallet represents a carrier of bills and coins. You use your wallet to pay for things, which reduces the balance held inside. When you visit an ATM, you can fill it back up with cash again. A sample driver for this class is shown below. You should probably make a more thorough driver to test your class better. I will have my own driver which will aim to exploit any possible edge cases.
Wallet Class Description
Wallet( );Wallet( int d, int c );int getDollars( );int getCents( );bool canPayFor( int dollarAmount, int centsAmount );void payFor( int dollarAmount, int changeAmount );void visitATMForCash( int dollarAmount );int dollars;int cents;
Wallet Sample Driver Code
int atm = 0, d = 0, c = 0, x = 0, y = 0;char cont = 'y';cout << "Hello, how much money do you have in your wallet?" << endl;cout << "Dollars: ";cin >> d;cout << "Cents: ";cin >> c;cout << endl;Wallet w(d, c);cout << "How many dollars would you like to withdraw from the ATM? ";cin >> atm;w.visitATMForCash(atm);cout << "Currently you have: " << endl;cout << "Dollars: " << w.getDollars() << endl << "Cents: " << w.getCents() << endl << endl;do { cout << "How much are you going to spend?" << endl; cout << "Dollars: "; cin >> x; cout << "Cents: "; cin >> y; if (w.canPayFor(x, y) == true) { w.payFor(x, y); cout << "
You now have: " << endl; cout << "Dollars:" << w.getDollars() << endl << "Cents:" << w.getCents() << endl; } else { cout << "
Sorry, you do not have enough money to spend. Please spend less." << endl; } cout << "Are you spending more money (y/n)? "; cin >> cont; cout << endl;} while (cont == 'y');cout << "You have this amount left in your wallet: " << endl;cout << "Dollars: " << w.getDollars() << " Cents: " << w.getCents() << endl << endl;
Sample Driver Output
Hello, how much money do you have in your wallet?Dollars: 10Cents: 0How many dollars would you like to withdraw from the ATM? 5Currently you have:Dollars: 15Cents: 0How much are you going to spend?Dollars: 6Cents: 56You now have:Dollars: 8Cents: 44Are you spending more money (y/n)? yHow much are you going to spend?Dollars: 10Cents: 0Sorry, you do not have enough money to spend. Please spend less.Are you spending more money (y/n)? nYou have this amount left in your wallet:Dollars: 8 Cents: 44
Codeboard Submission
Please refer to the codeboard submission instructions here: http://mjedmonds.com/CS50/c
Project 2 – Record Player
Following the class diagram shown below, create the class RecordPlayer
. An RecordPlayer
represents a stereo component that can play vinyl records. Please review the class and the sample driver code. There is a specific order to the way the class methods should be called. In other words, you can’t plop the Needle unless there actually is a record on the player and the recordplayer
is turned on. You also can’t return the Needle unless there is actually a record on the player and the recordplayer
is turned on. Your class should enforce these rules and write errors to cout as they occur. A sample driver for this class is shown below.
Rules of the record player
- You can affix a platter if the record player is on or off and the needle is not ploped
- Record player must be on and must have record affixed to the platter to plop the needle on the record
- Can only return the needle if the record player is on and there is a record on the player
- Performing the same action twice results in a “no-op” where nothing changes. For example, you can’t plop an already plopped needle, and you can’t return a needle that’s already returned. Another example: you can’t turn on/off a record player that is already on
Please note: codeboard must test differently for this project. There is no input. The provided driver will be called automatically. Your code will not compile when you start the project; only when you’ve implemented the RecordPlayer class will it compile successfully.
Record Player
RecordPlayer( );void turnOn( );void turnOff( );bool isPoweredOn( );void affixPlatter( string record );void plopNeedle( );void returnNeedle( );bool isOn;string record;bool needleIsOnTheRecord;
Record Player Sample Driver Code
//declare 6 RecordPlayersRecordPlayer good_r, bad_r1, bad_r2, bad_r3, bad_r4, bad_r5;//first test correctgood_r.turnOn();good_r.affixPlatter("Barrow Manila I");good_r.plopNeedle();good_r.returnNeedle();good_r.turnOff();//bad test, plops needle without turning it on or putting record onbad_r1.plopNeedle();//turns it on but no record on platterbad_r2.turnOn();bad_r2.plopNeedle();//not on nor has album on platterbad_r3.returnNeedle();//turned on but no albumbad_r4.turnOn();bad_r4.returnNeedle();//not turned onbad_r5.affixPlatter("Barrow Manila I");bad_r5.plopNeedle();
Record Player Sample Driver Code Output
Record player is turned onRecord Barrow Manila I is now on the platterNeedle is placed on the recordNeedle is returned from the recordRecord player is turned offCannot place needle on the recordRecord player is turned onCannot place needle on the recordCannot return needle from the recordRecord player is turned onCannot return needle from the recordRecord Barrow Manila I is now on the platterCannot place needle on the record
Codeboard Submission
Please refer to the codeboard submission instructions here: http://mjedmonds.com/CS50/codeboard.html
Reviews
There are no reviews yet.