Java Reference
In-Depth Information
3.
Revise the method
displayList
in Listing 12-2 so that it uses the list method
toArray
instead of methods
getLength
and
getEntry
.
4.
Suppose that you want an operation for the ADT list that returns the position of a given object in the list. The
header of the method could be as follows:
public int
getPosition(T anObject)
Write comments that specify this method.
5.
Suppose that you want an operation for the ADT list that removes the first occurrence of a given object from the
list. The header of the method could be as follows:
public boolean
remove(T anObject)
Write comments that specify this method.
6.
Suppose that you want an operation for the ADT list that moves the first item in the list to the end of the list. The
header of the method could be as follows:
public void
moveToEnd()
Write comments that specify this method.
7.
Write Java statements at the client level that return the position of a given object in the list
myList
. Assume that
the object is in the list.
8.
Suppose that the ADT list did not have a method
replace
. Write Java statements at the client level that replace an
object in the list
nameList
. The object's position in the list is
givenPosition
and the replacement object is
newObject
.
9.
Suppose that the ADT list did not have a method
contains
. Suppose further that
nameList
is a list of
Name
objects, where
Name
is as defined in Appendix B. Write Java statements at the client level that see whether the
Name
object
myName
is in the list
nameList
.
10.
Suppose that you have a list that is created by the following statement:
ListInterface<Student> studentList =
new
AList<Student>();
Imagine that someone has added to the list several instances of the class
Student
that Appendix C defined in Segment C.2.
a.
Write Java statements that display the last names of the students in the list in the same order in which the
students appear in the list. Do not alter the list.
b.
Write Java statements that interchange the first and last students in the list.
11.
Suppose that you have a list that is created by the following statement:
ListInterface<Double> quizScores =
new
AList<Double>();
Imagine that someone has added to this list the quiz scores received by a student throughout a course. The profes-
sor would like to know the average of these quiz scores, ignoring the lowest score.
a.
Write Java statements at the client level that will find and remove the lowest score in the list.
b.
Write Java statements at the client level that will compute the average of the scores remaining in the list.
12.
Consider a class
Coin
that represents a coin. The class has methods such as
getValue
,
toss
, and
isHeads
. The
method
getValue
returns the value, or denomination, of a coin. The method
toss
simulates a coin toss in which
the coin lands either heads up or tails up. The method
isHeads
returns true if a coin is heads up.
Suppose that
coinList
is an ADT list of coins that have randomly selected denominations. Toss each of these
coins. If the result of a coin toss is heads, move the coin to a second list called
headsList
; if it is tails, leave the
coin in the original list. When you are finished tossing coins, compute the total value of the coins that came up
heads. Assume that the list
headsList
has been created for you and is empty initially.