Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement the random-response solution discussed in slide 20 of Chapter 5 (Lectu

ID: 3926414 • Letter: I

Question

Implement the random-response solution discussed in slide 20 of Chapter 5 (Lecture A). At least 5 elements should be added to the responses. Comments and Screenshot are required to explain and demonstrate the code. Sample code (in slide 20 of Chapter 5A): private ArTayList responses; public Random Tester()//constructor {responses = new Array List(); responses.add("yes"); responscs.add("don't know"); public String get Response() {return responses.get(random.nextlnt(responses.size()));} Implement the code in slide 10 of Chapter 5 (Lecture B). Comments and Screenshot are required to explain and demonstrate the code. Sample code (in slide 10 of Chapter 5B) HashMap phoneBook; phoneBook = new HashMap

Explanation / Answer

import java.util.ArrayList;
import java.util.Random;

/**
* Program 1
*
* @author
*
*/
public class RandomTester {

   private ArrayList<String> responses;

   /**
   * default constructor to declare and add 5 elements
   */
   public RandomTester() {
       // TODO Auto-generated constructor stub
       responses = new ArrayList<String>();
       responses.add("yes");
       responses.add("dont know");
       responses.add("no");
       responses.add("Good");
       responses.add("i know");

   }

   /**
   * method to get the response from arraylist with the random index from 0 to
   * size
   *
   * @return
   */
   public String getResponse() {

       Random random = new Random();
       return responses.get(random.nextInt(responses.size()));

   }

   public static void main(String[] args) {

       RandomTester randomTester = new RandomTester();
       System.out.println(randomTester.getResponse());
   }

}

OUTPUT:

no

import java.util.HashMap;

/**
* Program 2
*
* @author
*
*/
public class PhoneBookTest {
   /**
   * @param args
   */
   public static void main(String[] args) {
       // declaration hashmap of string key and value
       HashMap<String, String> phoneBook;
       phoneBook = new HashMap<String, String>();

       // adding values to hashmap
       phoneBook.put("Charles Nguyen", "(531)9392 4587");
       phoneBook.put("Lisa Jones", "(402)4536 4674");
       phoneBook.put("William H.Smith", "(998)5488 0123");

       // get the phone number using key
       String phoneNumber = phoneBook.get("Lisa Jones");

       // print the value
       System.out.println(phoneNumber);
   }

}

OUTPUT:

(402)4536 4674