On this page:
1 Defining equality
1.1 Getting started
2 Alternative approaches:   bad and good
2.1 Bad Option - Incorrect alternative:
2.2 Good Option - A Correct alternative:
3 Designing a Game
4 Fish Feeding Frenzy
4.1 The javalib library
6.8

Lab 5: Equality and World Programming

Goals:

Practice designing equality methods for union data types, and an introduction to World programs in Java.

Related files:
  javalib.jar     tester.jar     Banking.zip     BlobWorldFun.java     BadSame.java  

The javalib library provides the support for the design of interactive games and creating images composed by combining geometric shapes as well as image files. See The Image Library for more information.

To use the library, download the javalib file above and add it to your project the same way you have added the tester library.

At the top of the .java file where the library is used, add the following import statements:

import tester.*; // The tester library import javalib.worldimages.*; // images, like RectangleImage or OverlayImages import javalib.funworld.*; // the abstract World class and the big-bang library import java.awt.Color; // general colors (as triples of red,green,blue values) // and predefined colors (Red, Green, Yellow, Blue, Black, White)
1 Defining equality
1.1 Getting started

For this part of the lab we will work with classes that define bank accounts: a checking account or a savings account. Download the Banking.zip file that represent this class hierarchy and start a new project. Run the project and read through the examples to make sure what is involved in defining each type of account. Make two more examples of each type of account and add the needed tests for the method amtAvailable in these classes.

Implementing the same method

Our object is to define a method that will determine whether a given Account is the same as this account. We may need such a method to find a desired account within some class that represents the bank and all accounts in it.

Of course, now that we have the abstract class it would be easy to compare just account number and the name on the account, but we want to make sure that all the customer’s data matches what we have on file exactly, including balances, interest rates, etc.

We will design the method same similar to the technique described in the lecture, called equality by safe casting. The relevant examples can be found in the lecture notes. You may want to look at the code there as you work through this problem.

2 Alternative approaches: bad and good
2.1 Bad Option - Incorrect alternative:

The method above can be incorrectly written with two features of the Java language: the instanceof operator and casting. In the Checking class this style method would look like the following:

// Is the given Account the same as this Checking? boolean same(Account that){
if(that instanceof Checking){
return this.sameChecking((Checking)that);
}
else {
return false;
}
}

However, this version introduces bugs!

The issue is that any class that later extends Checking, say a PremiumChecking class, will also be considered a Checking instance by the instanceof operator.

If we implement a similar same method in PremiumChecking:

// Is the given Account the same as this PremiumChecking? boolean same(Account that){
if(that instanceof PremiumChecking){
return this.samePremChecking((PremiumChecking)that);
}
else {
return false;
}
}

Now only a PremiumChecking object can be the same as the given instance of the PremiumChecking class.

But an instance of the Checking class can be the same as the given instance of the PremiumChecking class!

These kinds of bugs can cause serious problems. This issue is also illustrated in the example file BadSame.java. Add the file to your project and run the example to see where the program fails to provide correct answers.

2.2 Good Option - A Correct alternative:

In lecture we introduced another approach that works better. It requires us to add a new method to the abstract class for each subclass of Account, and relies on double dispatch to produce the correct answer. Revisit this problem and, following your notes from class, implement same using this technique. All your tests should still pass, but there should be no exceptions even mentioned in your code.

3 Designing a Game

Introduction

To see how a game is designed, start a new project named MyFirstGame. Add the file BlobWorldFun.java to the project’s src 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.}

4 Fish Feeding Frenzy

This week you’ll design and develop a simple version of the game Fishy!. In this game, you start off as a small fish in a pond of smaller and larger fish, and to survive you must eat the smaller fish while avoiding being eaten by the larger ones. Move your fish with the arrow keys. You win when you are larger than all the other fish in the pond.

To implement this game, you should:
  • Develop one or more classes to represent the player and the background fish.

  • Allow the player fish to move with the arrow keys

  • Correctly handle fish movement. Background fish swim onto the screen from one side, and leave on the other. The player fish loops around, so when it exits from one side it re-enters the screen on the other.

  • Determine when the player fish can eat another fish.

  • Determine when the player has been eaten by another fish.

  • End the game when the player is the largest fish in the pond.

  • Allow the player to grow based on how many fish it has eaten (and how big they are).

Be sure to test your game’s behavior thoroughly.

If you wish to embellish the game, you can add additional features:
  • Add inertia: Once you let go of an arrow key, the player fish should not stop immediately, but should drift along until it slows to a halt.

  • Add more inertia: The bigger the player gets, the harder it should become to accelerate the fish...and also to stop!

  • Keep score: eating bigger fish is worth more than eating several little fish.

  • Add size snacks: eating these can make the player’s size grow immediately.

  • Add speed snacks: eating these can give the player a speed boost.

Remember – you will be graded for your program design, not making a cool video game. So whatever you add, make sure it’s well-designed.

4.1 The javalib library

The javalib library provides the support for the design of interactive games and creating images composed by combining geometric shapes as well as image files. See The Image Library for more information.

To use the library, download the javalib file above and add it to your project the same way you have added the tester library.

At the top of the .java file where the library is used, add the following import statements:

import tester.*; // The tester library import javalib.worldimages.*; // images, like RectangleImage or OverlayImages import javalib.funworld.*; // the abstract World class and the big-bang library import java.awt.Color; // general colors (as triples of red,green,blue values) // and predefined colors (Red, Green, Yellow, Blue, Black, White)