Question

Given java code is below, please use it!

Problem a (LA2a.java) In this assignment you are to implement a challenge-response system to protect a users numeric PIN whi

Enter value sequence: 12345 Enter PIN: HELLO Invalid sequence Enter value sequence: 0.12345678 Enter PIN: HELLO Invalid seque

import java.util.Scanner;

public class LA2a {
  
   /**
   * Number of digits in a valid value sequence
   */
   public static final int SEQ_DIGITS = 10;
  
   /**
   * Error for an invalid sequence
   * (not correct number of characters
   * or not made only of digits)
   */
   public static final String ERR_SEQ = "Invalid sequence";
  
   /**
   * Error for an invalid pin
   * (not made entirely of
   * uppercase letters)
   */
   public static final String ERR_PIN = "Invalid PIN";
  
   /**
   * Converts an uppercase letter
   * to the corresponding number on
   * a standard phone...
   *
   * 1:
   * 2: ABC
   * 3: DEF
   *
   * 4: GHI
   * 5: JKL
   * 6: MNO
   *
   * 7: PQRS
   * 8: TUV
   * 9: WXYZ
   *
   * *:
   * 0: +
   * #:
   *
   * @param c assumed to be uppercase letter
   * @return corresponding phone number
   */
   public static int letterToPhone(char c) {
       return 0; // replace with your code
   }
  
   /**
   * Takes an input PIN and produces a response
   * via first converting each character via phone
   * number, then using that as an index
   * to a value sequence
   *
   * @param pin input PIN (assumed to be uppercase letters)
   * @param values input value sequence
   * @return response
   */
   public static int[] getResponse(String pin, int[] values) {
       int[] response = new int[pin.length()];
      
       // write your code here
      
       return response;
   }
  
   /**
   * Returns true if the length of the
   * input string is equal to the input
   * parameter
   *
   * @param s input string
   * @param k length to check
   * @return true if length of string
   */
   public static boolean stringIsKDigits(String s, int k) {
       return false; // replace with your code
   }
  
   /**
   * Returns true if all the characters
   * in the input string are '0' through '9'
   *
   * @param s input string
   * @return true if all characters in s are digits
   */
   public static boolean allDigits(String s) {
       return false; // replace with your code
   }
  
   /**
   * Returns true if all the characters
   * in the input string are 'A' through 'Z'
   *
   * @param s input string
   * @return true if all characters are uppercase letters
   */
   public static boolean allUppercaseLetters(String s) {
       return false; // replace with your code
   }
  
   /**
   * Converts a string of digits to an
   * array of integers (e.g. "12" -> {1, 2})
   *
   * Assumes string is comprised of only digits!
   *
   * @param s digit string
   * @return corresponding integer array
   */
   public static int[] digitStringToIntArray(String s) {
       return null; // replace with your code
   }
  
   /**
   * Returns how many times a value
   * occurs within an array
   *
   * @param value needle
   * @param values haystack
   * @return how many times the needle occurs in the haystack
   */
   public static int countValues(int value, int[] values) {
       return 0; // replace with your code
   }
  
   /**
   * Returns how many ways the response could have been
   * produced from a given value sequence
   *
   * @param response output
   * @param values value sequence
   * @return how many PINs could have produced the same response given the value sequence
   */
   public static int numPossible(int[] response, int[] values) {
       return 0; // replace with your code
   }
  
   /**
   * Inputs a value sequence and PIN,
   * outputs challenge response
   *
   * @param args command-line arguments, ignored
   */
   public static void main(String[] args) {
       @SuppressWarnings("resource")
       final Scanner in = new Scanner(System.in);
      
       System.out.printf("Enter value sequence: ");
       final String seq = in.nextLine();
      
       System.out.printf("Enter PIN: ");
       final String pin = in.nextLine();
      
       // write your code here
   }

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Explanation::

  • Code in JAVA is given below
  • Please read comments for better understanding of the code
  • Screenshots of the OUTPUT are given at the end of the code


Code in JAVA::

import java.util.Scanner;

public class LA2a {

