CS-161 Lab #7: Arrays, Loops, Constants, and JavaDoc. General At the top of the every document that you create (word processing files or source files) include: /** * Description of the class or document. * * @author YOUR NAME * @version CS161 Lab #, mm/dd/yyyy (replace with the last edit date) */ Submit in this lab via Moodle using the “Lab #” link. Your assignment must be uploaded by midnight of the due date. Moodle will automatically close the link at that time. It is strongly highly recommend you do not wait until the very last minute to submit your work. Concepts For this lab, you will explore the basics of Java arrays, the use of counting (for) loops to process data contained within the array, and the basics of JavaDoc formatted comments. This will also be the first lab in which you will create a completely new project and class from scratch so that you can convince yourself that there is no hidden “magic” happening that you are not aware of in the code. Background Read and review the section on Arrays in chapter #4. You are to create a new project and class from scratch for this lab. Read and review the sections from Chapter #5 on random number generation, using JavaDoc comments, and constants. Assignment Instructions Part 1: Copy the completed BlueJ project “BadAccumulator” from lab #6 to a new project called “BetterAccumulator”. Delete all of the “memoryCell” fields and create a single new field that is a Java integer array (see the last sections of Chapter #4) with 256 elements (0-255 for the indexes). This will simulate 256 memory cell locations. Now modify all of the remaining methods to use this array to simulate memory. If you do this correctly, it will DRAMATICALLY shorten and simplify the code. Think about what “BadAccumulator” would look like with 256 memory cells!!! Put in error checking on each of the methods to make sure that the “address” parameter is within the valid range allowed (0-255), simply print out an error message if the address given is outside of the valid range. Part 2: The general requirements for part two are to create a new project from scratch that contains a single class (name the class anything that seems appropriate to you). This class will have an array of integers “int[ ]” as a field (and not an arraylist), an integer constant field “SIZE” that defines the SIZE of the array, and an integer constant that defines the RANGE of values to put into the array. Note that constants use a style where their name is in all caps. The constructor will create the array object using the SIZE constant and call fillArray() to fill it with random values. Then you will define several additional methods that do the following (you must use the method signatures as I have given them). Your methods should be written generically enough so that you can change the array size constant and the methods will continue to work correctly. Additionally, you MUST use “for” loops for this lab, NOT “for-each” loops. For loops are the preferred iteration structure to use with Arrays and will help you better understand indexes. Western Oregon University Page 1 of 3 CS-161 Lab #7: Arrays, Loops, Constants, and JavaDoc. So here is the list of general requirements for this lab: 1) Create a new, empty class. Call it anything appropriate. 2) Define 2 constant fields (public static final fields): SIZE & RANGE 3) Define 1 integer array field 4) Define a default constructor that initializes the array field by calling “fillArray( )” 5) Implement all of the method interfaces defined below 6) Write JAVADOCs for each public method and public class that you define 7) Use “For” loops for all array processing in this lab (no “For-Each” loops). You may create any additional “private” helper methods that you determine are needed or helpful. 1) public void fillArray() fills each element of the array with a random integer in with values in the range from 0 to RANGE. Use the RANGE constant field in your class: public static final int RANGE = 100000; 2) public int getMax() this method returns the largest integer in the array. 3) public int getMin() this method returns the smallest integer in the array. 4) public int getSum() this method returns the sum of all integers in the array. 5) public int getAvg() this method returns the average value of the integers in the array. 6) public void printContents() prints each element value in the array from the item in index 0 through the item in the max index location. 7) public void testMethods() calls and prints the resulting values from the above methods to verify their correctness. The code for this method must be exactly as follows: /** * Tests the methods to verify their correctness; numeric * values will change due to the generation of random * values, but the math is easy enough * to visually verify if things are working */ public void testMethods() { fillArray(); printContents(); System.out.println("SIZE value: " + SIZE); System.out.println("Max value: " + getMax()); System.out.println("Min value: " + getMin()); System.out.println("Sum: " + getSum()); System.out.println("Average: " + getAvg()); } Western Oregon University Page 2 of 3 CS-161 Lab #7: Arrays, Loops, Constants, and JavaDoc. Test your code with several different values for the SIZE constant to see that your code properly adapts to different values. BE SURE that you have set your SIZE constant to 10 in the code that you turn in and your RANGE constant to 100000, then when you execute testMethods( ) with these values for RANGE and SIZE, your output should match the following, except for the numbers (which should be similar in range): [0] 95934 [1] 45366 [2] 60428 [3] 81464 [4] 24412 [5] 85560 [6] 14309 [7] 76920 [8] 33567 [9] 61862 SIZE value: Max value: Min value: Sum: Average: Challenge exercise: Search the internet for selection or bubble sort Java code. Integrate the code into your program or write a sort method from scratch. 10 95934 14309 579822 57982 OPTIONAL CHALLENGE QUESTION (not for extra credit, just to extend your knowledge): public void sort() sorts the elements in the array in numeric order using either bubble sort or selection sort. Search for information on these algorithms on the Internet, and add this method to your code. TURN IN BOTH of your BlueJ project folders for this lab zipped into a SINGLE ZIP FILE. Both projects should be properly commented and formatted, including the identifying information noted in the “general section”. Starting with this lab you MUST do JavaDoc comments for EACH public CLASS and EACH public METHOD that you create or modify. Starting with Lab #3, it is required that you run the code formatter (BlueJ Auto-layout) over ALL of the classes before you Zip and submit your lab. 1) ZIP BOTH of the entire project folders, DO NOT delete any of the files. You will upload the SINGLE ZIP compressed file that contains both project folders. Please ONLY submit Zip formats. Other compression formats require the grader to spend more time opening various tools to handle other formats, so only ZIP will be accepted. You should put both project folders into one zipped file. 2) Submit any required document as a separate file. 3) Log in to Moodle. 4) Click the Lab # link in the week # activity block in the class Moodle site, then browse to your document and upload the document you created. 5) If you need to submit an updated version, click on the submit link and you will see the file you previously uploaded, click the edit these files button. On the right of the file name you will see this icon . Click on the icon, delete your previous submission, and then upload your updated version. Western Oregon University Page 3 of 3