Java Reference
In-Depth Information
y -axis
y -axis
(9, 9)
(4, 5)
(6, 4)
(2, 2)
(0, 0)
x -axis
(0, 0)
x -axis
(a)
(b)
F IGURE 3.7
(a) Points inside and outside of the circle. (b) Points inside and outside of the
rectangle.
**3.23
( Geometry: point in a rectangle? ) Write a program that prompts the user to enter
a point (x, y) and checks whether the point is within the rectangle centered at
( 0 , 0 ) with width 10 and height 5 . For example, ( 2 , 2 ) is inside the rectangle and
( 6 , 4 ) is outside the rectangle, as shown in Figure 3.7b. ( Hint : A point is in the
rectangle if its horizontal distance to ( 0 , 0 ) is less than or equal to 10 / 2 and its
vertical distance to ( 0 , 0 ) is less than or equal to 5.0 / 2 . Test your program to
cover all cases.) Here are two sample runs.
Enter a point with two coordinates: 2 2
Point (2.0, 2.0) is in the rectangle
Enter a point with two coordinates: 6 4
Point (6.0, 4.0) is not in the rectangle
**3.24
( Game: pick a card ) Write a program that simulates picking a card from a deck
of 52 cards. Your program should display the rank ( Ace , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ,
Jack , Queen , King ) and suit ( Clubs , Diamonds , Hearts , Spades ) of the card.
Here is a sample run of the program:
The card you picked is Jack of Hearts
*3.25
( Geometry: intersecting point ) Two points on line 1 are given as ( x1 , y1 ) and ( x2 ,
y2 ) and on line 2 as ( x3 , y3 ) and ( x4 , y4 ), as shown in Figure 3.8a-b.
The intersecting point of the two lines can be found by solving the following
linear equation:
( y 1
-
y 2 ) x
-
( x 1
-
x 2 ) y
=
( y 1
-
y 2 ) x 1
-
( x 1
-
x 2 ) y 1
x 4 ) y 3
This linear equation can be solved using Cramer's rule (see Programming Exer-
cise 3.3). If the equation has no solutions, the two lines are parallel (Figure 3.8c).
( y 3
-
y 4 ) x
-
( x 3
-
x 4 ) y
=
( y 3
-
y 4 ) x 3
-
( x 3
-
 
Search WWH ::




Custom Search