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

USING C++ LANGUAGE!!!! PRINCOPLES OF DATA STRUCTURES PLEASE PROVIDE lock.h & loc

ID: 3869707 • Letter: U

Question

USING C++ LANGUAGE!!!!

PRINCOPLES OF DATA STRUCTURES

PLEASE PROVIDE lock.h & lock.cpp and lockdemo.cpp to see if the code works

Specify, design, and implement a class that

can be used in a program that simulates a

combination lock. The lock has a circular

knob, with the numbers 0 through 39 marked on the

edge, and it has a three-number combination, which

we’ll call x, y, z. To open the lock, you must turn the

knob clockwise at least one entire revolution, stopping

with x at the top; then turn the knob counterclockwise,

stopping the second time that y appears at

the top; finally turn the knob clockwise again, stopping

the next time that z appears at the top. At this

point, you may open the lock.

Your lock class should have a constructor that

for default arguments). Also provide member functions:

to alter the lock’s combination to a new threenumber

combination

to turn the knob in a given direction until a

specified number appears at the top

to close the lock

(e) to inquire about the status of the lock (open or closed

(f) to tell you what number is currently at the top

Explanation / Answer

#include<iostream>

using namespace std;

class lock{

    private:
        int x;
        int y;
        int z;
        string status;
        int top;
    public:
        lock(int a1, int b1, int c1){
           if (a1>=0 && a1 <=39)
              x = a1;
           else
              x = 0;
           if (b1>=0 && b1 <=39)
              y = b1;
           else
              y = 0;
           if (c1>=0 && c1 <=39)
              z = c1;
           else
              z = 0;
           status = "open";
           top = 1;
        }
        string getStatus(){
           return status;
        }
        string setStatus(string a){
           status = a;
        }
        string setCombination(int a, int b, int c){
           x = a;
           y = b;
           z = c;
        }
        int getX(){
           return x;
        }
        int getY(){
           return y;
        }
        int getZ(){
           return z;
        }
        void turn(int dir,int val){//dir = 1 clockwise, 0 anti clock wise)
            if (dir == 1){
               while (top != val){
                     top = top -1;
                     if (top == 0)
                        top = 39;
               }
            }
            if (dir == 0){
               while (top != val){
                     top = top + 1;
                     if (top == 39)
                        top = 0;
               }
            }
        }
        bool open(int a, int b, int c){
           
            int curr = top;
            bool res = false;
            if (status == "lock"){
               turn(1,curr);
              
               turn(1,a);
              
               if (top == x){
                  turn(0,b);
                  if (top == y){
                     turn (1,c);
                     if (top ==z){
                        res= true;
                     }
                  }
               }
              
             }
             else {
                 cout << "lock is already open" << endl;
                 res = true;
             }
            return res;   
        }
        bool locked(){
            bool res = true;
            if (status == "open")
                status = "lock";
            else
                cout << "lock is already locked" << endl;
            return res;
        }
       
       

};

int main(){

   lock l1(34,23,10);
   cout << l1.locked() << endl;
   cout << l1.open(12,25,30) << endl;
   cout << l1.open(34,23,10) << endl;
   return 0;


}