Java Reference
In-Depth Information
*9.10
( Algebra: quadratic equations ) Design a class named QuadraticEquation for
a quadratic equation ax 2
+
+
=
bx
x
0. The class contains:
Private data fields a , b , and c that represent three coefficients.
A constructor for the arguments for a , b , and c .
Three getter 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
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.
2 a
*9.11
( Algebra: 2
*
2 linear equations ) Design a class named LinearEquation for a
2
*
2 system of linear equations:
ed
-
bf
af
-
ec
ax
+
by
=
e
x
=
y
=
cx
+
dy
=
f
ad
-
bc
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 getter 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 ad
-
bc is 0, report that “The equation has no solution.” See Programming
Exercise 3.3 for sample runs.
**9.12
( Geometry: intersecting point ) Suppose two line segments intersect. The two end-
points 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. As discussed in Program-
ming Exercise 3.25, the intersecting point can be found by solving a linear equa-
tion. Use the LinearEquation class in Programming Exercise 9.11 to solve this
equation. See Programming Exercise 3.25 for sample runs.
**9.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)
 
Search WWH ::




Custom Search