The following code or the program will be used:
Step-by-step explanation:
import java.util.Scanner;
public class Authenticate
{
public static void main(String[] args)
{
// Actual password is 99508
int[] actual_password = {9, 9, 5, 0, 8};
// Array to hold randomly generated digits
int[] random_nums = new int[10];
// Array to hold the digits entered by the user to authenticate
int[] entered_digits = new int[actual_password.length];
// Randomly generate numbers from 1-3 for
// for each digit
for (int i=0; i < 10; i++)
{
random_nums[i] = (int) (Math.random() * 3) + 1;
}
// Output the challenge
System.out.println("Welcome! To log in, enter the random digits from 1-3 that");
System.out.println("correspond to your PIN number.");
System.out.println();
System.out.println("PIN digit: 0 1 2 3 4 5 6 7 8 9");
System.out.print("Random #: ");
for (int i=0; i<10; i++)
{
System.out.print(random_nums[i] + " ");
}
System.out.println();
System.out.println();
// Input the user's entry
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter code.");
String s = keyboard.next();
String s = keyboard.next();
// Extract the digits from the code and store in the entered_digits array
for (int i=0; i<s.length(); i++)
{
entered_digits[i] = s.charAt(i) - '0'; // Convert char to corresponding digit
}
// At this point, if the user typed 12443 then
// entered_digits[0] = 1, entered_digits[1] = 2, entered_digits[2] = 4,
// entered_digits[3] = 4, and entered_digits[4] = 3
/****
TO DO: fill in the parenthesis for the if statement
so the isValid method is invoked, sending in the arrays
actual_password, entered_digits, and random_nums as
parameters
***/
if (isValid (actual_password, entered_digits, random_nums)) // FILL IN HERE
{
System.out.println("Correct! You may now proceed.");
}
else
{
System.out.println("Error, invalid password entered.");
}
/***
TO DO: Fill in the body of this method so it returns true
if a valid password response is entered, and false otherwise.
For example, if:
actual = {9,9,5,0,8}
randnums = {1,2,3,1,2,3,1,2,3,1}
then this should return true if:
entered[0] == 1 (actual[0] = 9 -> randnums[9] -> 1)
entered[1] == 1 (actual[1] = 9 -> randnums[9] -> 1)
entered[2] == 3 (actual[2] = 5 -> randnums[5] -> 3)
entered[3] == 1 (actual[3] = 0 -> randnums[0] -> 1)
entered[4] == 3 (actual[4] = 8 -> randnums[8] -> 3)
or in other words, the method should return false if any of
the above are not equal.
****/
public static boolean isValid(int[] actual, int[] entered, int[] randnums)
{
int Index = 0;
boolean Valid = true;
while (Valid && (Index < actual.length))
{
int Code = actual[Index];
if (entered [Index] != randnums [Code])
{
Valid = false;
}
Index++;
}
return Valid;
}