In JAVA please In this assignment you are to implement a challenge-response syst
ID: 3878483 • Letter: I
Question
In JAVA please
In this assignment you are to implement a challenge-response system to protect a user's numeric PIN which they will enter via a phone. First you will be supplied a 10-digit numeric string (the challenge) Then, a PIN will be supplied as though typed into a phone ABC DEF GHI KL MNO PORS TUV WXYz So, "AGOT" would signify PIN 2468. To compute a response, substitute each PIN digit with the corresponding challenge digit. For example, assume this challenge (indexes shown for convenience) SEQUENCE 3 INDEX 3 3 3 3 8 If supplied "AGOT via phone (PIN: 2468), you would respond 3121 (the value at index 2, followed by index 4,...). Note that the PIN can be any length (1 or more characters). Also note that if the challenge has repeated digits, the PIN itself is protected because many input PINs could have the same response (and future communications can use a different challenge) You will need to validate the challenge (1o all-digit characters) and the PIN (all digits) and then output the response. For exampleExplanation / Answer
Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
import java.util.Scanner;
public class Challenge {
private static int letterToPhone(char ch)
{
String[] keys = {"", "ABC", "DEF", "GHI", "JKL", "MNO", "PQRS", "TUV", "WXYZ"};
for(int i = 0; i < keys.length; i++)
{
if(keys[i].indexOf(ch) != -1)
return i+1;
}
return -1;
}
private static String getResponse(String seq, String pinStr)
{
int[] pin = digitStringToIntArray(pinStr);
String resp = "";
for(int i = 0; i < pin.length; i++)
resp += seq.charAt(pin[i]);
return resp;
}
private static boolean stringIsKDigits(String s, int k)
{
return (s.length() == k);
}
private static boolean allDigits(String s)
{
for(int i = 0; i < s.length() ; i++)
{
char ch = s.charAt(i);
if(ch < '0' || ch > '9')
return false;
}
return true;
}
private static boolean allUppercaseLetters(String s)
{
for(int i = 0; i < s.length() ; i++)
{
char ch = s.charAt(i);
if(ch < 'A' || ch > 'Z')
return false;
}
return true;
}
private static int[] digitStringToIntArray(String s)
{
int[] arr = new int[s.length()];
for(int i = 0; i < s.length(); i++)
{
arr[i] = letterToPhone(s.charAt(i));
}
return arr;
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String seq, pin, response;
System.out.print("Enter value sequence: ");
seq = keyboard.next();
System.out.print("Enter PIN: ");
pin = keyboard.next();
if(stringIsKDigits(seq, 10) && allDigits(seq))
{
if(allUppercaseLetters(pin))
{
response = getResponse(seq, pin);
System.out.println("Response: " + response);
}
else
System.out.println("Invalid PIN");
}
else
System.out.println("Invalid sequence");
}
}
output
======
Enter value sequence: 12345
Enter PIN: HELLO
Invalid sequence
Enter value sequence: 0.12345678
Enter PIN: HELLO
Invalid sequence
Enter value sequence: 0123456789
Enter PIN: Hello
Invalid PIN
Enter value sequence: 0123456789
Enter PIN: HELLO
Response: 43556
Enter value sequence: 3231132213
Enter PIN: AGOT
Response: 3121
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.