Java Reference
In-Depth Information
elapsed since the unit was turned on. Write a program that allows the user to
enter as many waypoints as desired, storing each waypoint in an
ArrayList
,
where each waypoint is represented by a class that you design. Each waypoint
represents a successive sample point during a hike along some route. The coordi-
nates should be input as
double
s, and the timestamp as an integer. Have your
program compute the total distance traveled and the average speed in miles per
hour. Use the map scaling factor of 1 = 0.1 miles. For example, if the only two
waypoints are (X=1,Y=1,T=0) and (X=2,Y=1,T=3600) then the hiker traveled a
distance of 0.1 miles in 3600 seconds, or 0.1 miles per hour.
4.
Write a generic class,
MyMathClass
, with a type parameter
T
where
T
is
a numeric object type (e.g.,
Integer
,
Double
, or any class that extends
java.lang.Number
). Add a method named
standardDeviation
that takes an
ArrayList
of type
T
and returns as a
double
the standard deviation of the val-
ues in the
ArrayList
. Use the
doubleValue()
method in the
Number
class to
retrieve the value of each number as a double. Refer to Programming Project
6.2 for a definition of computing the standard deviation. Test your method
with suitable data. Your program should generate a compile-time error if your
standard deviation method is invoked on an
ArrayList
that is defined for
non-numeric elements (e.g.,
Strings
).
5.
Create a generic class with a type parameter that simulates drawing an item at
random out of a box. This class could be used for simulating a random drawing.
For example, the box might contain
Strings
representing names written on a
slip of paper, or the box might contain
Integers
representing a random drawing
for a lottery based on numeric lottery picks. Create an
add
method that allows
the user of the class to add an object of the specified type along with an
isEmpty
method that determines whether or not the box is empty. Finally, your class
should have a
drawItem
method that randomly selects an object from the box
and returns it. If the user attempts to drawn an item out of an empty box, return
null
. Write a
main
method that tests your class. To generate a random number
x
,
where 0
≤
x
≤
1, use
x
=
Math.random();
.
6.
Implement a priority queue capable of holding objects of an arbitrary type,
T
, by
defining a
PriorityQueue
class that implements the queue with an
ArrayList
. A
priority queue is a type of list where every item added to the queue also has an
associated priority. Define priority in your application so that those items with
the largest numerical value have the highest priority. Your class should support
the following methods:
•
Add(item, priority)
—Adds a new item to the queue with the associated
priority.
•
Remove()
—Returns the item with the highest priority and removes it from
the queue. If the user attempts to remove from an empty queue return
null
.