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.

222 Chapter 5 Methods<br />

Write a test program that prompts the user <strong>to</strong> enter a number and its width and displays<br />

a string returned by invoking format(number, width).<br />

*5.38 (Generate random characters) Use the methods in RandomCharacter in Listing<br />

5.10 <strong>to</strong> print 100 uppercase letters and then 100 single digits, printing ten per line.<br />

5.39 (Geometry: point position) <strong>Programming</strong> Exercise 3.32 shows how <strong>to</strong> test whether<br />

a point is on the left side of a directed line, on the right, or on the same line. Write<br />

the methods with the following headers:<br />

/** Return true if point (x2, y2) is on the left side of the<br />

* directed line from (x0, y0) <strong>to</strong> (x1, y1) */<br />

public static boolean leftOfTheLine(double x0, double y0,<br />

double x1, double y1, double x2, double y2)<br />

/** Return true if point (x2, y2) is on the same<br />

* line from (x0, y0) <strong>to</strong> (x1, y1) */<br />

public static boolean onTheSameLine(double x0, double y0,<br />

double x1, double y1, double x2, double y2)<br />

/** Return true if point (x2, y2) is on the<br />

* line segment from (x0, y0) <strong>to</strong> (x1, y1) */<br />

public static boolean onTheLineSegment(double x0, double y0,<br />

double x1, double y1, double x2, double y2)<br />

Write a program that prompts the user <strong>to</strong> enter the three points for p0, p1, and p2<br />

and displays whether p2 is on the left of the line from p0 <strong>to</strong> p1, right, the same<br />

line, or on the line segment. Here are some sample runs:<br />

Enter three points for p0, p1, and p2: 1 1 2 2 1.5 1.5<br />

(1.5, 1.5) is on the line segment from (1.0, 1.0) <strong>to</strong> (2.0, 2.0)<br />

Enter three points for p0, p1, and p2: 1 1 2 2 3 3<br />

(3.0, 3.0) is on the same line from (1.0, 1.0) <strong>to</strong> (2.0, 2.0)<br />

Enter three points for p0, p1, and p2: 1 1 2 2 1 1.5<br />

(1.0, 1.5) is on the left side of the line<br />

from (1.0, 1.0) <strong>to</strong> (2.0, 2.0)<br />

Enter three points for p0, p1, and p2: 1 1 2 2 1 -1<br />

(1.0, -1.0) is on the right side of the line<br />

from (1.0, 1.0) <strong>to</strong> (2.0, 2.0)

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

Saved successfully!

Ooh no, something went wrong!