Java Reference
In-Depth Information
-
the number of gold medals of
a
is less than the number of gold medals
of
b
,or
-
the number of gold medals are identical but the number of silver
medals of
a
is less than the number of silver medals of
b
,or
-
the number of gold and silver medals are identical but the number of
bronze medals of
a
is less than the number of silver medals of
b
.
Exercise 5.4 (A class for polynomials)
Consider defining a class for storing polynomials of arbitrary degree as
follows:
Program 5.13
A class for polynomials
class
Polynomial
int
degree ;
long
[]
coefficient ;
...
}
Observe that a polynomial of degree
d
has
d
+ 1 coecients. Provide
a constructor
Polynomial(int deg)
for that class that initializes all
deg+1
coecients to zero. Then design and implement the following
static
functions:
-
A clone function
static Polynomial copy(Polynomial source);
-
A display procedure
static void display(Polynomial source);
-
An equality predicate:
static boolean equalTo(Polynomial P, Polynomial Q);
-
A function
static Polynomial derivative(Polynomial source);
that computes the derivative of a polynomial.
-
A function
static Polynomial add(Polynomial P, Polynomial Q);
that adds two polynomials. Similarly, a function that subtracts two
polynomials:
static Polynomial subtract(Polynomial P, Polynomial Q);
-
A function
static long evaluate(Polynomial source, long x);
that evaluates the polynomial at value
x
. What is the time complexity
of this function? Horner proposed a scheme for evaluating polynomials
using only
d
multiplications and
d
additions using the following
schema:
P
(
x
)=(
...
((
p
d
x
+
p
d−
1
)
x
+
p
d−
2
)
x
+
...
)
x
+
p
0
Search WWH ::
Custom Search