The goal of this lab is to practice writing trivial methods. The logic is very simple in this lab; the point is to become familiar with the syntax of methods and how to document them.
As in previous labs, move into your cs235
directory.
Make a directory for this assignment, and change into it.
cd cs235 mkdir lab5 cd lab5
In this lab, I am giving you a partially-written file to start with. Copy it from the public directory for this class:
cp /homeemp/tvandrun/pub/235/StarshipDistance.java .
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.
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:
These methods can count as "trivial," so you can skip the second part (the description of the algorithm).
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.
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.
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.
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).
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)