New Required Java Skills: Arrays
Resources available for teachers. Get Access
In previous lessons, you have used the Finch sensors to enable the Finch to respond to its environment by making decisions based on light, temperature, acceleration, and the presence of obstacles. You can also use the Finch as a measurement tool. For example, the code below measures the value of the Finch’s left light sensor every second. It stores this data in an array called leftLightSensor.
Modify the code above so that the Finch moves along a path and records light sensor data at 15 locations. At each point, it should record the values of both light sensors and store this data in two arrays.
Create a new array named sumLight. This array should contain the sum of the right and left light sensor values for each location.
Declare a variable named maxLight. Its initial value should be 0. Then use a for loop to move through the sumLight array. If you find a value bigger than the current value of maxLight, set maxLight equal to this new, larger value. At the end of the for loop, you have found the maximum light value observed by the Finch! How do you know this is the maximum value?
Declare a variable named minLight and use it to find the minimum value in sumLight. What should the initial value of minLight be?