AP CS Chapter 6 short answer solutions
Describe programs that are difficult to implement without using arrays.
1.)a program to find the average midterm score of 600 students enrolled in an introductory computer science course 2.)a program to record and compute the sum of the snowfalls, recorded on a daily basis for the 40 days preceding the 2006 Winter Olympics 3.)a program to determine the relative frequency of each character in the Cyrillic alphabet in the original version of The Brothers Karamaso 4.)a program to compute the mean and standard deviation of the Dow Jones Industrial Average closings since September 11 5.)a program to store the coordinates of the vertices of polygons approximating the surface of a beating heart
Describe a program that would use the ArrayList class instead of arrays. Describe a program that would use arrays instead of the ArrayList class. Explain your choices
A program associated with a mail order Website for backpacks would use an object of the ArrayList class to implement the choices of colors of the backpacks because the colors and number of colors change with the seasons and as colors otherwise gain and lose popularity. An object of the ArrayList class can grow and shrink dynamically to accommodate these changes. A program associated with a personal day planner, with entries possible for each hour of the day, would use an array object to implement the choices for each hour of the day because the number of hours in a day, and hence the number of hours for which choices can be made for any given day, never changes. There is no need for the array object to grow or shrink to accommodate a larger or smaller number of hours
Write an array declaration and any necessary supporting classes to represent the following statement: For each employee of the L&L International Corporation: the employee number, hire date, and the amount of the last five raises
Employee[] LAndL = new Employee[staffSize] /* assumes int staffSize is assigned a value prior to the array declaration * public class Employee { private int employeeNumber; private String hireDate; private double raise[] = new double[5]; . . . }
Write a code fragment that loops through an ArrayList<Car> using a ListIterator and prints every element.
ListIterator it = myCars.listIterator(); while (it.hasNext()) { System.out.println(it.next()); } (where myCars is the ArrayList<Car>
Write an array declaration and any necessary supporting classes to represent the following statement: • Students' names for a class of 25 students
String[] students = new String[25];
Describe what problem occurs in the following code. What modifications should be made to it to eliminate the problem? int[] numbers = {3, 2, 3, 6, 9, 10, 12, 32, 3, 12, 6}; for (int count = 1; count <= numbers.length; count++) System.out.println (numbers[count] ) ;
The for loop fails to print the 0th element of the array, and attempts to print the nonexistent 11th element of the array. As a consequence, an ArrayIndexOutOfBoundsException is thrown. The problem can be eliminated by providing a for loop which initializes count to 0 (rather than 1) and tests if count is less than (rather than less than or equal to) numbers.length.
Write an array declaration and any necessary supporting classes to represent the following statement: Credit-card transactions that contain a transaction number, a merchant name, and a charge
Transactions[] charges = new Transactions[number] /* assumes int number is assigned a value prior to the array declaration */ public class Transactions { private int transactionNumber; private String merchantName; private double charge; . . . }
Write a code fragment that loops through an ArrayList<Car> using a foreach loop and prints every element
for (Car car : myCars) { System.out.println(car); } (where myCars is the ArrayList<Car>)
Write an array declaration and any necessary supporting classes to represent the following statement: Students' test grades for a class of 40 students
int[] grades = new int[40]; // for integer percentages or char[] grades = new char[40]; // for simple letter grades or String[] grades = new String[40]; /* for letter grades with +s and -s. */
valid declaration? does itinstantiate an array object? Explain your answers. int[] primes = new {2,3,5,7,11};
invalid; "new" on the right hand side of the assignment operator is neither necessary nor acceptable
valid declaration? does itinstantiate 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; "[]" is missing
valid declaration? does itinstantiate an array object? Explain your answers. int[] scores = int[30];
invalid; the right hand side of the assignment operator must contain either an initializer list or an expression of the form, "new int[30]"
valid declaration? does itinstantiate an array object? Explain your answers. char [] grades = new char[];
invalid; the size of the array must be indicated when the array is instantiated. Changing the right half of the statement to "new char[SIZE];", for example, would make the statement valid.
Write a method called sumArray that accepts an array of floating point values and returns the sum of the values stored in the array
public int sumArray (int[] values) { int sum = 0; for (int count = 0; count < values.length; count++) sum += values[count]; return sum; }
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) { if (first.length == second.length) { // copy contents of first into temp int [] temp = new int[first.length]; for (int i=0; i<first.length; i++) temp[i] = first[i]; //copy contents of second into first for (int i=0; i<first.length; i++) first[i] = second[i]; //copy contents of temp into second for (int i=0; i<first.length; i++) second[i] = temp[i]; } else { System.out.println("Arrays are of different " + "sizes and cannot be switched.") } }
valid declaration? does itinstantiate an array object? Explain your answers. float elapsedTimes[] = {11.47, 12.04, 11.72, 13.88};
valid; the "[]" can be placed either after the element type ("float") or after the reference variable ("elapsedTimes"
valid declaration? does itinstantiate an array object? Explain your answers. char grades[] = {'a', 'b', 'c', 'd', 'f'};
valid; the assignment is correct Java syntax
valid declaration? does itinstantiate an array object? Explain your answers. int[] scores = new int[30];
valid; the assignment is correct Java syntax