Question

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. The open method attempts to open the lock. The lock opens if the user first turned it right to the first number in combination, then left to the second, and then right to the third.

The tester file is provided so that you can check your output on the console. However, you need to write another Test class with at least five Junit test cases.

  • for this problem you can treat the parameter tick as the final value. That is, turnRight(20) just turns to 20. If you implemented the "harder" way, i.e., treat 20 as the amount of movement, that is also acceptable. Make sure your implementation of open(), and test cases are implemented accordingly.
  • to test comboLock, you can add some getters and setters so that you can test the state of the lock object.

comboLock file:

package combinationlock;


   /**
   A class to simulate a combination lock.
   */
   public class ComboLock
   {
   private int secret1;
   private int secret2;
   private int secret3;

   // 0: nothing entered, 1: secret1 done, 2: secret2 done,
   // 3: secret3 done and unlocked
   private int lockState;
   private int currentNumber;
   private boolean validSoFar;
  

   /**
   Initializes the combination of the lock.
   @param secret1 first number to turn right to
   @param secret2 second number to turn left to
   @param secret3 third number to turn right to
   */
   public ComboLock(int secret1, int secret2, int secret3)
   {
  
   }

   /**
   Resets the state of the lock so that it can be opened again.
   */
   public void reset()
   {
         
   }

   /**
   Turns lock left given number of ticks.
   @param ticks number of ticks to turn left
   */
   public void turnLeft(int ticks)
   {

   }

   /**
   Turns lock right given number of ticks
   @param ticks number of ticks to turn right
   */
   public void turnRight(int ticks)
   {
  
   }

   /**
   Returns true if the lock can be opened now
   @return true if lock is in open state
   */
   public boolean open()
   {

   }

junit tester file:

package combinationlock;
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!");
   }
   }

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

package com.HomeworkLib;

import java.util.Scanner;

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!");
   }
  
}

/**
A class to simulate a combination lock.
*/
class ComboLock
{
private int secret1;
private int secret2;
private int secret3;

// 0: nothing entered, 1: secret1 done, 2: secret2 done,
// 3: secret3 done and unlocked
private int lockState;
private int currentNumber;
private boolean validSoFar;


/**
Initializes the combination of the lock.
@param secret1 first number to turn right to
@param secret2 second number to turn left to
@param secret3 third number to turn right to
*/
public ComboLock(int secret1, int secret2, int secret3) {
   super();
   this.secret1 = secret1;
   this.secret2 = secret2;
   this.secret3 = secret3;
   currentNumber = 0;
   lockState = 0;
   validSoFar = false;
}

/**
Resets the state of the lock so that it can be opened again.
*/
public void reset()
{
   currentNumber = 0;
   lockState = 0;
   validSoFar = false;
}

/**
Turns lock left given number of ticks.
@param ticks number of ticks to turn left
*/
public void turnLeft(int ticks)
{
   currentNumber = ticks;
   if(secret2 == currentNumber && lockState == 1 && validSoFar){
       validSoFar = true;
       lockState = 2;
   }else{
       validSoFar = false;
   }
}

/**
Turns lock right given number of ticks
@param ticks number of ticks to turn right
*/
public void turnRight(int ticks)
{
  
   currentNumber = ticks;
   if(secret1 == currentNumber && lockState == 0){
       validSoFar = true;
       lockState = 1;
   }else if(secret3 == currentNumber && lockState == 2 && validSoFar){
       validSoFar = true;
       lockState = 3;
   }else{
       validSoFar = false;
   }
}

/**
Returns true if the lock can be opened now
@return true if lock is in open state
*/
public boolean open()
{
   return lockState == 3;

}
}

Add a comment
Know the answer?
Add Answer to:
Java Help, Will definitely give a thumbs up if it works. Declare a class ComboLock that...
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...

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

  • Write Junit Test for the following class below: public class Turn {    private int turnScore;    private Dice dice;    private boolean isSkunk;    private boolean isDoubleSkunk;    public Turn()    {...

    Write Junit Test for the following class below: public class Turn {    private int turnScore;    private Dice dice;    private boolean isSkunk;    private boolean isDoubleSkunk;    public Turn()    {        dice = new Dice();    }    public Turn(Dice dice) {        this.dice = dice;    }       public void turnRoll()    {        dice.roll();        if (dice.getDie1Value() == 1 || dice.getDie2Value() == 1 && dice.getLastRoll() != 2)        {            turnScore = 0;            isSkunk = true;        }        else if (dice.getLastRoll() == 2)        {            turnScore = 0;            isDoubleSkunk = true; }        else        {           ...

  • Solve this using Java for an Intro To Java Class. Provided files: Person.java /** * Models...

    Solve this using Java for an Intro To Java Class. Provided files: Person.java /** * Models a person */ public class Person { private String name; private String gender; private int age; /** * Consructs a Person object * @param name the name of the person * @param gender the gender of the person either * m for male or f for female * @param age the age of the person */ public Person(String name, String gender, int age) {...

  • Language is Java, any help is appreciated. Thank you! WHERE TO START FROM: import java.util.ArrayList; //PartTest.java...

    Language is Java, any help is appreciated. Thank you! WHERE TO START FROM: import java.util.ArrayList; //PartTest.java //package simple; public class PartTest { public static void main(String[] args) { ArrayList<ExpendablePart> system=new ArrayList<ExpendablePart>();   // creating Part Object Part part1 = new ExpendablePart("Last, First"); part1.setNumber("AX-34R"); part1.setNcage("MN34R"); part1.setNiin("ABCD-RF-WDE-KLJM"); // printing information System.out.println(part1.toString());       //Create a part2 object of class Part Part part2=new ConsumablePart("Widget, purple"); part2.setNumber("12345"); part2.setNcage("OU812"); part2.setNiin("1234-12-123-1234");    // printing information System.out.println(part2.toString());    //checking equality of two Part class objects if(part1.equals(part2)) System.out.println("part1 and...

  • Preliminaries Download the template class and the driver file. Objective Learn how to traverse a binary...

    Preliminaries Download the template class and the driver file. Objective Learn how to traverse a binary search tree in order. Description For the template class BinarySearchTree, fill in the following methods: insert - Inserts values greater than the parent to the right, and values lesser than the parent to the left.parameters elem - The new element to be inserted into the tree. printInOrder - Prints the values stored in the tree in ascending order. Hint: Use a recursive method to...

  • JAVA Only Help on the sections that say Student provide code. The student Provide code has...

    JAVA Only Help on the sections that say Student provide code. The student Provide code has comments that i put to state what i need help with. import java.util.Scanner; public class TicTacToe {     private final int BOARDSIZE = 3; // size of the board     private enum Status { WIN, DRAW, CONTINUE }; // game states     private char[][] board; // board representation     private boolean firstPlayer; // whether it's player 1's move     private boolean gameOver; // whether...

  • How to build Java test class? I am supposed to create both a recipe class, and...

    How to build Java test class? I am supposed to create both a recipe class, and then a class tester to test the recipe class. Below is what I have for the recipe class, but I have no idea what/or how I am supposed to go about creating the test class. Am I supposed to somehow call the recipe class within the test class? if so, how? Thanks in advance! This is my recipe class: package steppingstone5_recipe; /** * *...

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