Question

Declare a class ComboLock that works like the combination lock in a school or gym locker.....

Declare a class ComboLock that works like the combination lock in a school or gym locker..

The lock is constructed with a combination dial numbers between 0 and 39.

  • The constructor sets the three "secret" numbers and the current position to 0.
  • The reset method resets the dial so that it points to 0.
  • The turnLeft and turnRight methods turn the dial by a given number of ticks to the left or right.
  • The open method attempts to open the lock. The lock opens if the user first turned it right to the first number in the combination, then left to the second, and then right to the third. If the lock doesn't open, the lock is reset.
  • The toString method should show current position of the lock's dial
  • The getSolution method should return a String with the three "secret" numbers separated by spaces.
  • The equals method should compare the three "secret" numbers of two locks (one the calling object, the other a parameter ComboLock object).

Here are the headings of the methods that should be included in your ComboLock class for this lab:

  public class ComboLock {
      . . .
      public ComboLock(int secret1, int secret2, int secret3) { ... }
      public void reset() { ... }
      public void turnLeft(int ticks) { ... }
      public void turnRight(int ticks) { ... }
            public boolean open() { ... }
      public String toString() { ... }
      public String getSolution() { ... }
      public boolean equals(ComboLock another){ ... }
  }  

You will need to determine the properties required to track the state of a ComboLock and how close a combination is to opening the lock.

Please remember to document your constants and methods using Javadoc-style comments and tags. Spend some time making your documentation useful, thorough, and easy to understand.

In a separate class, create a main method to test your class. Initialize a new ComboLock with combination 15 - 28 - 4. Demonstrate your ComboLock class by showing the results of the following attempts to open the lock:

  • right 14, left 20 (34 ticks), right 4 (24 ticks)
  • left 15, right 28, left 4
  • right 15, left 28, right 8
  • right 15, left 28
  • right 15, left 28 (27 ticks), right 4 (16 ticks) - opens

Create another ComboLock object with a combination of your choice and demonstrate how it works, as well.

Finally compare the two locks using the equals method.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package mar10_20;
import java.util.Scanner;

/**
* A class to simulate a combination lock.
*/
public class ComboLock {
    int secret1, secret2, secret3;
    int dial = 0;
    int number1, number2, number3;
    int count = 0;

    public ComboLock() {
    }

    public ComboLock(int number1, int number2, int number3) {
        this.secret1 = number1;
        this.secret2 = number2;
        this.secret3 = number3;
    }

    public void setSecret1(int secret1) {
        this.secret1 = secret1;
    }

    public void setSecret2(int secret2) {
        this.secret2 = secret2;
    }

    public void setSecret3(int secret3) {
        this.secret3 = secret3;
    }

    public void reset() {
        this.dial = 0;
        this.number1 = 0;
        this.number2 = 0;
        this.number3 = 0;
        this.count = 0;
    }

    public void turnRight(int ticks) {
        this.dial = ticks;
        
        if (count == 0) {
            number1 = this.dial;
        }
        if (count == 2) {
            number3 = this.dial;
        }
        count++;
        
        if (count == 3) {
            count = 0;
        }
    }

    public void turnLeft(int ticks) {
        this.dial = ticks;
        if (count == 1) {
            number2 = this.dial;
            count++;
        }
    }

    public boolean open() {
        if (number1 == secret1 && number2 == secret2 && number3 == secret3) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Returns current value dial is pointing at
     *
     * @return value dial is pointing at currently
     */
    public int getCurrentNumber() {
        return dial;
    }
    
    public String toString() {
        return dial + "";
    }
    public String getSolution () {
        return secret1 + "-" + secret2 + "-" + secret3;
    }
    public boolean equals (ComboLock c) {
        return (secret1 == c.secret1) && (secret2 == c.secret2) && (secret3 == c.secret3);
    }

}


public class ComboLockDemo {

    public static void main(String[] args) {

        ComboLock clx = new ComboLock(15, 28, 4);
        clx.turnRight(14);
        clx.turnLeft(20);
        clx.turnRight(4);
        System.out.println(clx.open());
        
        clx.reset();
        clx.turnLeft(15);
        clx.turnRight(28);
        clx.turnLeft(4);
        System.out.println(clx.open());
        
        clx.reset();
        clx.turnRight(15);
        clx.turnLeft(28);
        clx.turnRight(8);
        System.out.println(clx.open());
        
        clx.reset();
        clx.turnRight(15);
        clx.turnLeft(28);
        System.out.println(clx.open());
        
        clx.reset();
        clx.turnRight(15);
        clx.turnLeft(28);
        clx.turnRight(4);
        System.out.println(clx.open());
        
    }

}

**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.

Add a comment
Know the answer?
Add Answer to:
Declare a class ComboLock that works like the combination lock in a school or gym locker.....
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
  • Code in blue j. A tester class is needed. Also if you comment that will help...

