|
home—info—lects—labs—exams—hws
textbook—tutor/PIs—java.lang docs—java.util docs
Last time, you wrote a test-class and methods stubs for Bookmark. Today, fill in the method stubs so that your test cases all pass. (Here is yesterday's lecture on adding fields.)
Once you implement and check all the methods, here are some extra challenges to help you test your knowledge:
Write a method getCoolFactor, which returns: the Bookmark's rating doubled, plus the number of times it has been visited. (Be sure to add one to two test cases, for this, before you write the stub and the actual method.)
Add a field which counts how many times the Bookmark has been visited since the last time setRating was called (for that Bookmark). This requires adding state -- another field!
We'll add a cool feature, where a Bookmark automatically gets its rating increased if the page is visted often!
More precisely: Every four times a page is visited (without setRating being called), that Bookmark's rating will increase by one.
// Inside BookmarkTester: Bookmark seasons = new Bookmark( "http://archaeoastronomy.com" ); System.out.println( "Expected 3, got " + seasons.getRating() ); seasons.visit(); seasons.visit(); System.out.println( "Expected 3, got " + seasons.getRating() ); seasons.visit(); seasons.visit(); System.out.println( "Expected 4, got " + seasons.getRating() ); // Make sure that setRating will re-set the count: seasons.visit(); seasons.visit(); seasons.visit(); seasons.setRating(2); seasons.visit(); // This visit should *not* increase the rating. seasons.visit(); System.out.println( "Expected 2, got " + seasons.getRating() ); seasons.visit(); seasons.visit(); // This visit *should* increase the rating. System.out.println( "Expected 3, got " + seasons.getRating() ); |
You'll want to use the field you added in the previous step. You'll also need an if statement:
if (someCondition) { doSomething } |
void withdraw( double amt ) { this.balance = this.balance - amt; if (this.balance < 0) { this.balance = this.balance - 2.5; // Assess a penalty } } |
Finally, make sure the rating never goes above 5! You can use an if statement, or you can use Math.min: this.myField = Math.min( somePotentiallyTooLargeValue, 5 );
home—info—lects—labs—exams—hws
textbook—tutor/PIs—java.lang docs—java.util docs
©2009, Ian Barland, Radford University Last modified 2009.Feb.11 (Wed) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |