Lab 4
Goals: The goals of this lab is to learn how to design and use abstract classes, and how to design interactive games using the javalib game libraries.
1 Abstract Classes
The following class diagram represents a library system that records the books that have been borrowed. There are three kinds of books: regular books, reference books, and audio books.
Reference books can be taken out for two days, other kinds of books for two weeks. The overdue fees are 10 cents per day for reference books and regular books, but 20 cents per day for audio books.
For reference book there is no author, only the title.
The day when the book it taken out and the day due are counted as days since the library opened in 2001. So, possibly, an audio book taken out today would have been recorded as taken out on the day 4300 with due date on the day 4314.
+-------+ |
| IBook | |
+-------+ |
/ \ |
--- |
| |
--------------------------------------- |
| | | |
+---------------+ +---------------+ +---------------+ |
| Book | | RefBook | | AudioBook | |
+---------------+ +---------------+ +---------------+ |
| String title | | String title | | String title | |
| String author | | int dayTaken | | String author | |
| int dayTaken | +---------------+ | int dayTaken | |
+---------------+ +---------------+ |
Design the interfaces and classes that represent the library borrowing system.
Define the abstract class ABook and lift those fields that can be lifted to this class.
Design the method daysOverdue that consumes the number that represents today in the library date-recording system and produces the number of days this book is overdue. If the number is negative, the book can still be out for that many days.
Design the method isOverdue that produces a boolean value that informs us whetehr the book is overdue on the given day.
Design the method computeFine that computes the fine for this book, if the book is returned on the given day.
For all methods, think carefully whether they should be designed as concrete methods in the abstract class, abstract methods that are defined in the concrete classes, or concrete methods in the abstract class that are overridden in one of the concrete classes.
2 Designing a Game
Learn how to draw shapes using the javalib.funworld library.
Introduction
To see how a game is designed, start a new project named AppleOrchardGame.
Download the following three files:
The file AppleOrchard.zip that contains the source code for an alomost complete working game.
The file Images.zip that contains the images used to create the graphical display for this game.
The file javalib.jar our library that allows you to design the images and the game actions.
Add the three files source files in the AppleOrchard.zip file to your project.
Unzip the Images.zipfolder and add all files to your project. (Save all image files in the Eclipse project directory that contains the src and bin folders.)
Add the javalib.jar file to the project’s class path (as you have done with the tester.jar library.
You can run the project in two ways. To see the display of the images and to run the game, open the AppleOrchardRunner file and click on the small green Run button on the top. It displays the apple and the basket image in one Canvas, the image of the game scene in a second Canvas, and runs the game in a third window frame.
To run the tests, set up a Run Configuration for the class ExamplesAppleOrchard.
The program illustrates the use of the javalib.funworld library that allows you design an interactive graphics-based game controlled by timer events and key presses.}
Complete documentation of the library with samples is available at http://www.ccs.neu.edu/javalib/World/.
Apple Orchard Game
You are now ready to design your first game. The canvas has a picture of apple tree. An apple falls down (controlled by the tick events). The player moves the basket on the ground, trying to catch the falling apple. The game ends when the player has caught ten apples. (Optionally, you may show the amount of time the player took to catch the apples.)
Well, this is the plan. For now, only one apple is falling, there is no check whether the basket has caught an apple, no count of falling apples, no new apples fall down once the lone apple had hit the ground.
So, you have your work cut out for you.
Start by reading the existing code.
The class Apple defines one falling apple, and falls some distance on each tick.
The class Basket represents a basket that the player can control: move left or right using the arrow keys.
The class CartPt extends the Posn class defined in our library, by adding methods that produce a point moved by some given distance, and computes the distance between two points.
The class AppleOrchard represents the game with one apple and one basket.
The class ExamplesAppleOrchard contains the tests for (almost) all methods defined int these classes.
The class AppleOrchardRunner contains the method that will run the game, as well as several fields that produce a Canvas window that displays some of the images used in the game.
This class should not be included in the homework submissions, as there is no way that the homework server can open a new window when checking your work.
Make sure you understand what each method does in every class. Add tests for the class CartPt that are missing.
Run the game and run the tests again, and think of what is missing.
The method caughtApple in the class Basket is only a stub. It is a mehtod with the correct purpose statement and the correct method header, but it produces only a default answer —
an answer of the correct type, but with no intentions of being correct. This allows us to run the game and work on the design of other parts of the game, and fix it later. So, do so now. You may need a helper method in the Apple class that reports the apple’s location.
Now that we can check wehther we caught an apple, we can start counting the caught apples. Add a new field to the class AppleOrchard that counts the number of caught apples. Now, if an apple is caught, just pdate the counter, but do not end the game. Instead, adjust the method worldEnds to end the world when ten apples have been caught.
What should happen when an apple hits the ground? Well, we need more apples to fall, so modify the method fall in the class Apple, so that when the apple hits the ground, it is replaced by a new apple falling from above.
At first, place the apple on a fixed location at the top, so you can easily test your method.
Modify your method to place the apple at random horizontal location, leaving the vertical displacement the same. Use checkRange test method to test this method.
Finally, make the game more interesting by having a list of several apples falling all at once. Use the class AppleOrchardRunner to check that your apples are shown correctly and the whole world looks as expected. Use the class ExamplesAppleOrchard for defining all tests for methods that do not involve the display of images.