CS 124, Fall 2011
Lab 9: Objects

Lab 9 consists of two exercises in which you will work with objects. In the first exercise, you will write one method that is part of a GUI program. You will have to use several objects. In the second exercise, you will write a small class and a short program to test it. The test program will make and use of several objects.

This lab is due at the beginning of lab next Thursday. You should copy your work into your homework folder by that time.

To begin the lab, create a new project named lab9. Open the directory /classes/cs120/lab9-files, and copy the files from that directory into your lab9 src folder. The files MovingBall.java, BouncinngBalls.java, and BouncinngBallsPanel.java are for Exercise 1. TextIO.java will be used in Exercise 2. (Note: MovingBall.java is a slightly modified version of a class used in Exercise 7.4 in the textbook.)

Exercise 1: Bouncing Balls

For this exercise, you should work in the file BouncingBallsPanel.java That file does not contain a main routine; to run the program, you should run BouncingBalls.java. The program also uses the file MovingBall.java. When you complete the program, it will work like the following applet. You can click the large, black display area to add a moving ball to the display. Several properties of the balls can be set by using the controls at the bottom of the applet. The "Opaqueness" setting allows you to make translucent, see-through balls. The value of opaqueness should be in the range 0 to 255. A 255 gives a fully opaque color. Lower values give translucent color (or in the case of 0 a rather useless absolutely invisible, fully transparent color). Try setting both radius and opaqueness to 100 and adding several balls. It's also cool to add a whole bunch of large, almost transparent balls, with opacity 10. (It might work better to run the jar file, BouncingBalls.jar, which you will find in /classes/cs124. On a Mac, the applet can't handle translucency very well.)

The file BouncingBallsPanel.java is complete, except for the implementation of mouse clicks on the display. The program is set to call the method

                public void mousePressed(MouseEvent evt)

when the user clicks the panel. Currently, the method is empty. Your job is to fill in the definition of this method. You do not have to change any other part of the code. The method already has a comment. The implementation has to match the comment.

The implementation that you write will create an object of type MovingBall. It will set certain properties in that object. And it will add the object to a list that contains all the balls in the display. The MovingBall class has a constructor

                public MovingBall(double left, double right, double top, double bottom)

The parameters specify the limits of motion of the ball. It is constrained to stay within the rectangle  left <= x <= right  and  top <= y <= bottom. In this case, the ball should move around in the display. You want left to be 0, right to be display.getWidth(), top to be 0, and bottom to be display.getHeight(). This constructor creates a red ball with radius 5. It is fully opaque. It starts in the center of the rectangle. It has a random velocity.

The MovingBall class also contains several methods that can be used to change various properties of the ball. You will have to use these methods to configure the balls that you add to the display. You can discover them for yourself in MovingBall.java (or simply by using Content Assist in Eclipse).

Here are some other classes, objects, and methods that you will have to work with:

I suggest that you start by creating a ball with its default properties, and adding the ball to the list. That only takes two or three lines. Once you do that, you can run the program and make balls by clicking the display. Note that all the balls start in the center of the display. Once you have that, you can figure out to set all the properties that you need to set for the ball.

Exercise 2: Math Quiz Redux

For this exercise, you should write a small MathQuestion class. An object of type MathQuestion will represent one question that might occur on a simple math quiz. Remember the Math Quiz Project from Lab 3? You are not going to redo that project, but you will write a class that might be used to redo the project in a more object-oriented way. The type of questions represented by MathQuestion objects are the type that could have been asked in the math quiz project. That is, they have an operation and two integer operands.

You should start by making a new class, and naming it MathQuestion. Your MathQuestion class should have two instance methods. One returns a String that represents the question. That is, a program can ask the question simply by displaying the string that is returned by this method. The second method should return the correct answer for the question. The constructor in the class should specify the operation and the operands for the question.

Write a separate main program to test your class. The main program should create at least two objects of type MathQuestion. Once it has the objects, it should use the objects to present each question to the user, get the user's answer, and check the answer. You do not have to create random questions; you can just make up specific questions.

Ideally, you would have a subroutine in your main program with a parameter of type MathQuestion. The subroutine would present the question to the user, read the user's response, and check whether the answer is correct. However, do should not put that subroutine in the MathQuestion class. The MathQuestion class just represents questions. It does not ask them!

Note: Though MathQuestion is only a simple class, you should try to design it well. For example, you should pay attention to the use of private versus public, and you should think about when it might be appropriate for your code to throw an exception.

Extra Info: Jar Files and More About Applets

This section is not part of the lab, but you can do it if you want. It's good to know something about "jar" files.

A typical Java program might contain dozens, hundreds, or maybe even thousands of classes. Each class compiles into a .class file. You don't want to have to manage all those separate files. A program should be one file. A .jar file is a file that can contain many class files. It can also contain other resources used by a program, such as sounds and images. By packaging everything into a jar file, you can have the entire program in one file.

An executable jar file is a jar file that "knows" which of its classes contains the main() routine of a program. If MyProgram.jar is an executable jar file, then you can run the program on the command line with the command

java  -jar  MyProgram.jar

On most operating systems, it is possible to run an executable jar file simply by double-clicking the icon of the jar file. On Ubuntu Linux, unfortunately, this does not work so easily. But you can run a jar file in the GUI as follows: Right-click the file icon. In the pop-up menu, select "Open With / Other Application". In the dialog box that appears, click on the triangle next to "Use a custom command". This will reveal a text-input box. Type java -jar into that box, and click "Open." This should run the jar file. (Note: If you do exactly what I said, then from now on, you can run executable jar files just by double-clicking the icon. If you don't want to do that, you should have unchecked the box next to 'Remember this application for "Java archive" files.')

For example, you run the executable jar file BouncingBalls.jar, which you can find in /classes/cs124, in this way.

Of course, you will want to make your own executable jar files. To make an executable jar file from an Eclipse project: Use the "Export command in the "File" menu, or right-click in the Project Explorer and select "Export". This will make a dialog box appear. In the dialog box, open the "Java" section by clicking the small triangle next to "Java", if it is not already open. Click "Runnable Jar File", and then click "Next". On the next screen, choose a "Launch Configuration" from the pop-up menu at the top. This will just be the class that contains the main routine for the program; it's how you tell the jar file which program to run. Click "Browse" and select a destination and file name for the jar file; the name of the file should end with ".jar". Click "Finish". The jar file should be created, and you should be able to run it as described above.

(A jar file created in this way will include all the classes from the project that contains your main program. This might be more than you need. If you want to create a smaller jar file with only some of the classes, see Section 6.8.4 in the textbook.)

Jar files can also be used for applets. To post an applet on the web, you need to post all the classes that are used in the applet. It is convenient to put all the classes into a jar file. When you do this, the jar file has to be specified in the <applet> tag. (See Section 6.2.4.)

For example, suppose that you want to put an applet version of the bouncing balls program on the web. First, you need a simple applet class, containing the code

import javax.swing.JApplet;
public class BouncingBallsApplet extends JApplet {
   public void init() {
      setContentPane( new BouncingBallsPanel() );

Then create a jar file from your project, named (for example) BBalls.jar. On the web page where you want the applet to appear, use the tag

<p align=center><applet code="BouncingBallsApplet.class" archive="BBalls.jar"
                      width="650" height="550"></applet></p>

David Eck, for CPSC 225