Question

In: Computer Science

In.java Write a program that repeatedly asks the user to enter their password until they enter...

In.java

Write a program that repeatedly asks the user to enter their password until they enter the correct one. However, after 5 failed attempts, the program "locks out" the user. We will show that with an error message.
Assume the password is HOC2141 (case sensitive).
Note that there is a special case that is not shown below. To identify it, think of all possible scenarios of input for this program.

----------- Sample run 1:
Enter your password: Blake
Wrong
Enter your password: hoc2341
Wrong
Enter your password: hoc2141
Wrong
Enter your password: HOC2141
Correct! You're in!
Bye 

----------- Sample run 2:
Enter your password: HOC2141
Correct! You're in!
Bye
 
----------- Sample run 3:
Enter your password: asdf
Wrong
Enter your password: ffgdf
Wrong
Enter your password: asdxcv
Wrong
Enter your password: 1276
Wrong
Enter your password: HOCK
Wrong
You are locked out!
Bye
 

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// Login.java

import java.util.Scanner;

public class Login {

      public static void main(String[] args) {

            // storing original password

            String password = "HOC2141";

            // scanner for input

            Scanner scanner = new Scanner(System.in);

            String userInput;

            int attempts = 0;

            boolean in = false;

            // looping until 5 attempts are exhausted or user enter it correctly

            while (attempts < 5) {

                  // asking and reading password

                  System.out.print("Enter your password: ");

                  userInput = scanner.nextLine();

                  // comparing password with original

                  if (userInput.equals(password)) {

                        // success

                        System.out.println("Correct! You're in!");

                        in = true;

                        break; // exiting loop

                  } else {

                        // wrong

                        System.out.println("Wrong");

                  }

                  // incrementing number of attempts

                  attempts++;

            }

            // if in is not true, and the loop is over, printing 'You are locked

            // out!'

            if (!in) {

                  System.out.println("You are locked out!");

            }

            System.out.println("Bye");

      }

}

/*OUTPUT 1*/

Enter your password: hdjsd

Wrong

Enter your password: HOC2141

Correct! You're in!

Bye

/*OUTPUT 2*/

Enter your password: hoc2141

Wrong

Enter your password: hOC2141

Wrong

Enter your password: HOC2141

Correct! You're in!

Bye

/*OUTPUT 3*/

Enter your password: hsjks

Wrong

Enter your password: jhfjk

Wrong

Enter your password: whuw

Wrong

Enter your password: sdh

Wrong

Enter your password: wduwhdj

Wrong

You are locked out!

Bye


Related Solutions

(IN PYTHON) Write a program that asks the user repeatedly to enter a student's score or...
(IN PYTHON) Write a program that asks the user repeatedly to enter a student's score or enter -1 to stop. When finished entering all the scores, the program should display the number of scores entered, the sum of the scores, the mean, the lowest and the highest score. (IN PYTHON)
Write a program that asks the user to enter a bunch of words until they type...
Write a program that asks the user to enter a bunch of words until they type the word “quit”. Count how many words had 5 letters or more. Sample program Enter a word: nothing Enter a word: stop Enter a word: word Enter a word: program Enter a word: supercalifragilisticexpialidocious Enter a word: quit You entered 3 words that were longer than 5 letters.
In.java Write down a program that asks the user for their full name given in the...
In.java Write down a program that asks the user for their full name given in the format first last. The program will do the necessary processing and print the name in a table with 3 columns: Last, First, Initials. Requirements/Specifications... Your program run must be identical with the one shown as an example (both in how it reads the input and it on what it prints). The table must have the top and bottom lines as shown. The columns for...
Create a C++ program which will prompt the user to enter a password continually until the...
Create a C++ program which will prompt the user to enter a password continually until the password passes the following tests. Password is 6 chars long Password has at least 1 number If the input does not match the tests, it will input a specific error message. "Pass must be 6 chars long." If the input is allowed, print a message saying "Correct Password Criteria."
In PYTHON: Write a program that asks for a password from a user and checks if...
In PYTHON: Write a program that asks for a password from a user and checks if it satisfies the conditions: Contains at least one lowercase letter (a-z). Contains at least one uppercase letter (A-Z). Contains at least one digit (0-9). Minimum length 6 characters. Contains no spaces. If the password is valid, then the program prints “password accepted”. Otherwise, it keeps asking for more passwords from the user until a valid password is obtained. Hint: You might like to check...
Using C++ Write a program to ask user to enter a password and validity the password....
Using C++ Write a program to ask user to enter a password and validity the password. The password should be 8 – 20 characters long (8 is included and 20 is included), it should contain at least one uppercase letter and one lower case letter. The password should contain at least one digit, and at least one symbol other than alphabets or numbers. Most importantly, the password may not contain any white space. Functions: You will need at least the...
Write a program that asks the user to enter the name of a file, and then...
Write a program that asks the user to enter the name of a file, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the file. Use Notepad or another text editor to create a sample file that can be used to test the program. Sample Run java FileLetterCounter Enter file name: wc4↵ Enter character to count: 0↵ The character '0' appears in the file...
C# Create an application that asks the user to enter their new password. The password must...
C# Create an application that asks the user to enter their new password. The password must be at least 6 characters long. Develop a custom exception handling case that checks for the password length. (hint: use " .Length " built-in method). If the new password is less than 6 characters long the custom exception should output an error message.
Program should be written in Java a) Write a program that asks the user to enter...
Program should be written in Java a) Write a program that asks the user to enter the approximate current population of India. You should have the computer output a prompt and then YOU (as the user should enter the population.)  For testing purposes you may use the value of 1,382,000,000 from August 2020. Assume that the growth rate is 1.1% per year. Predict and print the predicted population for 2021 and 2022. The printout should include the year and the estimated...
1. Write a program that keeps asking the user for a password until they correctly name...
1. Write a program that keeps asking the user for a password until they correctly name it. Once they correctly enter the password, the program congratulates the user and tells them how many guesses it took. Be sure to be grammatically correct with the guess/guesses output. Call the program LastNamePassword. Example (user input in italics) What is the password? monkeys Incorrect. Guess again. dishwasher Incorrect. Guess again. aardvark Correct! You got the password, and it took you 3 guesses to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT