Lab 4
Goals: The goals of this lab is to learn how to design and use abstract classes, and how to design intteractive 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 MyFirstGame. Download the file BlobWorldFun.java, add it to the project. Download the image small-shark.png and add it to your project folder. Add the javalib.jar file to the project’s class path. Run the project (the tests are defined in the ExamplesBlobWorld class.
The program illustrates the use of the javalib.funworld library that allows you design an interactive graphics-based game controlled by timer events, key presses, and mouse clicks.}
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.)
Do as much as you can in the lab. Finish this game as a part of your protfolio problems.
Start a new project named AppleGame. Download the images AppleImages.zip, unzip the folder and add the images to your project. Add the javalib.jar in its classpath.
Define the classes
Apple that represents the falling apple
Basket that represents the falling basket
AppleGame that represents the Apple Orchard Game
ExamplesAppleGame that will contain examples of the game and tests for all methods.
The class AppleGame extends the World. The class should import the same libraries as the BlobWorld (javalib.funworld.*, javalib.worldimages.*, javalib.colors.*, javalib.worldcanvas.*)
In the class AppleGame design the background image that consists of some background color an the apple tree. (Make the size of your world at least 200 by 200.
In the class Apple design the method moveDown() that will produce a new apple, fallen down by some fixed amount (to be later invoked on each tick).
In the class Basket design the method moveOnKey(String ke) that produces a new basket moved left if ke.equals("left"), produces a new basket moved right, if ke.equals("right"), and produces the same basked for any other input argument.
Design the method onTheGround that determines whether the apple has reached the ground. (Note: You decide where this method should be defined.)
Design the method fall that produces an apple as it should appear on the next tick: either fallen further down, or, if it has reached the ground, it should be replaced by a new apple somewhere up on the tree.
Place the apple on a fixed location at first, 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.
Optionally, vary the vertical displacement for the new apples as well.
Add the fields that will record the number of apples that have been caught to the AppleGame class.
Design the method caughtApple in the class AppleGame that determines whether an apple has fallen into a basket.
You may need some helper methods here.
Design the method onTick() in the class AppleGame that produces a new AppleGame object with the apple fallen down. If the apple ended up caught in the basket, the counter of fallen apples is incremented and the apple is replaced in the same manner as in the fall method designed earlier.
Design the method onKeyEvent(String ke) that allows the player to control the location of the basket on the ground.
Design the method WorldEnd stopWhen() that produces a new instance of the class WorldEnd as follows:
The class WorldEnd has two fields, a boolean field that is true if the game is over, and is false otherwise, and a WorldImage field that represents the last image that should be displayed when the game ends. (This field is ignored until the boolean field signals the end of the game.)
So, if the player has caught ten apples, report true for the end of the game, and provide the final image to be displayed —
if possible with some score shown, or some message to the player indicating that the game is over. Add a test case that will start the game - and play the game.