Java Reference
In-Depth Information
( x 2, y 2)
( x 2, y 2)
( x 2, y 2)
( x 3, y 3)
( x 3, y 3)
( x 3, y 3)
( x 4, y 4)
( x 1, y 1)
( x 1, y 1)
( x 4, y 4)
( x 1, y 1)
( x 4, y 4)
(a)
(b)
(c)
F IGURE 3.10
Two lines intersect in (a and b) and two lines are parallel in (c).
The intersecting point of the two lines can be found by solving the following lin-
ear equation:
( y 1 -
y 2 ) x
-
( x 1 -
x 2 ) y
=
( y 1 -
y 2 ) x 1 -
( x 1 -
x 2 ) y 1
( y 3 -
y 4 ) x
-
( x 3 -
x 4 ) y
=
( y 3 -
y 4 ) x 3 -
( x 3 -
x 4 ) y 3
This linear equation can be solved using Cramer's rule (see Exercise 3.3). If the
equation has no solutions, the two lines are parallel (Figure 3.10c). Write a pro-
gram that prompts the user to enter four points and displays the intersecting point.
Here are sample runs:
Enter x1, y1, x2, y2, x3, y3, x4, y4:
The intersecting point is at (2.88889, 1.1111)
2 2 5 -1.0 4.0 2.0 -1.0 -2.0
Enter x1, y1, x2, y2, x3, y3, x4, y4:
The two lines are parallel
2 2 7 6.0 4.0 2.0 -1.0 -2.0
3.26
( Use the && , || and ^ operators ) Write a program that prompts the user to enter an
integer and determines whether it is divisible by 5 and 6, whether it is divisible by
5 or 6, and whether it is divisible by 5 or 6, but not both. Here is a sample run of
this program:
Enter an integer:
Is 10 divisible by 5 and 6? false
Is 10 divisible by 5 or 6? true
Is 10 divisible by 5 or 6, but not both? true
10
**3.27
( Geometry: points in triangle? ) Suppose a right triangle is placed in a plane as
shown below. The right-angle point is placed at (0, 0), and the other two points are
placed at (200, 0), and (0, 100). Write a program that prompts the user to enter a
point with x- and y-coordinates and determines whether the point is inside the tri-
angle. Here are the sample runs:
(0, 100)
p 2
p 1
(0, 0)
(200, 0)
Search WWH ::




Custom Search