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

Utility class: ArrayHelpers 1. Create a class called ArrayHelpers. Create a main

ID: 3731948 • Letter: U

Question

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). It’s 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.

Explanation / Answer

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package arrayhelper;

/**
*
* @author DELL
*/
import java.util.Scanner;
public class ArrayHelper {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic
String[] arr=new String[]{"Apple","Orange","Banana"};

printArrayQuantities(arr); //Invoking method 1
String random=getRandomElement(arr); //Invoking method 2
System.out.println("Random String is: "+random);
System.out.println("How many strings you want in your random Array");
Scanner s=new Scanner(System.in);
int n=s.nextInt();
String[] randomArray= new String[n];
randomArray=getRandomArray(n,arr); //Invoking method 3
for(int i=0;i<n;i++)
System.out.println(randomArray[i]);
Sort(arr); //Invoking method 4
}
//Method 1
public static void printArrayQuantities(String[] arr){
  
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
  
}
//Method 2
public static String getRandomElement(String[] arr){
int ranPos = (int) (Math.random() * arr.length);
  
//Store String as per his integer position
String RanStr = arr[ranPos];
//String random = (arr[new Random().nextInt(arr.length)]);
return RanStr;
}
//Method 3
public static String[] getRandomArray(int size, String[] stockElements)
{
String[] RandomArr = new String[size];
for(int i=0;i<size;i++)
RandomArr[i]=getRandomElement(stockElements);

return RandomArr;

}
//Method 4
//Bubble sortinig Algorithm for sorting in Ascending order
private static void Sort(String[] arr) {

String temp;

// Outer For loop

for(int i=0; i<arr.length; i++) {

//Inner For loop

for(int j=0; j<arr.length-1-i; j++) {

//Open If Statement Compare and sort strings

if(arr[j].compareTo(arr[j+1])>0) {

temp = arr[j];

arr[j] = arr[j+1];

arr[j+1] = temp;

}//End of If Statement

}//End of Inner For Loop

}//End of Outer For Loop
System.out.println("Sorted Array is: ");
for(int i=0;i<arr.length;i++)
System.out.println(arr[i]);

}//End of Class Sort

}
// After executing this program you can delete the main method

run:
Apple
Orange
Banana
Random String is: Orange
How many strings you want in your random Array
2
Orange
Banana
Sorted Array is:
Apple
Banana
Orange
BUILD SUCCESSFUL (total time: 11 seconds)