Assignment 5: Writing simple methods

The goal of this Assignment is to practice writing trivial methods. The logic is very simple in this assignment; the point is to become familiar with the syntax of methods and how to document them.

1. Setup

As in previous assignments, move into your cs241 directory. Make a directory for this assignment, and change into it.

> cd cs241
> mkdir assign5
> cd assign5

In this assignment, I am giving you a partially-written file to start with. Copy it from the public directory for this class:

> cp /homeemp/tvandrun/pub/241/StarshipDistance.java .

2. Specification

You are the programmer on an interstellar space ship, travelling at light speed. Your captain wants to know how far the ship is from our home planet; all you know is how many days you have been been travelling. You are to write a program that asks the user for a number of travel days and displays the distance from the home planet in kilometers.

The speed of light is 2.997925 x 10^8 meters per second. To take a number of days and compute a number of kilometers using a meters per second rate takes several conversions. We will compute each step using a separate method.

3. First step

Open the file you copied in Xemacs.

 > xemacs StarshipDistance.java

Note that is has a main method, and also some method stubs that are commented out. In general, our plan is:

First implement the conversion from days to hours. Find the method stub that has the signature

 daysToHours(int)

Move the start-of-comment marker down so that the method is no longer commented out. Then fill in the body so it works.

After you have written it, you must do two things: document and test. To document, remember the rules for documenting methods. Write a block comment (start with two stars, and with stars all along the left side) containing:

Test your method by changing the main method so that it only calls your method and prints the result. Try a few examples, and make sure it is working.

4. More conversion methods

Repeat the process in part 3 for hoursToMinutes and minutesToSeconds. Document them properly and test them individually so you know they are right.

Then do the same for daysToSeconds, but instead of calculating the conversion directly, have this method call the other conversion methods to get its result. Document and test. Now you know that entire sequence of methods is right.

5. The heart of the matter

Now go ahead and fill in the code for secondsToMeters, calculating distance by time times velocity. You can write big number literals using "e" (for "exponent") to mean "times 10 to the..." That is, you might make a variable like this:

 double speedOfLight = 2.997925e8

Document and test. Then repeat the process for one more conversion method, metersToKilometers.

Finally, you can use calls to daysToSeconds, secondsToMeters, and metersToKilometers to write daysToKilometers. Since you've tested all the components, you should have nothing to worry about for the correctness of this method.

To wrap up, make sure everything is documented well (including the block at the beginning of the program and including all local variable declarations). Also make sure the main method has been cleared out of testing code, and works now according to the specification.

6. Extra credit

This is an American vessel, and your captain doesn't know what a "kilometer" is. Write and use an extra conversion method, from kilometers to miles, so that the more familiar unit of measure is used (and use it in your main method).

7. Turn in

Create the script file as before (cat, rm, compile, and run). Run the program using 1 day, 5 days, 16 days, and 80 days. Print it out and hand in a hard copy.

 > a2ps -P sp (the name of the script file)

(This will print "two up", meaning two pages shown next to each other on one pice of paper. If you use a2ps on a Java file, it will format it nicely like in the handouts I've given in class. The command lpr works similarly except it does no formatting and doesn't print two up by default.)

Then turn in the hard copy in class on Wednesday or put it in my box by 5:00 pm.

DUE: Wednesday, Feb 9, at 5:00 PM.


Thomas VanDrunen
Last modified: Tue Feb 8 10:27:09 CST 2005