   /**
   * Number of digits in a valid value sequence
   */
   public static final int SEQ_DIGITS = 10;

   /**
   * Error for an invalid sequence
   * (not correct number of characters
   * or not made only of digits)
   */
   public static final String ERR_SEQ = "Invalid sequence";

   /**
   * Error for an invalid pin
   * (not made entirely of
   * uppercase letters)
   */
   public static final String ERR_PIN = "Invalid PIN";

   /**
   * Converts an uppercase letter
   * to the corresponding number on
   * a standard phone...
   *
   * 1:
   * 2: ABC
   * 3: DEF
   *
   * 4: GHI
   * 5: JKL
   * 6: MNO
   *
   * 7: PQRS
   * 8: TUV
   * 9: WXYZ
   *
   * *:
   * 0: +
   * #:
   *
   * @param c assumed to be uppercase letter
   * @return corresponding phone number
   */
   public static int letterToPhone(char c) {
       if(c=='A' || c=='B' || c=='C') {
           return 2;
       }else if(c=='D' || c=='E' || c=='F') {
           return 3;
       }else if(c=='G' || c=='H' || c=='I') {
           return 4;
       }else if(c=='J' || c=='K' || c=='L') {
           return 5;
       }else if(c=='M' || c=='N' || c=='O') {
           return 6;
       }else if(c=='P' || c=='Q' || c=='R' || c=='S') {
           return 7;
       }else if(c=='T' || c=='U' || c=='V') {
           return 8;
       }else if(c=='W' || c=='X' || c=='Y' || c=='Z') {
           return 9;
       }else if(c=='+') {
           return 0;
       }
       return 0;
   }

   /**
   * Takes an input PIN and produces a response
   * via first converting each character via phone
   * number, then using that as an index
   * to a value sequence
   *
   * @param pin input PIN (assumed to be uppercase letters)
   * @param values input value sequence
   * @return response
   */
   public static int[] getResponse(String pin, int[] values) {
       int[] response = new int[pin.length()];
       /**
       * Running through the String pin and
       * calling letterToPhone() method for each char of String pin
       * And whatever the integer value is returned by letterToPhone(),
       * It is used as index for values[] array and whatever the value is
       * present there, It is stored in response
       *
       * */
       for(int i=0;i<pin.length();i++) {
           response[i]=values[letterToPhone(pin.charAt(i))];
       }
       return response;
   }

   /**
   * Returns true if the length of the
   * input string is equal to the input
   * parameter
   *
   * @param s input string
   * @param k length to check
   * @return true if length of string
   */
   public static boolean stringIsKDigits(String s, int k) {
       return s.length()==k;
   }

   /**
   * Returns true if all the characters
   * in the input string are '0' through '9'
   *
   * @param s input string
   * @return true if all characters in s are digits
   */
   public static boolean allDigits(String s) {
       /**
       * for loop traverse through each character of string s
       * and check if each char is digit or not.
       * if any char is not digit then false is returned
       * */
       for(int i=0;i<s.length();i++) {
           if(!Character.isDigit(s.charAt(i))) {
               return false;
           }
       }
       return true;
   }

   /**
   * Returns true if all the characters
   * in the input string are 'A' through 'Z'
   *
   * @param s input string
   * @return true if all characters are uppercase letters
   */
   public static boolean allUppercaseLetters(String s) {
       /**
       * for loop traverse through each character of string s
       * and check if each char is upper case or not.
       * if any char is not upper case then false is returned
       * */
       for(int i=0;i<s.length();i++) {
           if(!Character.isUpperCase(s.charAt(i))) {
               return false;
           }
       }
       return true;
   }

   /**
   * Converts a string of digits to an
   * array of integers (e.g. "12" -> {1, 2})
   *
   * Assumes string is comprised of only digits!
   *
   * @param s digit string
   * @return corresponding integer array
   */
   public static int[] digitStringToIntArray(String s) {
       int digitArray[]=new int[s.length()];
       for(int i=0;i<s.length();i++) {
           digitArray[i]=Character.getNumericValue(s.charAt(i));
       }
       return digitArray; // replace with your code
   }

