|
6 | 6 | distance between two cities. Divide the polygon into two triangles and use the |
7 | 7 | formula in Programming Exercise 2.19 to compute the area of a triangle.) |
8 | 8 | */ |
| 9 | +publicclassExercises_04_03 { |
| 10 | +publicstaticvoidmain(String[]args){ |
| 11 | +System.out.println("Atlanta, Georgia 33.7489954, -84.3879824"); |
| 12 | +System.out.println("Orlando, Florida 28.538335,-81.379236"); |
| 13 | +System.out.println("Savannah, Georgia 32.080899, -81.091203"); |
| 14 | +System.out.println("Charlotte, North Carolina 35.227087, -80.843127"); |
| 15 | + |
| 16 | +finaldoubleRADIUS =6371.01;// Constant value |
| 17 | + |
| 18 | +doublexAtlantaGeorgia =33.7489954; |
| 19 | +doubleyAtlantaGeorgia = -84.3879824; |
| 20 | +doublexOrlandoFlorida =28.538335; |
| 21 | +doubleyOrlandoFlorida = -81.379236; |
| 22 | +doublexSavannahGeorgia =32.080899; |
| 23 | +doubleySavannahGeorgia = -81.091203; |
| 24 | +doublexCharlotteNorthCarolina =35.227087; |
| 25 | +doubleyCharlotteNorthCarolina = -80.843127; |
| 26 | + |
| 27 | +// Convert degrees to radians |
| 28 | +xAtlantaGeorgia =Math.toRadians(xAtlantaGeorgia); |
| 29 | +yAtlantaGeorgia =Math.toRadians(yAtlantaGeorgia); |
| 30 | +xOrlandoFlorida =Math.toRadians(xOrlandoFlorida); |
| 31 | +yOrlandoFlorida =Math.toRadians(yOrlandoFlorida); |
| 32 | +xSavannahGeorgia =Math.toRadians(xSavannahGeorgia); |
| 33 | +ySavannahGeorgia =Math.toRadians(ySavannahGeorgia); |
| 34 | +xCharlotteNorthCarolina =Math.toRadians(xCharlotteNorthCarolina); |
| 35 | +yCharlotteNorthCarolina =Math.toRadians(yCharlotteNorthCarolina); |
| 36 | + |
| 37 | +// Calculate its great circle distance Atlanta Georgia to Orlando Florida |
| 38 | +doubledistanceAtlantaGeorgiaToOrlandoFlorida = |
| 39 | +RADIUS *Math.acos(Math.sin(xAtlantaGeorgia) *Math.sin(xOrlandoFlorida) + |
| 40 | +Math.cos(xAtlantaGeorgia) *Math.cos(xOrlandoFlorida) *Math.cos(yAtlantaGeorgia -yOrlandoFlorida)); |
| 41 | +// Calculate its great circle distance Orlando Florida to Savannah Georgia |
| 42 | +doubledistanceOrlandoFloridaToSavannahGeorgia = |
| 43 | +RADIUS *Math.acos(Math.sin(xOrlandoFlorida) *Math.sin(xSavannahGeorgia) + |
| 44 | +Math.cos(xOrlandoFlorida) *Math.cos(xSavannahGeorgia) *Math.cos(yOrlandoFlorida -ySavannahGeorgia)); |
| 45 | + |
| 46 | +// Calculate its great circle distance Savannah Georgia to Charlotte North Carolina |
| 47 | +doubledistanceSavannahGeorgiaToCharlotteNorthCarolina = |
| 48 | +RADIUS *Math.acos(Math.sin(xSavannahGeorgia) *Math.sin(xCharlotteNorthCarolina) + |
| 49 | +Math.cos(xSavannahGeorgia) *Math.cos(xCharlotteNorthCarolina) *Math.cos(ySavannahGeorgia -yCharlotteNorthCarolina)); |
| 50 | + |
| 51 | +// Calculate its great circle distance Charlotte North Carolina to Atlanta Georgia |
| 52 | +doubledistanceCharlotteNorthCarolinaToAtlantaGeorgia = |
| 53 | +RADIUS *Math.acos(Math.sin(xCharlotteNorthCarolina) *Math.sin(xAtlantaGeorgia) + |
| 54 | +Math.cos(xCharlotteNorthCarolina) *Math.cos(xAtlantaGeorgia) *Math.cos(yCharlotteNorthCarolina -yAtlantaGeorgia)); |
| 55 | + |
| 56 | +// Calculate its great circle Atlanta Georgia to SavannahGeorgia |
| 57 | +doubledistanceAtlantaGeorgiaToSavannahGeorgia = |
| 58 | +RADIUS *Math.acos(Math.sin(xAtlantaGeorgia) *Math.sin(xSavannahGeorgia) + |
| 59 | +Math.cos(xAtlantaGeorgia) *Math.cos(xSavannahGeorgia) *Math.cos(yAtlantaGeorgia -ySavannahGeorgia)); |
| 60 | + |
| 61 | +doubleside1 = (distanceAtlantaGeorgiaToOrlandoFlorida +distanceOrlandoFloridaToSavannahGeorgia +distanceAtlantaGeorgiaToSavannahGeorgia) /2; |
| 62 | + |
| 63 | +doublearea1 =Math.sqrt (side1 * (side1 -distanceAtlantaGeorgiaToOrlandoFlorida) * (side1 -distanceOrlandoFloridaToSavannahGeorgia) * (side1 -distanceAtlantaGeorgiaToSavannahGeorgia)); |
| 64 | + |
| 65 | +doubleside2 = (distanceSavannahGeorgiaToCharlotteNorthCarolina +distanceCharlotteNorthCarolinaToAtlantaGeorgia +distanceAtlantaGeorgiaToSavannahGeorgia) /2; |
| 66 | + |
| 67 | +doublearea2 =Math.sqrt (side2 * (side2 -distanceSavannahGeorgiaToCharlotteNorthCarolina) * (side2 -distanceCharlotteNorthCarolinaToAtlantaGeorgia) * (side2 -distanceAtlantaGeorgiaToSavannahGeorgia)); |
| 68 | + |
| 69 | +doubletotalArea =area1 +area2; |
| 70 | + |
| 71 | +System.out.printf("The area is %10.2f square kilometers",totalArea); |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | +} |