    Code in blue j. A tester class is needed. Also if you comment that will help greatly. If you can help me I will give you a thumbs up. Declare a class ComboLock that works like the combination lock in a gym locker, as shown below. The lock is constructed with a combination—three numbers between 0 and 39. The reset method resets the dial so that it points to 0. The turnLeft and turnRight methods turn the dial by a...

  • Java Help, Will definitely give a thumbs up if it works. Declare a class ComboLock that...

    Java Help, Will definitely give a thumbs up if it works. Declare a class ComboLock that works like the combination lock in a gym locker (Consult the API provided to look for the methods needed). The locker is constructed with a combination - three numbers between 0 and 39. The reset method resets the dial so that it points to 0. The turnLeft and turnRight methods turn the dial by a given number of ticks to the left or right....

  • Why doesn't this code let me run it. I just finished it really... public class ComboLock...

    Why doesn't this code let me run it. I just finished it really... public class ComboLock { int secret1, secret2, secret3; // secret keys int dial1, dial2, dial3; // variables used to store dialed ticks boolean first, second, third; // variables used to store current turn number(how many numbers entered already) public ComboLock(int secret1, int secret2, int secret3) { super(); this.secret1 = secret1; this.secret2 = secret2; this.secret3 = secret3; this.dial1 = 0; this.dial2 = 0; this.dial3 = 0; this.first =...

  • java problem here is the combination class class Combination {    int first,second,third, fourth;    public...

    java problem here is the combination class class Combination {    int first,second,third, fourth;    public Combination(int first, int second, int third,int fourth)    {        this.first=first;        this.second=second;        this.third=third;        this.fourth=fourth;    }    public boolean equals(Combination other)    {               if ((this.first==other.first) && (this.second==other.second) && (this.third==other.third) && (this.fourth==other.fourth))            return true;        else            return false;    }    public String toString()    {   ...

  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

  • Rewrite the Course class in Listing 10.6 to implement the comparable and the cloneable interfaces. The...

    Rewrite the Course class in Listing 10.6 to implement the comparable and the cloneable interfaces. The clone method must allow a deep copy on the students field. In addition, add the equals(Object o) and the toString() methods. Write a test program to invoke the compareTo, clone, equals, and toString methods in a meaningful way. Below is the Listing from the book that needs to be rewritten public class Course {    private String courseName;    private String[] students = new...

  • In this problem, you will complete several classes involving boxes, locks, keys and secrets. Follow the...

    In this problem, you will complete several classes involving boxes, locks, keys and secrets. Follow the specifications in the starter files for the classes. You can run the javadoc program to generate the API (see Tutorial 2) for the classes. A box has a secret inside it (and maybe a key!) and can be locked. In order to open a box, you need the right key to unlock the lock. Secrets.java will be a program that takes an array of...

  • I need help with these Java programming assignements. public class Die { //here you declare your...

    I need help with these Java programming assignements. public class Die { //here you declare your attributes private int faceValue; //operations //constructor - public Die() { //body of constructor faceValue=(int)(Math.random()*6)+1;//instead of 1, use random approach to generate it } //roll operation public void roll() { faceValue=(int)(Math.random()*6)+1; }    //add a getter method public int getFaceValue() { return faceValue; }    //add a setter method public void setFaceValue(int value) { faceValue=value; } //add a toString() method public String toString() { String...

  • LockADT

    1.   LockADT – Show the interface and all abstract methodsLockDataStructureClass – Show the following methods: default constructor, overloaded constructor, copy constructor, setX, setY, setZ, alter (change the lock’s combination to the numbers passed) turn (use for loops to show the dial turning), close (locks the lock), attempt (tries to unlock the lock – calls turn( ), inquire (locked or unlocked), current (returns the number the dial is pointing to), toStringLockClientDemoClass – You should have a Lock object instantiated and a...

  • Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance...

    Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance and interface behaviors . The link to the PDF of the diagram is below MotorVehical.pdf Minimize File Preview User Define Object Assignment: Create a Intellij Project. The Intellij project will contain three user defined classes. The project will test two of the User Define Classes by using the invoking each of their methods and printing the results. You are required to create three UML...

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