<< Chapter < Page Chapter >> Page >
The module uses the Ballworld program to demonstrate key ideas in abstract classes, polymorphism, inheritance and other coding techniques. This module will focus on an inheritance-based architecture.

In this module we will explore many OOP concepts by examining the program "Ballworld". Download the code for Ballworld here ( Link Temporarily Disabled. Please contact the authors for the code.).

Uml class diagram of ballworld

Note the union design pattern w.r.t. ABall and its subclasses.
To run Ballworld, load the files into DrJava and right-click the BallControl file. Select " Run Document's Main Method " to run the program. From the command line, go to the uppermost Ballworld directory and compile all the files in both the directories (" javac ballworld/*.java " and " javac command/*.java ") and then run the program as such: " java ballworld.BallControl ". The Make Ball button creates an instance of whatever ABall class is typed into the text field on the left. The " Clear All " button clears any balls off of the screen.

In a nutshell, the way Ballworld works is akin to a flip-book animation: The BallControl class contains a Timer that "ticks" every 50 milliseconds. Every time the timer ticks, the panel in the BallGUI upon which the balls are to be drawn is requested to repaint itself. When the panel repaints, it also tells the Dispatcher to notify every ABall in the system to update itself. When an ABall updates, it updates any internal values it has (its "state") such as its color, radius, and/or velocity. It then moves its position by an amount corresponding to its velocity and draws ("paints") itself onto the panel. Since this is happening 20 times a second, the balls appear to be moving and/or changing and thus the animation is acheived. The ILambda interface3 enables the BallGUI to communicate in a generic, decoupled manner to the BallControl and the Randomizer class is a utility class that provides methods to produce various random values needed by the system. Much of the code in Ballworld is significantly more sophisticated than what has been covered in the course so far--it will be covered soon, don't worry!

Abstract classes

First, we will focus on the union design pattern between ABall , WanderBall , CurveBall and StraightBall . In a union design pattern, we see that the superclass represents an abstraction of the union of its subclasses. For instance, a fruit is an abstraction of specific concrete classes such as apple and pear. A fruit embodies the common characteristics of its subclasses even if it doesn't completely describe the exact nature of those characteristics. In a fruit, there is a seed. However, the notion of "fruit" doesn't specify exactly the number, size, color or shape of its seed(s). It only specifies that it does indeed have a seed. Likewise, a fruit has the behavior of ripening. Apples, oranges, and bananas all ripen differently and at different rates. The abstract fruit notion does not specify the specific nature of the ripening behavior, just simply that it does have that behavior. In such, we see that we can never have a fruit that is not a specific class of fruit, such as an orange or grape.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Principles of object-oriented programming. OpenStax CNX. May 10, 2013 Download for free at http://legacy.cnx.org/content/col10213/1.37
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Principles of object-oriented programming' conversation and receive update notifications?

Ask