Java Software Solutions, 8e, Global Edition Exercise Solutions, Ch. 8
Chapter 8 Exercise Solutions
EX 8.1. Which of the following are valid declarations? Which instantiate an array object?
Explain your answers.
int primes = {2, 3, 4, 5, 7, 11};
Invalid; an int cannot be declared and initialized using an intializer list. The brackets are
missing.
EX 8.2. Describe five programs that would be difficult to implement without using arrays.
A program to find the average midterm score of 600 students enrolled in an introductory
computer science course.
EX 8.3. Describe how an element in an array is accessed in memory. For example, where is
myArray[25] stored in memory?
EX 8.4. Describe what problem occurs in the following code. What modifications should be
made to it to eliminate the problem?
EX 8.5. Write an array declaration and any necessary supporting classes to represent the
following statements:
b. students’ test grades for a class of 40 students
c. credit-card transactions that contain a transaction number, a merchant name, and a
charge
d. students’ names for a class and homework grades for each student
e. for each employee of the L&L International Corporation: the employee number,hire date, and
the amount of the last five raises
Java Software Solutions, 8e, Global Edition Exercise Solutions, Ch. 8
EX 8.6. Write code that sets each element of an array called items to twice the value in that
array element.
EX 8.7. Write code that prints half of the values stored in an array called items from front.
EX 8.8. Write code that sets every element of a boolean array called multiples to true, whose
position in the array is a multiple of 3.
EX 8.9. Write a method called averageArray that accepts an array of integers and returns the
average of the values stored in the array.
}
EX 8.10. Write a method called switchThem that accepts two integer arrays as parameters and
switches the contents of the arrays. Take into account that the arrays may be of
different sizes.
public void switchThem(int[] first, int[] second)
EX 8.11. Describe a program for which you would use the ArrayList class instead of arrays to
implement choices. Describe a program for which you would use arrays instead of the
ArrayList class. Explain your choices.
EX 8.12. What would happen if, in the Dots program, we did not provide empty definitions
for one or more of the unused mouse events?
EX 8.13. The Dots program listens for a mouse pressed event to draw a dot. How would the
program behave differently if it listened for a mouse released event instead? A mouse
clicked event?
EX 8.14. What would happen if the call to super.paintComponent were removed from the
paintComponent method of the DotsPanel class? Remove it and run the program to test
Java Software Solutions, 8e, Global Edition Exercise Solutions, Ch. 8
EX 8.15. What would happen if the call to super.paintComponent were removed from the
paintComponent method of the RubberLinesPanel class? Remove it and run the
program to test your answer. In what ways is the answer different from the answer to
Exercise 8.13?
EX 8.16. Create a UML class diagram for the Direction program.