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.

216 Chapter 5 Methods<br />

5.18 (Use the Math.sqrt method ) Write a program that prints the following table<br />

using the sqrt method in the Math class.<br />

Number<br />

SquareRoot<br />

0 0.0000<br />

2 1.4142<br />

...<br />

18 4.2426<br />

20 4.4721<br />

*5.19 (The MyTriangle class) Create a class named MyTriangle that contains the<br />

following two methods:<br />

/** Return true if the sum of any two sides is<br />

* greater than the third side. */<br />

public static boolean isValid(<br />

double side1, double side2, double side3)<br />

/** Return the area of the triangle. */<br />

public static double area(<br />

double side1, double side2, double side3)<br />

Write a test program that reads three sides for a triangle and <strong>com</strong>putes the area if<br />

the input is valid. Otherwise, it displays that the input is invalid. The formula for<br />

<strong>com</strong>puting the area of a triangle is given in <strong>Programming</strong> Exercise 2.15.<br />

5.20 (Use trigonometric methods) Print the following table <strong>to</strong> display the sin value and<br />

cos value of degrees from 0 <strong>to</strong> 360 with increments of 10 degrees. Round the<br />

value <strong>to</strong> keep four digits after the decimal point.<br />

Degree Sin Cos<br />

0 0.0000 1.0000<br />

10 0.1736 0.9848<br />

...<br />

350 -0.1736 0.9848<br />

360 0.0000 1.0000<br />

*5.21 (Geometry: great circle distance) The great circle distance is the distance between<br />

two points on the surface of a sphere. Let (x1, y1) and (x2, y2) be the geographical<br />

latitude and longitude of two points. The great circle distance between the two<br />

points can be <strong>com</strong>puted using the following formula:<br />

d = radius * arccos(sin(x 1 ) * sin(x 2 ) + cos(x 1 ) * cos(x 2 ) * cos(y 1 - y 2 ))<br />

Write a program that prompts the user <strong>to</strong> enter the latitude and longitude of two<br />

points on the earth in degrees and displays its great circle distance. The average<br />

earth radius is 6,371.01 km. Note that you need <strong>to</strong> convert the degrees in<strong>to</strong> radians<br />

using the Math.<strong>to</strong>Radians method since the <strong>Java</strong> trigonometric methods use<br />

radians. The latitude and longitude degrees in the formula are for North and West.<br />

Use negative <strong>to</strong> indicate South and East degrees. Here is a sample run:<br />

Enter point 1 (latitude and longitude) in degrees:<br />

39.55 -116.25<br />

Enter point 2 (latitude and longitude) in degrees:<br />

41.5 87.37<br />

The distance between the two points is 10691.79183231593 km

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

Saved successfully!

Ooh no, something went wrong!