Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Complete Chapter 8, Programming Activity 1: Working with Arrays. Make sure you study the Programming Activity 8-1 Guidance document. Programming Activity 8-1 Guidance ================================= This

Complete Chapter 8, Programming Activity 1: Working with Arrays.

Make sure you study the Programming Activity 8-1 Guidance document.

Programming Activity 8-1 Guidance ================================= This assignment gives you practice with basic operations on a single dimensional array of int values. The name of the array you will be using is arr. Here is the framework statement that defines arr: private static int [] arr; Here is the framework statement that creates arr: arr = new int[SIZE]; Note that arr is an array of int items. Array indexes are 0-relative ---------------------------- When we arrange 10 items in a sequence, we naturally refer to the first item as position 1, the second item as position 2, ..., and the last item as position 10. It is natural for computers to refer to OFFSETs from a base address. Because of this, arrays use 0-relative indexing. In a 10 item array, a programming language refers to the first item as index 0, the second item as index 1, ..., and the last item as index 9. The last item has an index one less that the number of items in the array. This affects loop control variable tests in "for" loops. Suppose we have an array of 10 items called array. Here is a standard "for" loop to traverse the array: for (int i = 0; i < 10; i++) { // Do something with array[i] } A common mistake is to use the following WRONG form: for (int i = 0; i <= 10; i++) { // Do something with array[i] } The problem here is that the final iteration of the loop will try to reference array[10], which causes an ArrayOutOfBoundsException exception because the last VALID index for this array is 9. Here is another point of confusion. Suppose you want to use a "for" loop to do SOMETHING 10 times that does NOT involve traversing through an array (or String). The most natural way to do this is as follows: for (int i = 1; i <= 10; i++) { // Do SOMETHING that does not involve an array (or String) index. } Use an array's length --------------------- When looping through an array, use the length of the array to control the loop as follows: int sum = 0; for (int i = 0; i < someArrayOfInts.length; i++) { sum += someArrayOfInts[i]; } Note the less than (<) in the loop control condition. Using less than or equal (<=) will cause an ArrayOutOfBoundsException exception. This is because "length" is a 1-relative concept, whereas an array index (like i) is 0-relative. Note that the length of an array is a field, not a method. It is NOT length(), but rather just length. Using "length" is better than hard-coding a number like 10. If you hard-code a length and the requirements change such that the array needs to be made larger (more slots), then you might increase the array size, but forget to change your hard-coded length in the "for" loop. Your code will still compile, but will have a logic error. This will cause untold damage until it is discovered. If you hard-code a length and the requirements change such that the array needs to be made smaller (fewer slots), then you might decrease the array size, but forget to change your hard-coded length in the "for" loop. Your code will still compile, but will cause an ArrayOutOfBoundsException exception. Part 1 ------ This part is provided as an example in the framework. Although the framework provides a SIZE constant, it is a better practice to use arr.length as shown. Part 2 ------ The framework comments specify that each printed element is to be separated from the next by a space, not by a new line. This means each element must be appended with a space and printed using System.out.print(). Part 3 ------ To change an array value, simply put a reference to that slot on the left side of an assignment statement. Part 4 ------ To compare an int array value, simply put a reference to that slot on the left side of an equality operator (==). Part 5 ------ This week's outline contains the following similar code: Finding the index of the maximum value of an array: int indexMaxValue = 0; for (int i = 1; i < aTestScores.length; i++) { if (aTestScores[i] > aTestScores[indexMaxValue]) { indexMaxValue = i; } } It is easy to tweak this code to find a minimum value for a different array. 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro Database Migration To Azure Data Modernization For The Enterprise

Authors: Kevin Kline, Denis McDowell, Dustin Dorsey, Matt Gordon

1st Edition

1484282299, 978-1484282298

More Books

Students also viewed these Databases questions