Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Utility class: ArrayHelpers 1. Create a class called ArrayHelpers. Create a main method that you will use to test the methods you develop. 2. Inside

Utility class: ArrayHelpers 1. Create a class called ArrayHelpers. Create a main method that you will use to test the methods you develop. 2. Inside this class, create (and test) the following seven methods. You must write all of the methods yourself, and not use built-in array methods to help you. (a) public static void printArrayQuantities(String[] arr) Displays all the unique elements of arr, along with a quantity (count) of each element. You may assume the array is sorted. (b) public static String getRandomElement(String[] arr) Returns a random element from the array arr. (c) public static String[] getRandomArray(int size, String[] stockElements) Generates and returns a String array of length size. The Strings in the array should be selected randomly from the array stockElements (use getRandomElement(stockElements) to do this). Its okay if an element appears multiple times! Think of stockElements as a list of all the possible Strings that might appear in your random array.

(d) public static void sort(String[] arr) Sorts the array arr using any sorting algorithm you prefer. Do not use a built-in sorting method. You must state in your comments which algorithm you are using. Sort by alphabetical order and ignore case. (e) public static int binarySearch(String[] arr, String key) Performs binary search on the array arr. Returns position of key, if found, or returns -low - 1 if not found. Do not use a built-in search method. Ignore case in your search. (f) public static String[] insert(String[] arr, String item, int pos) Generates and returns a new String array, which consists of the original array arr with the String item inserted at position pos. Note that the new array will have a greater length than the original array. (g) public static String[] remove(String[] arr, int pos) Generates and returns a new String array, which consists of the original array arr, with the String at position pos having been removed. Note that the new array will have a smaller length than the original array. 3. Note: It may be tempting to use a resizable structure that is not an array, however, the purpose of this assignment is to practice using arrays, so you may not use a dierent built-in data structure. 4. When all the methods in ArrayHelpers are working properly, delete the main method (including the method header). ArrayHelpers can no longer be run by itself, but the methods inside can be invoked from other classes (in the same directory or package). 2 The MyCollection program Write a program called MyCollection that creates a random collection of your type of item, and then repeatedly displays a menu to the user, allowing them to perform some actions to your collection. The collection should remain sorted (alphabetically) at all times. You will need to use methods from ArrayHelpers. Do not copy-paste the method denitions into your new program! Instead, you must invoke the methods directly from ArrayHelpers. Your program should be modular (made up of strongly cohesive and independent methods). You should use the top-down design process. A program with everything in the main method will not receive any design marks. 2.1 Specications Start-up: Generate a random collection of 10 items, then sort that collection. The items should be selected from a larger stock list that you prepared earlier. Menu: The option numbers should be as follows (but you can customize the labels for your own collection): Select an action, or enter 0 to exit. 1. Display 2. Search

When an option is selected, perform that action, then display the menu again. Inputs other than the possible options should not be accepted. The user should be prompted again until a valid input is selected. You may add extra options that provide extra functionality if you like! Display: Displays the entire collection (should appear in alphabetical order). Do not display duplicates separately. Instead, display a quantity for each item. Should also indicate the total size of the collection. Search: Ask the user to input an item to search for. If the item is found, ask if the user would like to remove that item. If they say yes, ask if the user would like to remove just one, or all copies of that item. If they say yes to any kind of removal, conrm before removing the item. After removing, display the quantity of items removed. If the item is not found, notify the user, and ask if the user would like to add that item to their collection. If they say yes, insert the item in the correct position. Validation: It should not be possible for a user to crash the program. Validate all inputs, and prompt again if an input is not understood

Sample output of MyCollection Note: I am aware that some of my rocks are actually minerals. Welcome to your rock collection! Select an option, or enter 0 to exit. 1. Display all rocks 2. Search for a specific rock 1 You have 10 rocks. feldspathic gneiss 2 limestone 2 magnetite 1 pyrite 1 quartz 1 schist 3 Select an option, or enter 0 to exit. 1. Display all rocks 2. Search for a specific rock 2 Enter a rock to search for: limestone Found limestone. Would you like to remove: 1. Just one 2. All 0. Do not remove any 1 Are you sure? y/n y Okay, 1 limestone removed. Select an option, or enter 0 to exit. 1. Display all rocks 2. Search for a specific rock 2 Enter a rock to search for: granite Did not find granite. Would you like to add a granite rock to your collection? y/n y Okay, 1 granite added.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David Kroenke, David Auer, Scott Vandenberg, Robert Yoder

8th Edition

013460153X, 978-0134601533

More Books

Students also viewed these Databases questions

Question

Distinguish between an assault and a battery.

Answered: 1 week ago

Question

Test procedures

Answered: 1 week ago