# Inlab 8: Arrays, Part II

Due: Monday 25 July at 6:00 pm

## Purpose

• Do more sophisticated operations on array and with for loops.

## Partners

This is an individual assignment, though collaboration (not solution sharing) is allowed.

## A Note on Student Responsibility

This is a challenging lab. If you show up to lab, wait 20 seconds and throw your hand in the air claiming you have no idea how to even start, you will not complete it. The responsible student will attend lab prepared by understanding the lecture material and perhaps even having already read over or begun the lab. It is not your TA or Undergraduate Helper's responsibility to do this assignment for you.

• Create a project called Inlab8 and type this into a Driver.
• Create a class called Array.
• Make an instance variable for an integer array. This is the only instance variable you are allowed to have.
• The constructor for the class will be the same as for last week: Take an integer parameter that will be the number of elements in the array and create the array in the body.
• Create a method called fill that will assign each element in your array a random integer between 0 and 9 (including 0 and 9). That means it must be in that range, not 1-9 or 2-10.
• Create a method called print that will print out the contents of your array exactly as shown in the output below. It may seem silly, but it is easier than the outlab and will help prepare you for that print method.
• Create a method called sort that will sort the array in this fashion. You may NOT use any built in Java sorting functions.
• Create a method called printFrequency that adds up, and prints, the total number of occurrences of each possible value (0, 1, ..., 9) from a SORTED array (i.e. You can assume the array is sorted when this method is called).
• Here is output for the Driver provided (results will vary due to the randomness of fill).

## Hints

• I use three for-loops in my print method. One for the first line of dashes, the second for the array contents and vertical lines, and the third for the bottom set of dashes.
• My sort() method follows the sample slides pretty closely. Think about the clever use of a local boolean variable and what sorts of loops you need. You know you need to go through (almost) the entire array each pass, but you don't know how many passes you will have to do.
• My printFrequency() method has 7 lines of code (doesn't count lines with just a curly bracket). Think about what sort of loops you need. It may make sense to have a for-loop that iterates through the possible values (0-9) instead of through the slots in the array. Then, you need to somehow progress through the array counting up occurrences of the index the for-loop is currently at. Since the array is sorted, as soon as there is a mismatch between the value in the slot and the for-loop index, we know that we can move on and iterate the for-loop index. My print statement is somewhere in the loop, so I only have one System.out.println(...) call in my code.

## Submission

By Monday at 6:00 pm, submit the file Array.java into the appropriate D2L dropbox folder. DO NOT SUBMIT .class files.