CS 630 - Computing Fundamentals I - Fall 2005
Homework 6
Loyola College >
Department of Computer Science >
CS 630 >
Homework >
Homework 6
Due
Monday, October 31st at the beginning of class
Problems
Exercise (p. 274-275)
5.1, 5.2, 5.3
4) Consider the following code segment:
if (i == j) {
System.out.println("A");
}
else if ((i%j) < 3) {
System.out.println("B");
}
else if (i < (j - 1)) {
System.out.println("C");
}
else {
System.out.println("D");
}
- If i is 9 and j is 4, what is the output?
- If i is 4 and j is 9, what is the output?
- If i is 5 and j is 6, what is the output?
- If i is 5 and j is 9, what is the output?
5) Suppose the following definitions are in effect.
boolean p = true;
boolean q = false;
boolean r = false;
String s = "b";
String t = "b";
String u = t;
int i = 10;
int j = 0;
Evaluate the following expressions.
- !p == q
- s != t
- t != u
- s == u
- s.equals(u)
- t.equals(u)
- r == (p && q)
- q && (p || r)
- q || (p && !r)
- p && !q && !r || (p == !q)
Programming Projects
Objectives
- to write methods that use conditional statements
- to use named constants
- to write test drivers
Introduction
The LibraryBook class represents the a library book with
fields for title(a String), author(a String),
borrower(a String), due date(a GregorianCalendar),
call number (a String), and
replacement cost (a double). The constructors and some methods
have been provided. I suggest browsing them because one or two will
be useful when you add the new methods. If a method does what you need it
to do, you should call that method rather then rewriting the code in the
method. These new methods will use
conditionals (if statements).
Assignment
You will add three methods to the LibraryBook class and then add code to
main to throughly test your methods
- 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 method in the String class 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.
(Some of these methods will crash when called on a book that is not checked
out; that is OK for now).
Finish the test driver started in the BookTest 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 will depend on how completely you test
your class.
Hint: You may draw a flow chart of your methods. To completely test them, you need to provide conditions that execute every path in the flow chart. Depending on how you write the methods, you may require different numbers of test cases.
Download Code
LibraryBook.java
BookTest.java