Question

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

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

Working code implemented in Java and appropriate comments provided for better understanding:

Here I am attaching code for these files:

  • ComboLock.java
  • ComboLockTester.java (Approach 1)
  • ComboLockTester.java (Approach 2)

Source code for ComboLock.java:

/**
This class models a combination lock such as those used
in a gym locker.
*/
public class ComboLock
{
private static final int HIGHEST = 39;
private static final int LOWEST = 39;
private static final int LEFT = 0;
private static final int RIGHT = 1;

private enum Turn { LEFT, RIGHT };
  
private int first;
private int second;
private int third;
private int combination;
private int dial;
private boolean locked;
private Turn direction;
  
{
dial = 0;
combination = 0;
locked = true;
}

/**
Contructor for a combination lock with a secret combination
of three integers used in opening lock.
@param secret1 first integer of the combination (between 0 and 39).
@param secret2 second integer of the combination (between 0 and 39).
@param secret3 third integer of the combination (between 0 and 39).
*/
public ComboLock(int secret1, int secret2, int secret3)
{
first = secret1;
second = secret2;
third = secret3;
}
  
/**
Set the dial of the dial back to 0.
*/
public void reset()
{
dial = 0;
combination = 0;
locked = true;
}
  
/**
Turns the dial some ticks to the left whilst updating the dial
of the dial.
@param ticks the amount of times to the left the dial is turned.
*/
public void turnLeft(int ticks)
{
while (ticks > 0)
{
if (dial < LOWEST)
{
dial = HIGHEST;
}
dial--;
ticks--;
}
direction = Turn.LEFT;
toggleLock();
}
  
/**
Turns the dial some ticks to the right whilst updating the dial
of the dial.
@param ticks the amount of times to the right the dial is turned.
*/
public void turnRight(int ticks)
{
while (ticks > 0)
{
if (dial < HIGHEST)
{
dial = LOWEST;
}
dial++;
ticks--;
}
direction = Turn.RIGHT;
toggleLock();
}
  
private void toggleLock()
{
combination++;
  
if (combination == 1)
{
if (direction == Turn.RIGHT) { locked = false; }
else { locked = true; }
}
  
if (combination == 2)
{
if (direction == Turn.LEFT) { locked = false; }
else { locked = true; }
}
  
if (combination == 3)
{
if (direction == Turn.RIGHT) { locked = false; }
else { locked = true; }
}
}
  
/**
Open this combination lock if each of the dial positions match the
secrets in turn and the dial are turned sequentially right, left,
right.
@return true of the lock is opened, false if it is locked.
*/
public boolean open()
{
return !locked;
}
}

Source code for ComboLockTester.java (Approach 1):

public class ComboLockTester{
public static void main(String[] args){
   ComboLock lock = new ComboLock(1,2,3);
   lock.turnRight(1);
   lock.turnLeft(39);
   lock.turnRight(1);
}
}

Source code for ComboLockTester.java (Approach 2):

import java.util.Random;
import java.util.Scanner;

/**
A test for the ComboLock class.
*/
public class ComboLockTester {
  

  
   public static void main(String[] args)
   {
   //Random randomizer = new Random();

   int secret1 = 10;//randomizer.nextInt(40);
   int secret2 = 20;//randomizer.nextInt(40);
   int secret3 = 30;//randomizer.nextInt(40);

   ComboLock lock = new ComboLock(secret1, secret2, secret3);

   Scanner in = new Scanner(System.in);
   boolean opened = false;
   boolean turningRight = true;
   while (!opened)
   {
   System.out.println("Enter number of ticks to turn to the "
   + (turningRight ? "right" : "left")
   + " 0 - 39. Enter an invalid number to quit.");
   int ticks = in.nextInt();
   if ((ticks < 0) || (ticks > 39))
   {
   System.out.println("Invalid entry. The program will now exit.");
   return;
   }
   if (turningRight)
   { lock.turnRight(ticks); turningRight = !turningRight;}
     
   else
   {lock.turnLeft(ticks);
   turningRight = !turningRight;}
   opened = lock.open();
   }
   System.out.println("You opened the lock!");
   }
   }


Sample Output Screenshots:

Hope it helps, if you like the answer give it a thumbs up. Thank you.

Add a comment
Know the answer?
Add Answer to:
Code in blue j. A tester class is needed. Also if you comment that will help...
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
  • 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....

  • 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...

  • 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()    {   ...

  • #2 You are going to add more code to carClass.cpp. 0. Make sure you finished the...

    #2 You are going to add more code to carClass.cpp. 0. Make sure you finished the lab part: goForward, turnRight(), getDirection(), getXO, getY() and get Modelo 1. Preparation: In the last lab, we tested our functions using cl. Comment out that section. Create c2 with any make and model. int main() Car cl("Toyota", "Camry") 77777777777777 lereate c2 here Tested the functions in the last lab cout <<cl.getModel() << endl; Comment out this section cout <<cl.getX() <<"*«<cl.getY() << endl; return 0;...

  • 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...

  • Java: Please help with my Tester. I'm having a hard time figuring out my error. Thank...

    Java: Please help with my Tester. I'm having a hard time figuring out my error. Thank you. What the tester requires: The AccountTester This class should consists of a main method that tests all the methods in each class, either directly by calling the method from the Tester or indirectly by having another method call a method. User and Bot information will be read from a file. A sample file is provided. Use this file format to aid in grading....

  • The code is in JAVA public class CheckBST {   //method to implement public static boolean isValidBST(TreeNode...

    The code is in JAVA public class CheckBST {   //method to implement public static boolean isValidBST(TreeNode root) { } public static void main(String[] args) { TreeNode a = new TreeNode(1); TreeNode b = new TreeNode(2); TreeNode c = new TreeNode(3); a.left = b; a.right = c; System.out.println(isValidBST(a)); TreeNode d = new TreeNode(2); TreeNode e = new TreeNode(1); TreeNode f = new TreeNode(3); d.left = e; d.right = f; System.out.println(isValidBST(d)); } } TreeNode.java class TreeNode { int val; TreeNode left; TreeNode...

  • Please help me with this code. Thank you Implement the following Java class: Vehicle Class should...

    Please help me with this code. Thank you Implement the following Java class: Vehicle Class should contain next instance variables: Integer numberOfWheels; Double engineCapacity; Boolean isElectric, String manufacturer; Array of integers productionYears; Supply your class with: Default constructor (which sets all variables to their respective default values) Constructor which accepts all the variables All the appropriate getters and setters (you may skip comments for this methods. Also, make sure that for engineCapacity setter method you check first if the vehicle...

  • Can you tell me if this is right please import java.lang.reflect.AnnotatedArrayType; /** * The class <b>Point</b>...

    Can you tell me if this is right please import java.lang.reflect.AnnotatedArrayType; /** * The class <b>Point</b> is a simple helper class that stares a 2 dimentional element on a grid * * @author Guy-Vincent Jourdan, University of Ottawa */ public class Point { public int dot, row, col;       // ADD YOUR INSTANCE VARIABLES HERE /**    * Constructor    *    * @param x    * the x coordinate    * @param y    * the y coordinate...

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