Concept explainers
The following code creates a small phone book. An array is used to store a list of names and another array is used to store the phone numbers that go with each name. For example, Michael Myers phone number is 333-8000 and Ash Williams’ phone number is 333-2325.
Write the method lookupName so the code properly looks up and returns the phone number for the input target name. lookupName should return a blank string if the name is not in the phone book.
Scanner kbd = new Scanner (System.in);
String[] name = (“Michael Myers”, “Ash Williams”, “Jack Torrance”, “Freddy Krueger”);
String [] phoneNumbers = (“333-8000”, “333-2323”, “333-6150”, “339-7970”):
System.out. print ln (“Enter name to look up.”);
String targetName = kbd.nextLine();
String targetPhone = lookupName (targetName, names, phoneNumbers);
System.out.println (“The phone number is” + targetPhone);
Program Project 13 asks you to rewrite this program using an array of a single object. The object-based approach is scalable than this approach, which requires a separate array to be managed for every property of the phone contact.
Want to see the full answer?
Check out a sample textbook solutionChapter 7 Solutions
Java: An Introduction to Problem Solving and Programming (7th Edition)
Additional Engineering Textbook Solutions
Starting Out with Python (4th Edition)
Starting Out with C++: Early Objects (9th Edition)
Artificial Intelligence: A Modern Approach
Concepts of Programming Languages (11th Edition)
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
- Create a class containing the main method. In the main method, create an integer array and initialize it with the numbers: 1,3,5,7,9,11,13,15,17,19 Pass the array as an argument to a method. Use a loop to add 1 to each element of the array and return the array to the main method. In the main method, use a loop to add the array elements and display the result. Note: In the loops, use the array field that holds the length of the array and do not use a number for array length.arrow_forwardTrue or False? Arrays cannot be passed as parameters, but must be used only in the main method.arrow_forwardIn programming language Java, make a programm that scans an array of numbers and then checks which numbers have ONLY one digit 7. For example: we input numbers 23, 143, 98, 2373, 77, 97, 7001. And it displays these numbers: 2373, 97, 7001. (Doesn't display 77 because it has 2 sevens).arrow_forward
- 2. write two method called findIndex and findLastIndex that search for a number in an array. they should take an int array and an int as parameters. findIndex should return the index of the first instance of the number in the array. findLastIndex should return the index of the last instance of the number in the array. They both should return -1 if the number is not found. Do not use any library methods. Sample code int a[] int x = findIndex(a,50); Sample output {10,50,30, 40,50}; 1 System.out.println(x); int a[] int x = findLastIndex (a,50); System.out.println(x); int a[] = {10,50,30,40,50}; int x = findIndex(a,70); {10,50,30,40,50}; 4 -1 System.out.println(x);arrow_forwardWrite a program that has an array of at least 10 string objects that hold people’s names and phone numbers. You may make up your own strings or use the following: "Becky Warren, 678-1223""Joe Looney, 586-0097""Geri Palmer, 223-8787""Lynn Presnell, 887-1212""Holly Gaddis, 223-8878""Sam Wiggins, 486-0998""Bob Kain, 586-8712""Tim Haynes, 586-7676""Warren Gaddis, 223-9037""Jean James, 678-4939""Ron Palmer, 486-2783" The program should ask the user to enter a name or partial name to search for in the array. Any entries in the array that match the string entered should be displayed. For example, if the user enters “Palmer” the program should display the following names from the list:Geri Palmer, 223-8787 Ron Palmer, 486-2783arrow_forwardURLify: Write a method to replace all spaces in a string with '%20'. You may assume that the string has sufficient space at the end to hold the additional characters, and that you are given the "true" length of the string. (Note: if implementing in Java, please use a character array so that you can perform this operation in place.)EXAMPLEInput: "Mr John Smith ", 13Output: "Mr%20John%20Smith"arrow_forward
- Assignment 5A: Multiple Frequencies. In the last assignment, we calculated the frequency of a coin flip. This required us to have two separate variables, which we used to record the number of heads and tails. Now that we know about arrays, we can track the frequency of all numbers in a randomly generated sequence. For this program, you will ask the user to provide a range of values (from 1 to that number, inclusive) and how long of a number sequence you want to generate using that number range. You will then generate and save the sequence in an array. After that, you will count the number of times each number occurs in the sequence, and print the frequency of each number. Hints: You can use multiple arrays for this assignment. One array should hold the number sequence, and another could keep track of the frequencies of each number. Sample Output #1: What's the highest number you want to generate?: 5 How Long of a number sequence do you want to generate?: 10 Okay, we'll generate 10…arrow_forwardWrite a method that receives an array to determine how many scores are above or equal to the average. Assume that the maximum number of scores is 100. The header of the method is as follows:arrow_forward3. Randomly generate a number in range [1, 12]. Map this number to a season: if the number is 3, 4, or 5, map it to “Spring", if the number is 6, 7, or 8, map it to "Summer", if the number is 9, 10, or 11, map it to "Fall", and if the number 12, 1, or 2, map it to "Winter". Display season namearrow_forward
- An Array is called wonderful if the frequency (number of occurrence) of each odd element is odd. Write a method that receives an integer array A then returns true if the array is wonderful or returns false if not. Note: All numbers in the array from 20 to 35. Example: A = {4,5,5,6,8,5,7,11,7,12,7} in this case the method should return true.arrow_forwardString[] name={"John", "Mary", "Jim", "Sam"}; Which choice is correct? Group of answer choices name.get(1)="John" name.get(1)="Mary" name[1]="John" name[1]="Mary"arrow_forwardAdd a new method to ArrayTools with filter. Make this method accept a String array. Change all the Strings that are longer than 5 characters to uppercase. Write in Java and use test cases 1 & 2 as a result.arrow_forward
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage