Question

In: Computer Science

The lab folder has two source files ComboLock and the testing application ComboLockTesting.  There is one utility...

The lab folder has two source files ComboLock and the testing application ComboLockTesting.  There is one utility class (compiled, without given code) MyGoodLock.

ComboLock class has three private fields first, second, third, all of int type.  (As a matter of fact, they are numbers from 1 to 40).  It has a private field state of int type, representing the lock being partially opened.  It has a public field open of boolean type.  The class has one three-parameter constructor.  It also has two mutator methods: turnRight and turnLeft.  

ComboLock

-first : int

-second : int

-third : int

-state : int{0}

+open : boolean{false}

----------------------------

+ComboLock(int,int,int)

+turnRight(int):

+turnLeft(int):

Compile this class as given

If anyone can help me with this lab please.I have been having some trouble. Thank You!

Solutions

Expert Solution

/**************************************ComboLock.java************************/

import java.util.Scanner;

/**
* The Class ComboLock.
*/
public class ComboLock {

   /** The Constant TOTAL_STATES. */
   private static final int TOTAL_STATES = 40;

   /** The state. */
   private int state;

   /** The first. */
   private int first;

   /** The second. */
   private int second;

   /** The third. */
   private int third;

   /**
   * Instantiates a new combo lock.
   *
   * @param first the first
   * @param second the second
   * @param third the third
   */
   public ComboLock(int first, int second, int third) {
       state = 0;
       this.first = first;
       this.second = second;
       this.third = third;
   }

   /**
   * Turn left.
   *
   * @param ticks the ticks
   */
   public void turnLeft(int ticks) {
       if (state - ticks >= 0) {
           state -= ticks;
       } else {
           state = TOTAL_STATES - ticks + state;
       }
   }

   /**
   * Turn right.
   *
   * @param ticks the ticks
   */
   public void turnRight(int ticks) {
       if (state + ticks < TOTAL_STATES) {
           state += ticks;
       } else {
           state = (state + ticks) % TOTAL_STATES;
       }
   }

   /**
   * Reset.
   */
   public void reset() {
       state = 0;
   }

   /**
   * Open.
   *
   * @return true, if successful
   */
   public boolean open() {
       boolean first = false;
       boolean second = false;
       boolean third = false;

       turnRight(getInput("Turn state right: "));

       if (state == this.first) {
           first = true;
       }

       turnLeft(getInput("Turn state left: "));

       if (state == this.second) {
           second = true;
       }

       turnRight(getInput("Turn state right again: "));

       if (state == this.third) {
           third = true;
       }

       return first && second && third;
   }

   /**
   * Gets the input.
   *
   * @param prompt the prompt
   * @return the input
   */
   public int getInput(String prompt) {
       int input;
       Scanner scan = new Scanner(System.in);
       do {
           System.out.print(prompt);
           input = scan.nextInt();

           if (input < 1 || input > 40) {
               System.out.print("Input value must be between 1 and 40. ");
           }
       } while (input < 1 || input > 40);
       return input;
   }
}

Please let me know if you have any doubt or modify the answer, Thanks :)


Related Solutions

Create a class Student (in the separate c# file but in the project’s source files folder)...
Create a class Student (in the separate c# file but in the project’s source files folder) with those attributes (i.e. instance variables): Student Attribute Data type (student) id String firstName String lastName String courses Array of Course objects Student class must have 2 constructors: one default (without parameters), another with 4 parameters (for setting the instance variables listed in the above table) In addition Student class must have setter and getter methods for the 4 instance variables, and getGPA method...
Each of the following files in the Chapter15 folder of your downloadable student files has syntax and/or logic errors.
Each of the following files in the Chapter15 folder of your downloadable student files has syntax and/or logic errors. In each case, determine the problem and fix the program. After you correct the errors, save each file using the same filename preceded with Fix. For example, DebugFifteen1.java will become FixDebugFifteen1.java. a. DebugFifteen1.java b. DebugFifteen2.java c. DebugFifteen3.java d. DebugFifteen4.java    
Create a web application for a daily planner in HTML. Please include testing in the files...
Create a web application for a daily planner in HTML. Please include testing in the files and show correct code formatting.
Assignment Requirements Write a python application that consists of two .py files. One file is a...
Assignment Requirements Write a python application that consists of two .py files. One file is a module that contains functions used by the main program. NOTE: Please name your module file: asgn4_module.py The first function that is defined in the module is named is_field_blank and it receives a string and checks to see whether or not it is blank. If so, it returns True, if not it return false. The second function that is defined in the module is named...
C++ ONLY -- LAB ASSIGNMENT DIFFICULT We have to turn in two files - List.h and...
C++ ONLY -- LAB ASSIGNMENT DIFFICULT We have to turn in two files - List.h and Lab5.cpp What should the code for those two files look like? INSTRUCTIONS AS FOLLOWS: What Should This Program Do? Linked List Class Design your own linked list class (List.h) to hold a series of strings. The linked list node should be implemented as a struct. The class should have member functions for appending, inserting, and deleting nodes. You should also have a display function...
C# Simple Text File Merging Application - The idea is to merge two text files located...
C# Simple Text File Merging Application - The idea is to merge two text files located in the same folder to create a new txt file (also in the same folder). I have the idea, and I can merge the two txt files together, however I would like to have an empty line between the two files. How would I implement this in the code below? if (File.Exists(fileNameWithPath1)) { try { string[] file1 = File.ReadAllLines(fileNameWithPath1); string[] file2 = File.ReadAllLines(fileNameWithPath2); //dump...
Write a mail merge application titled EmailMerge.java You will use two files for this program.  The first...
Write a mail merge application titled EmailMerge.java You will use two files for this program.  The first is a text file that contains a template letter. template.txt [ Dear <>, Because you are <> years old and <>, we have a free gift for you. You have absolutely nothing to buy; just pay the shipping and handling charge of $9.99. To claim your gift, call us immediately. Thank you, Office of Claims Department ] The tags <>, <>, and <> are...
PLEASE MAKE THIS TWO DIFFERENT FILES. ONE FOR CLASS (temperature.h) AND ONE FOR temperature.cpp Objective: This...
PLEASE MAKE THIS TWO DIFFERENT FILES. ONE FOR CLASS (temperature.h) AND ONE FOR temperature.cpp Objective: This assignment will provide further practice with implementing classes. Task:  For this homework, you will write a class called Temperature, in the files temperature.h and temperature.cpp, for creating and using objects that will store temperatures (like for weather forecasting programs). This class should be portable, so it should work with any up-to-date C++ compiler. Program Details and Requirements: 1) An object of type Temperature should represent...
Suppose you have a polarized light source in your lab that has an intensity of 1364...
Suppose you have a polarized light source in your lab that has an intensity of 1364 W/m2, where the electric field oscillates in the z direction. The light travels toward a workbench where you can send it through either one of two polarizing filters. (a) If the light passes through a filter with its transmission axis at an angle of 45.0° from the z direction, what is the intensity of the transmitted light? =? W/m2 (b) If, instead, the light...
This needs to be written in JavaScript with two files. One HTML file and a JavaScript...
This needs to be written in JavaScript with two files. One HTML file and a JavaScript file. Make an empty HTML file, put three buttons inside the body and an empty main tag below the buttons. The text of the buttons should be "Foo", "Bar", and "FooBar" respectively. Don't put the " in the buttons that's just for clarification in these instructions. Add unique IDs to each button In your JavaScript, get 3 separate references to the buttons using the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT