Scott Grissom
School of Computing & Info Systems
Grand Valley State University
picture

Lab: Ch 4 Arrays

Preparation

Do the following before arriving at lab:

  • Read section 4.12
  • Bring your textbook to lab

Objectives

After completing this lab, you should be able to:

  • dynamically allocate the size of an array
  • process data in an array
  • use the for loop to solve problems
  • generate random numbers
  • distinguish between private and public methods
  • use the Arrays.sort( ) method to sort an array of data

Exercises

Implement a class that includes the following:

  • DO NOT call your class "Arrays" or you will experience odd problems
  • an instance variable that holds an array of integers
  • public void fillRandomly (int N) - dynamically allocate an array with N elements and fill it with random numbers between 0 and 1000. Refer to sections 5.4.1 and 5.4.2 in your book to generate random numbers using the Random class.
  • private double calcAverage ( ) - calculate and return the average of all N elements.
  • private int findMin ( ) - identify and return the minimum value in the array. Do not sort the data.
  • private int findMax ( ) - identify and return the maximum value in the array. Do not sort the data.
  • private void printElements ( ) - print each element (ten per line). Use tabs ("\t") between each element to make the output more attractive. The trick is to insert a carriage return after every tenth number. See below for a sample.
  • private double findMedian ( ) - identify and return the median value. If the number of elements is odd then the median is the middle element. If the number of elements is even then the median value is the average of the two middle elements. You will need to sort the elements using the Arrays.sort ( ) method.
  • public void printResults ( ) - Write a method to test all of your previous methods by invoking them and printing the results from each. 1) print all elements by calling printElements( ), 2) print the average, 3) print the minimum, 4) print the maximum, and 5) print the median. Duplicate the sample output provided below. However, you might have more spaces between the numbers.
  • EXTRA CHALLENGING! private int findMode ( ) - identify and return the mode. Recall that the mode is the value that appears the most often regardles of where within the array. You will need to sort the elements using the Arrays.sort ( ) method. In the case of multiple values qualifying as the mode, return the smallest value (the first one discovered in the sorted list).
Sample Output
Sorted Elements:
2 9 19 30 31 31 31 37 37 53
58 62 66 67 82 101 102 110 114 124
129 143 146 150 165 180 186 209 215 249
279 308 323 323 357 359 361 385 388 410
421 428 429 432 440 440 448 455 457 472
480 485 493 503 511 514 526 527 552 557
565 568 577 586 595 603 624 640 641 652
678 721 722 732 734 745 753 755 775 781
782 787 793 797 810 819 842 842 863 871
876 890 903 915 915 919 927 947 983 992
Average: 469.21
Minimum: 2
Maximum: 992
Median: 476.0 Mode: 31

Grading Criteria

This lab is worth a possible 10 points. Turn in a printout of the source code and sample output with array of size 100.

  • methods (7 pts)
  • coding style (2 pts)
  • sample output (1)