Exercises
Exercises
Exercises
Sierra, Kathy; Bates, Bert; Gee, Trisha. Head First Java: A Brain-Friendly Guide (pp. 245-247).
O'Reilly Media. Kindle Edition.
File A
class Books {
String title;
String author;
}
class BooksTestDrive {
public static void main(String[] args) {
Books[] myBooks = new Books[3];
int x = 0;
while (x < 3) {
System.out.print(myBooks[x].title);
System.out.print(" by ");
System.out.println(myBooks[x].author);
x = x + 1;
}
}
}
File B
class Hobbits {
String name;
while (z < 4) {
z = z + 1;
h[z] = new Hobbits();
h[z].name = "bilbo";
if (z == 1) {
h[z].name = "frodo";
}
if (z == 2) {
h[z].name = "sam";
}
System.out.print(h[z].name + " is a ");
System.out.println("good Hobbit name");
}
}
}
int y = 0;
ref = index[y];
islands[0] = "Bermuda";
islands[1] = "Fiji";
islands[2] = "Azores";
islands[3] = "Cozumel";
int ref;
while (y < 4) {
System.out.println(islands[ref]);
index[0] = 1;
index[1] = 3;
index[2] = 0;
index[3] = 2;
System.out.print("island = ");
y = y + 1;
class TestArrays {
public static void main(String[] args) {
class HeapQuiz {
int id = 0;
}
class HeapQuizTest {
public static void main(String[] args) {
int x = 0;
HeapQuiz[] hq = new HeapQuiz[5];
while (x < 3) {
hq[x] = new HeapQuiz();
hq[x].id = x;
x = x + 1;
}
hq[3] = hq[1];
hq[4] = hq[1];
hq[3] = null;
hq[4] = hq[0];
hq[0] = hq[3];
hq[3] = hq[2];
hq[2] = hq[0];
} // do stuff
}
“Tawny, I know we’re tight on memory, but your spec said that we had to be able to access individual
contact information for all ten allowable contacts; this was the best scheme I could cook up,” said
Bob. Kate was next, already imagining coconut cocktails at the party, “Bob,” she said, “your solution’s
a bit kludgy, don’t you think?” Kate smirked, “Take a look at this baby”:
Contact contactRef;
while ( x < 10 ) { // make 10 contact objects
contactRef = new Contact();
x = x + 1;
}
// do complicated Contact list updating with contactRef
"I saved a bunch of reference variables worth of memory, Bob-o-rino, so put away your sunscreen,”
mocked Kate. “Not so fast Kate!” said Tawny, “you’ve saved a little memory, but Bob’s coming with
me.”