Java Reference
In-Depth Information
■
Three
get
methods for
a
,
b
, and
c
.
■
A method named
getDiscriminant()
that returns the discriminant, which is
b
2
-
4
ac
.
■
The methods named
getRoot1()
and
getRoot2()
for returning two roots of
the equation
b
2
b
2
r
1
=
-
b
+ 2
-
4
ac
r
2
=
-
b
- 2
-
4
ac
and
2
a
2
a
These methods are useful only if the discriminant is nonnegative. Let these meth-
ods return
0
if the discriminant is negative.
Draw the UML diagram for the class and then implement the class. Write a test
program that prompts the user to enter values for
a
,
b
, and
c
and displays the result
based on the discriminant. If the discriminant is positive, display the two roots. If
the discriminant is 0, display the one root. Otherwise, display “The equation has
no roots.” See Programming Exercise 3.1 for sample runs.
*8.11
(
Algebra: linear equations
) Design a class named
LinearEquation
for a
system of linear equations:
2
*
2
2
*
2
ax
+
by
=
e
ed
-
bf
af
-
ec
f
x
=
bc
y
=
cx
+
dy
=
ad
-
ad
-
bc
The class contains:
Private data fields
a
,
b
,
c
,
d
,
e
, and
f
.
■
A constructor with the arguments for
a
,
b
,
c
,
d
,
e
, and
f
.
■
Six
get
methods for
a
,
b
,
c
,
d
,
e
, and
f
.
■
A method named
isSolvable()
that returns true if
ad
-
bc
is not 0.
■
Methods
getX()
and
getY()
that return the solution for the equation.
Draw the UML diagram for the class and then implement the class. Write a test
program that prompts the user to enter
a
,
b
,
c
,
d
,
e
, and
f
and displays the result.
If is 0, report that “The equation has no solution.” See Programming
Exercise 3.3 for sample runs.
■
ad
-
bc
**8.12
(
Geometry: intersection
) Suppose two line segments intersect. The two endpoints
for the first line segment are (
x1
,
y1
) and (
x2
,
y2
) and for the second line segment
are (
x3
,
y3
) and (
x4
,
y4
). Write a program that prompts the user to enter these four
endpoints and displays the intersecting point. (
Hint
: Use the
LinearEquation
class in Exercise 8.11.)
Enter the endpoints of the first line segment:
Enter the endpoints of the second line segment:
The intersecting point is: (1.0, 1.0)
2.0 2.0 0 0
0 2.0 2.0 0
**8.13
(
The
Location
class
) Design a class named
Location
for locating a maximal
value and its location in a two-dimensional array. The class contains public data
fields
row
,
column
, and
maxValue
that store the maximal value and its indices in
a two-dimensional array with
row
and
column
as
int
types and
maxValue
as a
double
type.
Write the following method that returns the location of the largest element in a
two-dimensional array:
public static
Location locateLargest(
double
[][] a)