FOR MORE CLASSES VISIT
tutorialoutletdotcom
Assignment 8: Collection CPSC 1150-005/006, Spring 2017
Instructor: A. Goldberg
Langara College Introduction
You may work in pairs for this assignment. You will be using arrays to create and organize a
collection of your favourite collector’s item! You will first create a utility class with helper methods,
and use it when creating your MyCollection program. You will eventually submit both classes
(with Javadoc comments). Preparation
1. Assignment 8 using arrays
FOR MORE CLASSES VISIT
tutorialoutletdotcom
Assignment 8: Collection CPSC 1150-005/006, Spring 2017
Instructor: A. Goldberg
Langara College Introduction
You may work in pairs for this assignment. You will be using arrays to
create and organize a
collection of your favourite collector’s item! You will first create a
utility class with helper methods,
and use it when creating your MyCollection program. You will
eventually submit both classes
(with Javadoc comments). Preparation
Textbook sections
• 4.2.5
• 7.2, 7.6-7, 7.10-11 Most relevant lectures
• L14-15 Exercises
1. Read through the lab.
2. Decide on a type of item to collect, and make a list of at least 5
different types of that item.
The more the better. If you are typing your list, type it in array syntax,
for easy copying
later. For example: Rocks. Types are ["granite",
"limestone", "sandstone", . . . 1 Assignment 8:
1 Collection CPSC 1150-005/006, Spring 2017
Instructor: A. Goldberg
Langara College Utility class: ArrayHelpers
1. Create a class called ArrayHelpers. Create a main method which you
will use to test the
methods you develop.
2. 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 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)
3. 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 different 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 Assignment 8:
2 Collection CPSC 1150-005/006, Spring 2017
Instructor: A. Goldberg
Langara College The main program: MyCollection 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 definitions 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 Specifications 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.
4. 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, confirm 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. 3 Assignment 8:
2.2 Collection CPSC 1150-005/006, Spring 2017
Instructor: A. Goldberg
Langara College Sample output Note: I am aware that some of my
5. “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. 4 Assignment 8: Collection CPSC 1150-005/006,
6. Spring 2017
Instructor: A. Goldberg
Langara College 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
granite 1
limestone 1
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
0
Goodbye! Submission
Recall that submission instructions are in the Lab Guide. Your group is
required to submit one
.zip folder (in one person’s D2L dropbox) containing the modular,
internally documented with
javadoc, and properly styled source code files
• ArrayHelpers.java (should not have a main method)
• MyCollection.java
External documentation is not necessary. The files should contain both
partners’ names in the
headers. Make sure both partners save copies of the finished code to
their personal H: drive. 5
************************************************
7. Spring 2017
Instructor: A. Goldberg
Langara College 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
granite 1
limestone 1
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
0
Goodbye! Submission
Recall that submission instructions are in the Lab Guide. Your group is
required to submit one
.zip folder (in one person’s D2L dropbox) containing the modular,
internally documented with
javadoc, and properly styled source code files
• ArrayHelpers.java (should not have a main method)
• MyCollection.java
External documentation is not necessary. The files should contain both
partners’ names in the
headers. Make sure both partners save copies of the finished code to
their personal H: drive. 5
************************************************