Beruflich Dokumente
Kultur Dokumente
Name
Exercise 10.1 Conceptual O er ie! 1. A pro"ram needs many aria#les to store and process data. $o! does an array sol e this pro#lem%
'. (ary is usin" an array o) dou#les to store an employee*s !a"e amounts )or each day o) the !eek +(onday throu"h ,riday-. .ra! a picture o) this array !ith sample items and re)erences to each one.
Exercise 10.& /imple Array (anipulations 1. Assume that the array contains the )i e inte"ers '01 &'1 231 451 and 1&. Write the alues o) the )ollo!in" expressions:
a.
a[1]
b.
a[a.length 1]
c.
a[2] + a[3]
&. What happens !hen a pro"ram attempts to access an item at an index that is less than 0 or "reater than or e6ual to the array*s len"th%
Exercise 10.' 7oopin" 8hrou"h Arrays 1. Write a loop that prints all o) the items in an array a to the terminal screen.
'. Write a loop that locates the )irst occurrence o) a ne"ati e inte"er in an array a. When the loop is )inished1 the aria#le index should contain the index o) the ne"ati e num#er or the len"th o) the array i) there !ere no ne"ati e num#ers in the array.
#.
!tring tr = "#;
;. What is the ad anta"e o) usin" the instance aria#le length in a loop !ith an array%
Exercise 10.0 .eclarin" Arrays 1. .eclare and instantiate array aria#les )or the )ollo!in" data:
a. An array o) 1; dou#les
'.
0. Why is it #etter to use the )orm <t$%e&[] <'ariable& instead of <t$%e& <'ariable&[] !hen declarin" an array aria#le%
Exercise 10.; Workin" !ith Arrays that are not )ull 1. What happens !hen the pro"rammer tries to access an array cell !hose index is "reater than or e6ual to its lo"ical si<e%
&. .escri#e an application that uses an array that mi"ht not #e )ull.
'. .eclare and instantiate the aria#les )or parallel arrays to track the names1 a"es1 and /ocial /ecurity num#ers o) ;0 employees.
0. Assume that the array na(e contains the names o) people in a phone #ook and the parallel array n)(ber contains their phone num#ers. Write a code se"ment that displays each name and num#er in )ormatted columns +usin" ta# spacin"-. Assume that each num#er is the same len"th.
;. Write a code se"ment that creates parallel arrays containin" the )irst 10 non?ne"ati e po!ers o) &. One array should contain the exponent and the other array should contain & raised to that po!er.
Exercise 10.3 =sin" the Enhanced )or 7oop 1. Assume that array abc is )illed !ith strin"s. Con ert the )ollo!in" loops to simpler ersions usin" the enhanced )or loop. a.
for (int i = 0; i < abc.length; i++) !$ te(.o)t.%rintln(abc[i]);
#.
!tring target = ...; boolean fo)nd = fal e; for (int i = 0; i < abc.length; i++) if (target.e*)al (abc[i]))+ fo)nd = tr)e; brea,; -
&. 7ist t!o di))erent pro#lems )or !hich an enhanced )or loop !ould not #e appropriate.
Exercise 10.4 Arrays and (ethods 1. What happens !hen one uses the assi"nment operator +@- !ith t!o array aria#les%
'. Write a method that returns the a era"e o) the num#ers in an array o) do)ble.
0. Write a method )b.rra$ that expects an array o) int and t!o int as parameters. 8he inte"ers represent the startin" position and the endin" position o) a su#array !ithin the parameter array. 8he method should return a ne! array that contains the elements )rom the startin" position to the endin" position.
;. Write a method that searches a t!o?dimensional array )or a "i en inte"er. 8his method should return an o#Aect o) class /oint1 !hich contains a ro! and a column. 8he constructor )or /oint is /oint(an0nteger1 an0nteger).
Exercise 10.5 Array o) O#Aects 1. Write a method get2igh!t)dent that expects an array o) students as a parameter. 8he method returns the !t)dent o#Aect that contains the hi"hest score. Bou may assume that the !t)dent class has a method get2igh!core(). +$int: 8he method should declare a local aria#le o) type !t)dent to track the student !ith the hi"hest score. 8he initial alue o) this aria#le should #e n)ll.-