Page 460 - Introduction to Programming with Java: A Problem Solving Approach
P. 460
426 Chapter 10 Arrays and ArrayLists 4. [after §10.4] Zoo Animals Program:
As part of your internship at Parkville’s new zoo, you’ve been asked to write a program that keeps track of the zoo animals. You want to make the program general purpose so that when you’re done, you can sell your program to zoos worldwide and make millions. Thus, you decide to create a generic Zoo class.
Write a Zoo class. Your class does not have to do very much—it simply handles the creation and printing of Zoo objects. To give you a better idea of the Zoo class’s functionality, we provide a main method:
}
public static void main(String[] args)
{
b
Zoo zoo1 = new Zoo();
String[] animals = {"pig", "possum", "squirrel", "Chihuahua"};
Zoo zoo2 = new Zoo(animals, "Parkville");
animals[0] = "white tiger";
Zoo zoo3 = new Zoo(animals, "San Diego");
zoo1.display();
zoo2.display();
zoo3.display();
When run, the main method should print this: The zoo is vacant.
Parkville zoo: pig, possum, squirrel, Chihuahua
San Diego zoo: white tiger, possum, squirrel, Chihuahua
Although it’s not required, you’re encouraged to write a complete program in order to test your Zoo class. Apago PDF Enhancer
5. [after §10.5] Assume that this code fragment compiles and runs. What is its output? Be precise when showing your output.
char[] a = new char[3];
char[] b;
for (int i=0; i<a.length; i++)
{
}
a[i]
= a;
= 'a';
'b';
b[2] =
System.out.println("a[1]=" + a[1] + ", a[2]=" + a[2]);
System.out.println("b[1]=" + b[1] + ", b[2]=" + b[2]);
6. [after §10.5] What needs to be added to the following code fragment so that all values except the first two values (100000.0 and 110000.0) are copied from allSalaries to workerSalaries?
double[] allSalaries = {100000.0, 110000.0, 25000.0, 18000.0,
30000.0, 9000.0, 12000.0};
double[] workerSalaries;
7. [after §10.5] The following program is supposed to reverse the order of the elements in the simpsons array. It compiles and runs, but it doesn’t work properly.
public class Reverse
public static void main(String[] args)
{
{
String[] simpsons = {"Homer", "Flanders", "Apu"};