CS 201 - Computer Science I - Fall 2004
Lab 5 - if statements
Loyola College >
Department of Computer Science >
CS 201 >
Labs >
Lab 5
Due
Electronic Submission: Monday, October 25 at 11:59pm. Labs submitted one day late will be assessed a
20% penalty. Labs will not be accepted more than one day late.
Write-up and print out: Wednesday, October 25 at the beginning of class
Objectives
- to write methods that use conditional statements
- to use named constants
- to write test drivers
Introduction
Recall that the LibraryBook class from a previous example had
fields for title, author, borrower, and due date. This class has been
updated to include two more fields: call number (a String), and
replacement cost (a double). The constructors have been
updated to work with these new fields. We will also add several methods
that use these fields and the original fields. The new methods will use
conditionals (if statements).
Assignment
First change the name of LibraryBook.java to LibraryBook<LastName1><LastName2>.java and change the name of BookTest.java to BookTest<LastName1><LastName2>.java.
Then add the following methods to the LibraryBook<LastName1><LastName2> class.
(Some of these methods will crash when called on a book that is not checked
out; that is OK for now).
- isReference, which returns true is the book is
a reference book and false otherwise. Reference books are
distinguished from other books by the first letter of the call number, which
is 'R' for reference books.
You can use the charAt
String method to get the first character of the call number and
can then compare that to the named constant REFERENCE_PREFIX
(which is defined to be 'R') using the normal comparison operators.
- renew, which advances the due date by 7 days for reference
books and 21 days for all other books.
Use the named constants declared in LibraryBook
instead of 7 and 21.
(Note that this method is separate from the one that allows one to
specify the number of days to renew the book).
Use named constants where appropriate.
- calcOverdueFine, which returns the overdue fine for the
book. For books that are not due, the fine should be 0. For
reference books, the fine is 1 dollar per day. The fine for other books
is 25 cents per day. No fine should exceed the replacement cost of the book,
which is stored in the replacementCost field, except when the
replacement cost is 0, in which case there is no limit on the fine.
You can calculate the fine by
- multiplying the number of days until the book is due by 1.00 or 0.25 and saving the result in a variable;
- if the result is larger than the replacement cost, setting the variable to the replacement cost; and
- if the book is not overdue, setting the variable to zero; and finally
- returning the value stored in your variable.
Use named constants where appropriate.
Finish the test driver started in the BookTest<LastName1><LastName2> class.
Your test driver should test all the cases of the methods described above.
For example, to test isReference you would display the result
of calling isReference on a reference book and a non-reference
book. Part of your grade for this lab will depend on how completely you test
your class.
Individual Write-up
In this write-up, you should begin by drawing a flow-chart for each of the three methods that you wrote. Then using the flow-chart and the code, explain how your test driver completely tests all your code. You should indicate which code is run by which test cases and argue that you have executed all the code and checked all the cases necessary.
This write-up will be graded for quality of writing and use of technical terms. Sophistication of the write-up will also be accessed.
Grading
- Attendance in lab (5 pts)
- Code compiles and runs (10 pts)
- Code produces the expected output (10 pts)
- Use of Constants (5 pts)
- Completeness of Testing (10 pts)
- Program includes sufficient comments (10 pts)
- Write-up (50 pts)
Files
Submissions
Submit the source code for the LibraryBook<LastName1><LastName2> class with your
additions as well as the source code for the BookTest<LastName1><LastName2> class using the class submission page. Turn in a print out of your code and write-ups in class.