Introduction to Programming (CPSC 124)
—Hobart & William Smith Colleges, Fall 2014
Thursday Lab #2
Home | Syllabus | Calendar | Class Notes | Labs and Projects | General Notes

Due by 2:59 pm on Friday, 09/12/2014)

# Overview

There are three goals for this project: to reinforce your new knowledge of the double and int data types, to gain some experience using the methods offered by the Math library, and to continue the practice of problem-solving through isolating and delaying concerns.

• A list of the available methods in the Math library is available from the "General Notes" section of our web site. It is a partial list, but it has everything you need to complete this assignment.

• A review of the major concepts needed for this lab—double and int data types

and the delayed-concerns technique— are in the "Class Notes" section (September 8 and 10)

1. How many miles is is from Geneva NY to Los Angeles? From Chicago to Winnepeg? From Perth, Australia to Hong Kong?

Your job is to write a program, GeoDistance.java, which can be used to answer these questions. The program is run with six arguments: the name of a city, two numbers, the name of another city, and another two numbers. The numbers are the latitude and longitude of each location. The program should print the straight-line distance between the two locations as traveled across the Earth, with a message that is gives the city names and the distance between them, in both miles and kilometers.

### Sample Interaction

```John-Lasseter:~ jlasseter\$ java GeoDistance Toronto 43.7  -79.4  Chicago 41.881944  -87.627778
It is 701.5722 kilometers (435.9367 miles) from Toronto to Chicago.

```

### How to solve this

First, a word about the arguments. You can get the latitude & longitude of any city by looking it up on Wikipedia. You'll see a "coordinates" link at the top right of the page. Click on this, and from the resulting page, you'll see the city's coordinates in several formats. Use the WGS84 format (top left of the Geohack page), specifically, the version that uses fractional values rather that degrees/minutes/seconds. Try looking up Toronto and Chicago, and match what you see against the example above.

Now for the program. Begin by getting the correct reading of input values done. Once you have this, set up a mostly-working "print" statement, which is as close as you can get it to the expected output format. What quantities can you compute with just the input? What quantities do you still need to figure out? Fill those in with appropriate variables, but don't worry about getting their values right just yet. Instead, just make sure you can compile and run the program in its "half-solution" state.

This is an example of isolating and delaying concerns.

Now for the messy parts. You need to know: the conversion between miles and kilometers, the radius of the globe you're computing the distance on (i.e. the Earth), and a formula for computing this distance (we're working with a globe, not straight-line distance!)

The most important component is to learn the general formula for computing the distance between two points on a sphere (the "great circle distance"): for points (plat,plon) and (qlat, qlon), the distance on a sphere with radius r is

(Don't over-think this. You're not being asked to come up with a formula! Your only job here is to write Java code that expresses the formula above.)

While the Earth is neither a true sphere, nor possessed of a perfectly uniform surface, let's ignore that for our present work: assume a perfect sphere with radius of 6378 kilometers.

Do some internet research to figure out how to convert kilometers to miles.

2. Once you've got your program working, prepare a report with with the following:

1. A transcript of the interactions you used to answer the three distance questions given at the beginning of this lab.
2. There are some city names that you can't run this program on. Name three of them, and include a transcript of your attempt to make your program run on these arguments. What goes wrong in each case? Based on this evidence, what do you think the rule is here for input of the city names? What would you need to know in order to handle these names?

# Turn In

• Your source code file, GeoDistance.java, should be saved in a folder called "lab2". This folder should be copied to your turn-in directory:

`/classes/cs124/<user_name>`

Make sure you copy it here and not just in your home directory!

• Paper printouts of both your source code files and your report must be turned in at the beginning of class.

• Please remember to reboot your computer when you leave the lab (or log out if you are in the Lansing lab).

John H. E. Lasseter