   /**
   * Returns how many times a value
   * occurs within an array
   *
   * @param value needle
   * @param values haystack
   * @return how many times the needle occurs in the haystack
   */
   public static int countValues(int value, int[] values) {
       int count=0;
       for(int i=0;i<values.length;i++) {
           if(values[i]==value) {
               count++;
           }
       }
       return count;
   }

   /**
   * Returns how many ways the response could have been
   * produced from a given value sequence
   *
   * @param response output
   * @param values value sequence
   * @return how many PINs could have produced the same response given the value sequence
   */
   public static int numPossible(int[] response, int[] values) {
       int answer=1;
       for(int i=0;i<response.length;i++) {
           answer=answer*countValues(response[i],values);
       }
       return answer;
   }

   /**
   * Inputs a value sequence and PIN,
   * outputs challenge response
   *
   * @param args command-line arguments, ignored
   */
   public static void main(String[] args) {
       @SuppressWarnings("resource")
       final Scanner in = new Scanner(System.in);

       System.out.printf("Enter value sequence: ");
       final String seq = in.nextLine();

       System.out.printf("Enter PIN: ");
       final String pin = in.nextLine();
      
      
       if(!stringIsKDigits(seq,SEQ_DIGITS) || !allDigits(seq)) {
           System.out.println(ERR_SEQ);
       }else if(!allUppercaseLetters(pin)) {
           System.out.println(ERR_PIN);
       }else {
           int values[]=digitStringToIntArray(seq);
           int response[]=getResponse(pin,values);
           System.out.print("Response: ");
           for(int i=0;i<response.length;i++) {
               System.out.print(response[i]);
           }
           System.out.println();
       }
   }

}

OUTPUT::

TEST CASE 1::

Enter value sequence: 12345 Enter PIN: HELLO Invalid sequence
TEST CASE 2::

<terminatdLAa ava Application] C:Program File Enter value sequence: 0.16767265652 Enter PIN: HELLO Invalid sequence
TEST CASE 3::

<terminated> LA2a [Java Application] C:Progran Enter value sequence: 0123456789 Enter PIN: Hello Invalid PIN
TEST CASE 4::

<terminated> LA2a [Java Application] C:Program Files Enter value sequence: 0123456789 Enter PIN: HELLO Response: 43556
TEST CASE 5::

<terminated> LA2a [Java Application] CProgran Enter value sequence: 3231132213 Enter PIN: AGOT Response: 3121

Please provide the feedback!!

Thank You!!

