Miss Natalia

(14)

$20/per page/Negotiable

About Miss Natalia

Levels Tought:
Elementary,High School,College,University

Expertise:
Accounting,Business & Finance See all
Accounting,Business & Finance,Calculus,Computer Science,Environmental science,Health & Medical Hide all
Teaching Since: Apr 2017
Last Sign in: 360 Weeks Ago, 3 Days Ago
Questions Answered: 6064
Tutorials Posted: 6070

Education

  • Doctor of Education in Educational Leadership with a Specialization in Educational Technology
    Phoniex University
    Oct-1999 - Nov-2005

Experience

  • HR Executive
    a21, Inc.
    Nov-1998 - Dec-2005

Category > Computer Science Posted 03 May 2017 My Price 10.00

Declare a class ComboLock that works like the combination lock on a gym locker

Declare a class ComboLock that works like the combination lock on a gym locker. 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 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.

Use the following headers for methods of 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()

Write a class TestComboLock containing the main method to test the class ComboLock with user inputs.

Example run:

Enter Combination: 1 39 1

Right: 1

Left: 2

Right: 2

The lock is open

I need this to be in 2 seperate files to be able to pull it from the tester over to the regular class. I have this but it is not working the way I need it to.

public class ComboLock{

int secret1,secret2,secret3;

int dial = 0;

int number1,number2,number3;

int count = 0;

boolean turnRight = true;

public ComboLock( int secret1, int secret2, int secret3 ){

this.secret1 = secret1;

this.secret2 = secret2;

this.secret3 = secret3;

}

public void reset(){

this.dial = 0;

this.number1 = 0;

this.number2 = 0;

this.number3 = 0;

}

public void turnRight( int ticks ){

if (count == 0){

int tempdial = this.dial - ticks;

if (tempdial < 0){

this.dial = 40+tempdial;

}

this.dial = tempdial;

number1 = this.dial;

}

if (count == 2){

int tempdial = this.dial - ticks;

if (tempdial < 0){

this.dial = 40+tempdial;

}

this.dial = tempdial;

number3 = this.dial;

}

count++;

}

public void turnLeft( int ticks ){

if (count == 1){

int tempdial = this.dial + ticks;

if (tempdial > 40){

this.dial = tempdial-40;

}

this.dial = tempdial;

}

number2 = this.dial;

count++;

}

public boolean open()

{

if(number1==secret1&&number2==secret2&&number3==secret3){

return true;

}

else{

return false;

}

}

}

import java.util.Scanner;

public class ComboLockTest{

public static void main(String[] args){

int secret1 = 0;

int secret2 = 0;

int secret3 = 0;

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

Scanner sc = 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 = sc.nextInt();

if((ticks < 0) || (ticks > 39)){

System.out.println("Invalid entry. Program will close.");

return;

}

if(turningRight){

lock.turnRight(ticks);

}

else{

lock.turnLeft(ticks);

}

turningRight =! turningRight;

opened = lock.open();

}

System.out.println("Open");

}

}

Answers

(14)
Status NEW Posted 03 May 2017 04:05 AM My Price 10.00

Sol-----------uti-----------ons----------- fi-----------le ----------- He-----------llo----------- Si-----------r/M-----------ada-----------m -----------Tha-----------nk -----------you----------- fo-----------r y-----------our----------- in-----------ter-----------est----------- an-----------d b-----------uyi-----------ng -----------my -----------pos-----------ted----------- so-----------lut-----------ion-----------. P-----------lea-----------se -----------pin-----------g m-----------e o-----------n c-----------hat----------- I -----------am -----------onl-----------ine----------- or----------- in-----------box----------- me----------- a -----------mes-----------sag-----------e I-----------

Not Rated(0)