19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

126 Chapter 3 Selections<br />

y-axis<br />

(9, 9)<br />

y-axis<br />

(4, 5)<br />

(2, 2)<br />

(6, 4)<br />

(0, 0)<br />

x-axis<br />

(0, 0)<br />

x-axis<br />

(a)<br />

(b)<br />

FIGURE 3.9<br />

rectangle.<br />

(a) Points inside and outside of the circle. (b) Points inside and outside of the<br />

(Hint: A point is in the circle if its distance <strong>to</strong> (0, 0) is less than or equal <strong>to</strong> 10. The<br />

formula for <strong>com</strong>puting the distance is 2(x 2 - x 1 ) 2 + (y 2 - y 1 ) 2 . Test your program<br />

<strong>to</strong> cover all cases.) Two sample runs are shown below.<br />

Enter a point with two coordinates:<br />

Point (4.0, 5.0) is in the circle<br />

4 5<br />

Enter a point with two coordinates: 9 9<br />

Point (9.0, 9.0) is not in the circle<br />

**3.23 (Geometry: point in a rectangle?) Write a program that prompts the user <strong>to</strong> enter a<br />

point (x, y) and checks whether the point is within the rectangle centered at (0,<br />

0) with width 10 and height 5. For example, (2, 2) is inside the rectangle and (6,<br />

4) is outside the rectangle, as shown in Figure 3.9b. (Hint: A point is in the rectangle<br />

if its horizontal distance <strong>to</strong> (0, 0) is less than or equal <strong>to</strong> 10 / 2 and its vertical<br />

distance <strong>to</strong> (0, 0) is less than or equal <strong>to</strong> 5.0 / 2. Test your program <strong>to</strong> cover<br />

all cases.) Here are two sample runs.<br />

Enter a point with two coordinates: 2 2<br />

Point (2.0, 2.0) is in the rectangle<br />

Enter a point with two coordinates: 6 4<br />

Point (6.0, 4.0) is not in the rectangle<br />

**3.24 (Game: pick a card) Write a program that simulates picking a card from a deck of<br />

52 cards. Your program should display the rank (Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10,<br />

Jack, Queen, King) and suit (Clubs, Diamonds, Hearts, Spades) of the card<br />

Here is a sample run of the program:<br />

The card you picked is Jack of Hearts<br />

*3.25 (Geometry: intersecting point) Two points on line 1 are given as (x1, y1) and (x2,<br />

y2) and on line 2 as (x3, y3) and (x4, y4), as shown in Figure 3.10a–b.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!