CSCI-1320 Homework 06: Cracking the Pin

Instructor: Todd Dole

Purpose Statement

Practice for loops, counters, and basic conditionals.

Instructions

You need to break into a safe, and time is running out!

The safe has a 4 digit pin to unlock it, ranging from 1000 to 9999. You have five attempts to guess the pin correctly. Can you break the code in time?

For this assignment, you will create a program called PasswordLock (saved as PasswordLock.java) to simulate the safe. Your program will randomly generate a 4 digit pin using the following code.

int pin = (int)(Math.random() * 9000) + 1000;

You will then use a for loop. At each iteration, let the user know how many attempts remain, and prompt the user for the pin.

Each time, you will let the user know if their guess is too low or two high.

If the user guesses correctly, you can use the break command to immediately exit the loop.

Example :

		You have 5 attempts remaining.  Please enter your pin: 4589
		Your pin is lower than that.
		You have 4 attempts remaining.  Please enter your pin: 2345
		Your pin is higher than that.
		You have 3 attempts remaining.  Please enter your pin: 3333
		Congratulations!  The safe is unlocked!
        

Upload your solution (your PasswordLock.java source code file) to the assignment in Canvas.

After you've finished and turned your assignment in, experiment with other shapes! Can you make the sentence repeat a second time to make an X on the screen?