Java Reference
In-Depth Information
Compiling and running this program, we obtain the following console output:
2 versus (!=) 3
(2,1)
(3,4)
5.8 Exercises
Exercise 5.1 (A class for storing supermarket product items)
In a supermarket, each product is typically labeled with:
-
(1) a string depicting the product name,
-
(2) a price tag, and
-
(3) a date of validity that informs when the product expires.
Write a class
Date
for storing dates with a constructor that initializes
these various fields. Write another class
Product
for storing information
related to products that use the class
Date
. Store a collection of product
items into an array of
Product
elements. Given a product query element
Element
, write a function that checks whether the product is inside the
array or not. The equality test shall be defined by using the product
strings only. Write a function that, given a price range
low
and
high
,
reports all product items with prices falling inside this range. Finally,
describe a function that takes as its argument a given date, and report
all elements of the array expiring before this date.
Exercise 5.2 (Lexicographic order on 2D points)
Write a class
Point2D
for storing the (
x, y
) coordinates of points. We
would like to totally order points so that given two points
P
1
=(
x
1
,y
1
)
and
P
2
=(
x
2
,y
2
),
P
1
<P
2
if and only if
x
1
<x
2
or
x
1
=
x
2
and
y
1
<
y
2
. Write a predicate function
static boolean LessThan(Point2D P1,
Point2D P2)
that implements this lexicographic order. Consider now an
array of 2D points. Give a function that reports the smallest point defined
with respect to that lexicographic order. Extend these functions to 3D
point sets stored in an array.
Exercise 5.3 (Lexicographic order for the Olympic games)
Design a class
Medals
for storing the number of gold, silver and bronze
medals of a given country. Provide a constructor for that class. Define
then a lexicographic order on two objects
a
and
b
of type
Medals
as
follows:
a<b
if and only if:
Search WWH ::
Custom Search