0% found this document useful (0 votes)
9 views4 pages

Exercises

The document contains exercises from 'Head First Java' focusing on Java programming concepts. It includes tasks such as determining if Java files will compile, reconstructing scrambled code snippets, and analyzing object references in a heap. Additionally, it features a scenario where programmers compete to create a memory-efficient method for a client’s software installation.

Uploaded by

stasv4777
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Exercises

The document contains exercises from 'Head First Java' focusing on Java programming concepts. It includes tasks such as determining if Java files will compile, reconstructing scrambled code snippets, and analyzing object references in a heap. Additionally, it features a scenario where programmers compete to create a memory-efficient method for a client’s software installation.

Uploaded by

stasv4777
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

5.

Exercises
Sierra, Kathy; Bates, Bert; Gee, Trisha. Head First Java: A Brain-Friendly Guide (pp. 245-247).
O'Reilly Media. Kindle Edition.

5.1 BE the Complier


Each of the Java files on this page represents a complete source file. Your job is to play compiler and
determine whether each of these files will compile and run without exception. If they won’t, how
would you fix them?

File A
class Books {
String title;
String author;
}

class BooksTestDrive {
public static void main(String[] args) {
Books[] myBooks = new Books[3];
int x = 0;

myBooks[0].title = "The Grapes of Java";


myBooks[1].title = "The Java Gatsby";
myBooks[2].title = "The Java Cookbook";
myBooks[0].author = "bob";
myBooks[1].author = "sue";
myBooks[2].author = "ian";

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;

public static void main(String[] args) {


Hobbits[] h = new Hobbits[3];
int z = 0;

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");
}
}
}

5.2 Code Magnets


A working Java program is all scrambled up on the fridge. Can you reconstruct the code snippets to
make a working Java program that produces the output listed below? Some of the curly braces fell on
the floor and they were too small to pick up, so feel free to add as many of those as you need!

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;

String[] islands = new String[4];

System.out.print("island = ");

int[] index = new int[4];

y = y + 1;

class TestArrays {
public static void main(String[] args) {

5.3 A Heap o'Trouble


A short Java program is listed below. When “// do stuff” is reached, some objects and some
reference variables will have been created. Your task is to determine which of the reference variables
refer to which objects. Not all the reference variables will be used, and some objects might be
referred to more than once. Draw lines connecting the reference variables with their matching
objects. Tip: Unless you’re way smarter than we are, you probably need to draw diagrams like the
ones in “Life on the garbage-collectible heap”–60 of this chapter. Use a pencil so you can draw and
then erase reference links (the arrows going from a reference remote control to an object).

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
}

5.4 The case of the pilfered references


It was a dark and stormy night. Tawny strolled into the programmers’ bullpen like she owned the
place. She knew that all the programmers would still be hard at work, and she wanted help. She
needed a new method added to the pivotal class that was to be loaded into the client’s new top-
secret Java-enabled cell phone. Heap space in the cell phone’s memory was tight, and everyone
knew it. The normally raucous buzz in the bullpen fell to silence as Tawny eased her way to the white
board. She sketched a quick overview of the new method’s functionality and slowly scanned the
room. “Well folks, it’s crunch time,” she purred. “Whoever creates the most memory efficient version
of this method is coming with me to the client’s launch party on Maui tomorrow...to help me install
the new software.”
The next morning Tawny glided into the bullpen. “Ladies and Gentlemen,” she smiled, “the plane
leaves in a few hours, show me what you’ve got!” Bob went first; as he began to sketch his design on
the white board, Tawny said, “Let’s get to the point Bob, show me how you handled updating the list
of contact objects.” Bob quickly drew a code fragment on the board: Contact [] contacts = new
Contact[10];

while (x < 10 ) { // make 10 contact objects


contacts[x] = new Contact();
x = x + 1;
}
// do complicated Contact list updating with contacts

“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.”

You might also like