home—info—lects—labs—exams—hws
D2L—tutor/PIs—zoom (snow day)
Object120 + its docs—java.lang docs—java.util docs
if-else, and helper functions
Train Schedules
Your assignment is to complete the
»Train Ticket« program,
with a few modified requirements.
Turn in a hard copy, and attach your TrainTicket.java
to the D2L, hw02-dropbox.
Due Sep.16 (Mon)18 (Wed) 10:00.
After reading over the instructions on that link above, our assignment will
be the same essence,
just converting I/O to test-cases
(and, specifying the exact names of functions you should have).
-
To do this, we'll have an overall function named fareMsg which:
-
will be called with three arguments:
the start-station number,
the end-station number,
and the hour (all ints).
-
fareMsg will return a string
in the following format (exactly):
"starting-station-name to ending-station-name at hour:00 costs $cost on the train-line line.".
-
For example:
- fareMsg(3,1,13) might return
"Movie Palace to Dark
Sside at 13:00 costs $1.60 on the 15675.987.1068 line."
- fareMsg(2,6,18) might return
"Soggy Creek to Grassy Square at 18:00 costs $5.20 on the 15675.987.1068 line.".
Of course, the exact station-names and rates will be different for you.
See the note below, about formatting numbers.
-
You must have a (helper) function called
fare
which will be called with the same three arguments as fareMsg;
it will return the cost of the trip, in dollars.
E.g. fare(3,1,13) might return 1.6,
while fare(2,6,18) might return 5.2.
-
You must have a (helper) function
stationName, which
takes in
a valid station-number,
and returns that station's name.
E.g. stationName(5) might return
"Misty Market"
-
You may certainly use other helper-functions, if you like.
My own solution happened to include a helper which took in to station-numbers and returned a boolean.
-
For each function above, you must also have a test-function.
(Write each of these before you write the corresponding function-being-tested.)
- testStationName must have two tests.
- testFare must test the following sorts of trips:
-
each of the two samples given on your sheet;
-
A trip just like some other test-case
but with swapped beginning/ending stations (hence it has the same fare);
-
at least one trip crossing the river;
-
at least one trip not crossing the river;
-
a trip only 1 stop long;
-
a trip 0 stops long.
(Note that one test might be able to satisfy several of the conditions above;
you might be able to get by with as few as five tests.
Myself, I had eight different tests before I thought I was reasonably covering all cases, though.)
- testFareMsg should test the same inputs that testFare does.
- Include a function testAll which calls the other three test-functions.
-
Purely optional (but it makes your life easier):
Use assertEquals for your test-cases.
(It was added to
Object120.java
on Sep.13 (Fri) noon).
Recommendation:
First write tests for stationName followed by stationName.
Only then, start writing fare's tests and code,
and only do fareMsg's tests and code last.
-
Use other helper functions as appropriate; also include tests for them.
-
You may assume a function's inputs are always valid numbers.
Make sure that each of your (non-test) functions
have a short @return … javadoc-comment
(that is, inside /** … */ immediately preceding your function).
-
Just to be clear:
We will not prompt the user for stations or the time.
Don't use any input (such as class Scanner),
and there should be no
printlns outside of a test-function.
-
Formatting numbers:
When including numbers in the fareMsg,
for full credit,
make sure
- the price has exactly two decimal places;
hint: String.format("%.3f",23.14159) returns "23.142"
-
the hour (right before the ":00", is exactly two-digits long, padded with a
leading-zero as needed.
hint: String.format("%02d",4) returns "04"
However, if your actual-results aren't formatted quite right, you'll still get nearly all the points.
But your desired-results definitely should meet this requirement.
Of course, your answers should be correct (at least to within $0.0000001 :-).
-
Look over Ms. Brenneman's
HW3gradingWorksheet.pdf,
but do not turn it in
(since we'll have a slightly different grading rubric, allocating points to helper-functions and unit tests).
-
Be sure to use the exact file and function-names given here;
part of this homework may be auto-graded, and having the wrong name will
mean the auto-grader gives you 0 for that section.
home—info—lects—labs—exams—hws
D2L—tutor/PIs—zoom (snow day)
Object120 + its docs—java.lang docs—java.util docs
 This page licensed CC-BY 4.0 Ian Barland Page last generated | Please mail any suggestions (incl. typos, broken links) to ibarland radford.edu |
 |