Question
In this project with using C++ involves implementing an object class to represent a combination lock that has three tumblers (or dials), each of which
This class will be used in a program that starts by declaring a lock instance that is initialized with an unknown random combination and whose tumblers are all initialized to 0-0-0. Your program must allow the user to advance any one of the dials by one position to a higher number. The program exits when the user quits in exasperation, or the lock has been opened.
You are given the Lock.h. Do not change this code. You need to create Lock.cpp to implement the functions.
TheLockclass has data members to store the current settings of each of its tumblers and to store its actual combination.
The Lockclass constructor initializes the combination and the tumblers.
The combination is set randomly to 3 numbers between 0 and 9.
The tumblers must all start out on 0.
The privatenumCorrectmethod is used to answer the question: How many tumblers are set correctly right now? It returns the number of correct tumblers as its return value.
ThespinTumblermethod takes as a parameter a number indicating which tumbler to advance and advances that tumbler by one position higher.Remember that the dials wrap around to 0 after 9. This method advances any of the three tumblers based on the parameter.
TheisUnlockedmethod tests to see if the lock is unlocked and returns true/false.
ThedisplayLockmethod displays the current lock setting and the number correct to the screen (call the private numCorrect method don't re-implement). See the sample output for the format.
You are given LockMain.cpp. Do not change this code.
main declares an instance of classLock, displays its current tumbler setting, and thenin a loop:
Asks the user what he/she wants to do: advance a tumbler, or quit. It rejects invalid inputs.
It displays the current setting of the lock and how many correct tumblers there are.
This repeats until the user quits or the lock opens (when there are three correct tumblers).
Do not change this code
Lock.h
#pragma once //=================================================================== // Lock.h : Implements a combination lock class with a randomly // generated three-number combo. //===================================================================
#ifndef LOCK_H #define LOCK_H
const int NUM_TUMBLERS = 3; // The number of tumblers on the lock const int MAX_NUM = 9; // A tumbler can be set from 0 to MAX_NUM
class Lock { private: int tumbler[NUM_TUMBLERS]; // Current lock setting int combination[NUM_TUMBLERS]; // Lock's combination // When the values in tumbler match the values in combination // it is unlocked.
int numCorrect(); // How many tumbler digits are correct?
public: Lock(); // Constructor
void spinTumbler(int i); // Set tumbler i to next number // Don't forget to wrap bool isUnlocked(); // True if all three tumblers // match the combination void displayLock(); // Display current state (see sample output) };
#endif
LockMain.cpp
#include #include #include \"Lock.h\"
using namespace std;
int getMenuChoice();
int main() { // Do \"Press return to continue...\" on exit atexit([] {system(\"pause\"); });
Lock combo; // instance of the padlock class int choice; // which tumbler to spin, or quit
cout combo.displayLock();
for (choice = getMenuChoice(); choice != 4; choice = getMenuChoice()) {
combo.spinTumbler(choice);
if (combo.isUnlocked()) { cout combo.displayLock(); break; }
combo.displayLock(); }
cout
} // end main
//=========================================================================== // getMenuChoice() // // PRE: nothing // POST: returns user's choice - a valid choice, or it is rejected
int getMenuChoice() { int choice; // which menu choice they pick
do { cout cout cout cout cout cout
cin >> choice;
} while (choice 4);
return choice;
}
And i need to get the Lock.cpp.
Sample output.
Welcome to the combination lock program.
_______
/_______\\
// \\\\
|| ||
|| ||
+-----------+
| 0 | 0 | 0 |
+-----------+
| |
| |
=============
0 tumblers are set correctly.
YOUR CHOICES:
1) Spin tumbler one
2) Spin tumbler two
3) Spin tumbler three
4) Quit
Your choice: 1
_______
/_______\\
// \\\\
|| ||
|| ||
+-----------+
| 1 | 0 | 0 |
+-----------+
| |
| |
=============
1 tumblers are set correctly.
We know that the first one is correct because it is the only one we changed and the number right went from 0 to 1.
YOUR CHOICES:
1) Spin tumbler one
2) Spin tumbler two
3) Spin tumbler three
4) Quit
Your choice: 2
___________
/_______\\
// \\\\
|| ||
|| ||
+-----------+
| 1 | 1 | 0 |
+-----------+
| |
| |
=============
1 tumblers are set correctly.
YOUR CHOICES:
1) Spin tumbler one
2) Spin tumbler two
3) Spin tumbler three
4) Quit
Your choice: 2
___________
/_______\\
// \\\\
|| ||
|| ||
+-----------+
| 1 | 2 | 0 |
+-----------+
| |
| |
=============
1 tumblers are set correctly.
YOUR CHOICES:
1) Spin tumbler one
2) Spin tumbler two
3) Spin tumbler three
4) Quit
Your choice: 2
___________
/_______\\
// \\\\
|| ||
|| ||
+-----------+
| 1 | 3 | 0 |
+-----------+
| |
| |
=============
2 tumblers are set correctly.
We know that the second one is now correct because it is the only one we changed and the number right went from 1 to 2.
YOUR CHOICES:
1) Spin tumbler one
2) Spin tumbler two
3) Spin tumbler three
4) Quit
Your choice: 3
___________
/_______\\
// \\\\
|| ||
|| ||
+-----------+
| 1 | 3 | 1 |
+-----------+
| |
| |
=============
2 tumblers are set correctly.
YOUR CHOICES:
1) Spin tumbler one
2) Spin tumbler two
3) Spin tumbler three
4) Quit
Your choice: 3
___________
/_______\\
// \\\\
|| ||
|| ||
+-----------+
| 1 | 3 | 2 |
+-----------+
| |
| |
=============
2 tumblers are set correctly.
YOUR CHOICES:
1) Spin tumbler one
2) Spin tumbler two
3) Spin tumbler three
4) Quit
Your choice: 3
___________
/_______\\
// \\\\
|| ||
|| ||
+-----------+
| 1 | 3 | 3 |
+-----------+
| |
| |
=============
2 tumblers are set correctly.
YOUR CHOICES:
1) Spin tumbler one
2) Spin tumbler two
3) Spin tumbler three
4) Quit
Your choice: 3
___________
/_______\\
// \\\\
|| ||
|| ||
+-----------+
| 1 | 3 | 4 |
+-----------+
| |
| |
=============
2 tumblers are set correctly.
YOUR CHOICES:
1) Spin tumbler one
2) Spin tumbler two
3) Spin tumbler three
4) Quit
Your choice: 3
___________
/_______\\
// \\\\
|| ||
|| ||
+-----------+
| 1 | 3 | 5 |
+-----------+
| |
| |
=============
2 tumblers are set correctly.
YOUR CHOICES:
1) Spin tumbler one
2) Spin tumbler two
3) Spin tumbler three
4) Quit
Your choice: 3
YOU GOT IT!!
___________
/_______\\
// \\\\
|| ||
|| ||
+-----------+
| 1 | 3 | 6 |
+-----------+
| |
| |
=============
3 tumblers are set correctly.
GOODBYE
Press any key to continue . . .
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started