Java Reference
In-Depth Information
12.
Implement the ADT recipe that Project 7 of Chapter 12 describes.
13.
Repeat Project 5 of Chapter 4, but use an instance of
AList
instead of a bag.
14.
Revise
ListInterface
, as given in Listing 12-1 of the previous chapter, so that each of the methods
add
,
remove
,
replace
, and
getEntry
throws an exception if the position passed to it is out of range. Then revise the class
AList
so that it implements your revised interface.
The popular social network Facebook
™
was founded by Mark Zuckerberg and his classmates at Harvard University
in 2004. At the time, he was a sophomore studying computer science.
Design and implement an application that maintains the data for a simple social network. Each person in the
network should have a profile that contains the person's name, optional image, current status, and a list of friends.
Your application should allow a user to join the network, leave the network, create a profile, modify the profile,
search for other profiles, and add friends.
15.
A
NSWERS
TO
S
ELF
-T
EST
Q
UESTIONS
1.
When the name comes after the name of the student in the last occupied desk; the new student then sits at the desk
after the last one that is currently occupied.
2.
The students remain in consecutively numbered positions. You do not have to keep track of the locations of the
empty desks.
3.
Time is saved by not moving the students.
4.
Advantage: It is easier to implement this
add
method. Your code will more likely be correct if the other
add
method is correct.
Disadvantage: Invoking another method uses more execution time. Additionally, the second
add
method invokes
makeRoom
needlessly.
a.
a b c d w e
b.
a b c d e w
c.
5.
The operation in Part
a
6.
myList.add(newEntry)
. The other
add
method validates the position 6 and then needlessly invokes
makeRoom
.
7.
If the list is empty,
numberOfEntries
is zero, so
(givenPosition >= 1) && (givenPosition <= 0)
is always
false. Thus, the only way that the previous expression can be true is if the list is not empty.
8.
By the reasoning given in the previous answer, if the list is empty, the initial value of
result
is returned.
9.
We could, but it is not necessary. After
remove
decrements
numberOfEntries
,
list[numberOfEntries]
is the
array location that Haley originally occupied. So
list[numberOfEntries]
and
list[numberOfEntries - 1]
both reference Haley. Since we are not deallocating Haley, we do not have to set
list[numberOfEntries]
to
null
. We can simply ignore its contents.
10.
The method
clear
could take one of the following actions to deallocate the objects currently in a list: (1) Set elements
of the array
list
to
null
; (2) repeatedly remove the last entry by repeatedly calling
remove(numberOfEntries)
;
(3) reallocate the array
list
.
11.
O(1); O(
n
)
12.
O(1); O(1)
13.
O(1); O(1)
14.
O(1) when the entry is found immediately in position 1. O(
n
) when the entry is found in the last position or is not
found at all.