Java Reference
In-Depth Information
13.
Write a method called
reverse3
that accepts an
ArrayList
of integer values as a parameter and reverses each suc-
cessive sequence of three values in the list. If the list has extra values that are not part of a sequence of three, those
values are unchanged. For example, if a list stores values
[3, 8, 19, 42, 7, 26, 19, -8]
, after the call the
list should store the values
[19, 8, 3, 26, 7, 42, 19, -8]
. The first sequence of three
(3, 8, 19)
has been
reversed to be
(19, 8, 3)
. The second sequence
(42, 7, 26)
has been reversed to be
(26, 7, 42)
, and so on.
Notice that
19
and
-8
are unchanged because they were not part of a sequence of three values.
14.
Write a method called
removeShorterStrings
that accepts an
ArrayList
of strings as a parameter and removes
from each pair of values the shorter string in the pair. If the list is of odd length, the final element is unchanged. For
example, suppose that a list contains
["four", "score", "and", ""seven", "years", "ago", "our"]
. In
the first pair (
"four"
and
"score")
the shorter string is
"four"
. In the second pair
("and"
and
"seven")
the
shorter string is
"and"
. In the third pair
("years"
and
"ago"
) the shorter string is
"ago"
. Your method should
remove these shorter strings, changing the list to store
["score", "seven", "years", "our"]
. If both strings
in a pair have the same length, remove the first string in the pair.
15.
Write a method called
clump
that accepts an
ArrayList
of strings as a parameter and replaces each pair of strings
with a single string that consists of the two original strings in parentheses separated by a space. If the list is of odd
length, the final element is unchanged. For example, suppose that a list contains
["four", "score", "and",
"seven", "years", "ago", "our"
]. Your method should change the list to store
["(four score)", ("and
seven"), ("years ago"), "our"].
16.
Write a method called
interleave
that accepts two
ArrayList
s of integers
a1
and
a2
as parameters and inserts
the elements of
a2
into
a1
at alternating indexes. If the lists are of unequal length, the remaining elements of the
longer list are left at the end of
a1
. For example, if
a1
stores
[10, 20, 30]
and
a2
stores
[4, 5, 6, 7, 8]
, the
call of
interleave(a1, a2);
should change
a1
to store
[10, 4, 20, 5, 30, 6, 7, 8]
. If
a1
had stored
[10, 20, 30, 40, 50]
and
a2
had stored
[6, 7, 8]
, the call of
interleave(a1, a2);
would change
a1
to
store
[10, 6, 20, 7, 30, 8, 40, 50]
.
17.
Modify the
Point
class from Chapter 8 so that it defines a natural ordering by implementing the
Comparable
inter-
face. Compare the
Points
by
y
-major order; that is, points with smaller
y
-coordinate values should come before
those with higher
y
-coordinate values. Break ties by comparing
x
-coordinate values.
18.
Modify the
TimeSpan
class from Chapter 8 to include a
compareTo
method that compares time spans by their
length. A time span that represents a shorter amount of time is considered to be “less than” one that represents a longer
amount of time. For example, a span of 3 hours and 15 minutes is greater than a span of 1 hour and 40 minutes.
19.
Modify the
CalendarDate
class from this chapter to include a year field, and modify its
compareTo
method to
take years into account when making comparisons. Years take precedence over months, which take precedence over
days. For example, July 18, 1995 comes before March 2, 2001.
Programming Projects
1.
Write classes to model a shopping list. Make an
Item
class that represents a grocery item's name and price, such as
tissues for $3. Also implement an
ItemOrder
class that represents a shopper's desire to purchase a given item in a
given quantity, such as five boxes of tissues. You might wish to implement bulk-discounted items, such as two boxes
of tissue for $4, which would bring the cost of the given item order of 2
+
2
+
1 boxes of tissues to $4
+
$4
+
$3, or
$11.00. Lastly, implement a
ShoppingCart
class that stores
ItemOrder
s in an
ArrayList
and allows item orders
Search WWH ::
Custom Search