Java Reference
In-Depth Information
17.
The last line would change to the following:
I ate cod, broccoli, , peanuts, and apples.
18.
public void
removeAll()
{
numberUsed = 0;
}
19.
public void
increaseCapacity(
int
newCapacity)
{
if
(newCapacity > numberUsed)
{
maxNumberElements = newCapacity;
double
[] temp =
new double
[newCapacity];
for
(
int
i = 0; i < a.length; i++)
temp[i] = a[i];
a = temp;
}
//else do nothing.
}
20.
All you need to do to make your code work for sorting into decreasing order is to
replace the
<
with
>
in the following line of the definition of
indexOfSmallest
:
if
(a[index] < min)
However, to make your code easy to read, you should also rename the method
indexOfSmallest
to
indexOfLargest
, rename the variable
min
to
max
, and
rename the variable
indexOfMin
to
indexOfMax
. You should also rewrite some of
the comments to reflect these changes.
21.
If an array has a value that occurs more than once and you sort the array using
the method
SelectionSort.sort
, then there will be as many copies of the
repeated value after the array is sorted as there originally were in the array.
extra code
on CD
22.
We give two slightly different versions. Both versions are on the accompanying CD.
import
java.util.Scanner;
public class
ForEachEnumDemo
{
enum
WorkDay {MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY};
public static void
main(String[] args)
{
WorkDay[] day = WorkDay.values();
Scanner keyboard =
new
Scanner(System.in);
double
hours = 0, sum = 0;
for
(WorkDay oneDay : day)