Add a comment
Know the answer?
Add Answer to:
Given java code is below, please use it! import java.util.Scanner; public class LA2a {      ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PLEASE COMPLETE THE CODES. package javaprogram; import java.io.PrintStream; import java.util.ArrayList; import java.util.Scanner; /** * Movie class...

    PLEASE COMPLETE THE CODES. package javaprogram; import java.io.PrintStream; import java.util.ArrayList; import java.util.Scanner; /** * Movie class definition. * * @author David Brown * @version 2019-01-22 */ public class Movie implements Comparable { // Constants public static final int FIRST_YEAR = 1888; public static final String[] GENRES = { "science fiction", "fantasy", "drama", "romance", "comedy", "zombie", "action", "historical", "horror", "war" }; public static final int MAX_RATING = 10; public static final int MIN_RATING = 0; /** * Converts a string of...

  • DO NOT USE ANY EXTERNAL LIBRARIES! KEEP IT SIMPLE!!! import java.util.Scanner; public class StoryTime {   ...

    DO NOT USE ANY EXTERNAL LIBRARIES! KEEP IT SIMPLE!!! import java.util.Scanner; public class StoryTime {    /*Method name : descrambler() @returns: String @param: int @param: String */ // Make your method here    public static void main(String[] args) {        StoryTime s = new StoryTime();        Scanner scn = new Scanner(System.in);        System.out.println("Pick 1 for a joke or 2 for a Short Scary Story");        int input = scn.nextInt();        String str = "Juswert alois...

  • Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args)...

    Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); final int maxSize = 128; String[] titles = new String[maxSize]; int[] lengths = new int[maxSize]; int numDVDs = 0; String op; op = menu(stdIn); System.out.println(); while (!op.equalsIgnoreCase("q")) { if (op.equalsIgnoreCase("a")) { if (numDVDs < maxSize) numDVDs = addDVD(titles, lengths, numDVDs, stdIn); } else if (op.equalsIgnoreCase("t")) searchByTitle(titles, lengths, numDVDs, stdIn);    else if (op.equalsIgnoreCase("l")) searchByLength(titles, lengths, numDVDs, stdIn); System.out.println('\n');...

  • I need a java flowchart for the following code: import java.util.*; public class Main {   ...

    I need a java flowchart for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");       ...

  • in java coorect this code & screenshoot your output ---------------------------------------------------------------------- public class UNOGame {     /**...

    in java coorect this code & screenshoot your output ---------------------------------------------------------------------- public class UNOGame {     /**      * @param args the command line arguments      */         public static void main(String[] args) {       Scanner input=new Scanner(System.in);          Scanner input2=new Scanner(System.in);                             UNOCard c=new UNOCard ();                UNOCard D=new UNOCard ();                 Queue Q=new Queue();                           listplayer ll=new listplayer();                           System.out.println("Enter Players Name :\n Click STOP To Start Game..");        String Name = input.nextLine();...

  • Please help with Java programming! This code is to implement a Clock class. Use my beginning...

    Please help with Java programming! This code is to implement a Clock class. Use my beginning code below to test your implementation. public class Clock { // Declare your fields here /** * The constructor builds a Clock object and sets time to 00:00 * and the alarm to 00:00, also. * */ public Clock() { setHr(0); setMin(0); setAlarmHr(0); setAlarmMin(0); } /** * setHr() will validate and set the value of the hr field * for the clock. * *...

  • Convert Code from Java to C++ Convert : import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class...

    Convert Code from Java to C++ Convert : import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Template { public static void main(String args[]) { Scanner sc1 = new Scanner(System.in); //Standard input data String[] line1 = sc1.nextLine().split(","); //getting input and spliting on basis of "," String[] line2 = sc1.nextLine().split(" "); //getting input and spiliting on basis of " "    Map<String, String> mapping = new HashMap<String, String>(); for(int i=0;i<line1.length;i++) { mapping.put(line1[i].split("=")[0], line1[i].split("=")[1]); //string in map where in [] as key and...

  • Java class quiz need help~ This is the Backwards.java code. public class Backwards { /** *...

    Java class quiz need help~ This is the Backwards.java code. public class Backwards { /** * Program starts with this method. * * @param args A String to be printed backwards */ public static void main(String[] args) { if (args.length == 0) { System.out.println("ERROR: Enter a String on commandline."); } else { String word = args[0]; String backwards = iterativeBack(word); // A (return address) System.out.println("Iterative solution: " + backwards); backwards = recursiveBack(word); // B (return address) System.out.println("\n\nRecursive solution: " +...

  • complete this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class...

    complete this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class WordDetective { /** * Picks the first unguessed word to show. * Updates the guessed array indicating the selected word is shown. * * @param wordSet The set of words. * @param guessed Whether a word has been guessed. * @return The word to show or null if all have been guessed. */ public static String pickWordToShow(ArrayList<String> wordSet, boolean []guessed) { return null; //TODO...

  • Write code for RSA encryption package rsa; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class RSA...

    Write code for RSA encryption package rsa; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class RSA {    private BigInteger phi; private BigInteger e; private BigInteger d; private BigInteger num; public static void main(String[] args) {    Scanner keyboard = new Scanner(System.in); System.out.println("Enter the message you would like to encode, using any ASCII characters: "); String input = keyboard.nextLine(); int[] ASCIIvalues = new int[input.length()]; for (int i = 0; i < input.length(); i++) { ASCIIvalues[i] = input.charAt(i); } String ASCIInumbers...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT