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 (8th 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)
- The zoo will have several animals, and each animal will have a name, age, and species. Moreover, each animal will have a method make_sound(), which should return a string representing the sound it makes. Perform the following steps: Create a Zoo class. This class should be able to store multiple animals. It should have methods to add_animal and remove_animal. Also, add a method get_animals that prints a list of all animals currently in the zoo, including their name, age, and species. Create an Animal parent class. This class should have attributes for name, age, and species. Include a make_sound() method that returns a string representing the sound the animal makes. Create two or more child classes that inherit from Animal (for example, Lion and Elephant - you get to choose the animal types). These child classes should override the make_sound() method to return a sound specific to the animal they represent. Your program should be interactive. It should provide a menu to the user that…arrow_forwardA class plays a fishing game in which a large tub is filled with plastic fish that are identical, except that some are red and the rest are white. A student is blindfolded and pulls 10 fish out of the tub. The student removes the blindfold, writes down how many of each color fish she got, and then puts the fish back in the tub. Each student takes a turn. The results are shown in the table. Display these data in a dot plot. Write and answer (to the extent possible) at least three questions about the data display in part (a); include at least one question at each of the three graph-reading levels discussed in the reading.arrow_forwardCreate 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_forward
- In this project you will generate a poker hand containing five cards randomly selected from a deck of cards. The names of the cards are stored in a text string will be converted into an array. The array will be randomly sorted to "shuffle" the deck. Each time the user clicks a Deal button, the last five cards of the array will be removed, reducing the size of the deck size. When the size of the deck drops to zero, a new randomly sorted deck will be generated. A preview of the completed project with a randomly generated hand is shown in Figure 7-50.arrow_forwardPattern and Value are two strings that you must use. The pattern string, which denotes a pattern inside a string, is made up just of the letters a and b. For instance, the string catcatgocatgo matches the pattern aabab (where cat is represented by letter A and go by letter B). Additionally, it matches a, ab, and b pattern. To check whether a value matches a pattern, create a method.arrow_forwardThis method returns a string with a number of horizontal bars that correspond to the bars parameter. The method most not rely on System.out.println(). No arraysarrow_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_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
- Use Java programming language Write a program that asks the user to enter 5 test grades (use an array to store them). Output the grades entered, the lowest and highest grade, the average grade, how many grades are above the average and how many are below and the letter grade for the average grade. Create a method that returns the lowest grade. Create a method that returns the highest grade. Create a method that returns the average grade. Create a method that returns how many grades were above the average. Create a method that returns how many grades were below the average. Create a method that returns the letter grade of the average (90-100 - A, 80-89 - B, 70-79 - C, < 70 - F)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_forwardIn python Add the following four methods to your Crew class: move(self, location): This takes in a location as a string, along with self, and attempts to move the crew member to the specified location. If location is one of the five valid location options ("Bridge", "Medbay", "Engine", "Lasers", or "Sleep Pods"), then this should change self.location to that new value. Otherwise, the function should print out the message: Not a valid location. repair(self, ship): first_aid(self, ship): fire_lasers(self, ship, target_ship, target_location): The above three methods represent tasks that a basic Crew member is not capable of (but one of its derived classes will be able to accomplish). So each of them should simply print out a message of the form: <Name> doesn't know how to do that. Examples: Copy the following if __name__ == "__main__" block into your hw12.py file, and comment out tests for parts of the class you haven’t implemented yet. if __name__ == '__main__': crew1…arrow_forward
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage