CS 124, Spring 2014
Lab 8: Objects
This lab concentrates on using objects. You will not write any classes this week; the objects that you use will be created as instances of classes that already exist.
Start by creating an Eclipse project named lab8. Add copies of the five files in /classes/cs124/lab8-files to the project. (Remember to add just the individual files, not the entire directory that contains them.)
Your work from this lab is due before the start of lab next Tuesday. Expect it to be collected from your homework folder at about 1:00 PM.
Exercise 1: A Small Program with BigInteger
The "3N+1" sequence was used as an example several times in the textbook. The file ThreeN.java is a simple implementation. You can run the program to see what it does, and you can review the definition of the 3N+1 sequence in Section 3.2.2.
One problem with this program is that the number N can get so large that it can no longer be represented as a 32-bit integer. When this happens, you get no warning, but the program is no longer computing the sequence correctly. In fact the output can end with a bogus negative value. This is a general problem when working with 32-bit integers. In mathematics, integers can be arbitrarily big. Why can't the computer use arbitrarily big integers?
In fact, it can (limited only by the amount of memory that is available). In Java, the standard class BigInteger represents integers that can be arbitrarily large. The first exercise of the lab is to modify the ThreeN program to use BigInteger instead of int, so that it will be able to work with integers of any size. The first step is to import the BigInteger class, which is found in the package java.math. Use this directive at the top of the source code:
BitIntegers are objects, not primitive values. That means that you have to create them with a constructor, using the new operator. The BigInteger constructor that you want to use takes a parameter of type String. The string should contain the number written out in ordinary, decimal notation. For example, to create BigIntegers to represent the integers 1 and 123456789012345678901234567890, you could say:
BigInteger one, reallyBig; one = new BigInteger("1"); reallyBig = new BigInteger("123456789012345678901234567890");
Furthermore, when using BigInteger, you can't do arithmetic with the operators +, -, *, /, and %. Instead, you have to use instance methods. For example, to add 1 to reallyBig, you could say
reallyBig = reallyBig.add(one);
or, if the variable one didn't already exist, you could say
reallyBig = reallyBig.add( new BigInteger("1") );
Note that reallyBig.add does not modify the object that realllyBig points to; it creates an entirely new BigInteger instance that must be assigned to reallyBig to change its value. The BigInteger instance methods subtract, multiply, divide, and mod work similarly (where mod is the equivalent of the % operator).
Some programming notes: To let the user input a BigInteger, you have to read a String from the user, and then apply the BigInteger constructor to the input string. Since BigIntegers are objects, they should not be compared with the == operator. Instead, use the equals method, just as you do for Strings. You might find an efficient way to get the value of N % 2 in the BigInteger API.
Exercise 2: An Even Smaller BigInteger Program
For fun, write a little program that computes and prints the value of 1000!. 1000!, which is read as "one thousand factorial," is obtained by starting with 1 and then multiplying it by 2, then by 3, then by 4, and so on up to 1000. That is, it is equal to 1×2×3×...×1000. The value will be very large and can only be represented as a BigInteger. The program needs less than 20 lines.
Exercise 3: Moving Balls, with Symmetry
For the third exercise, you will need the files SymBall.java, BallPen.java, and MovingBalls.java, The first two files define classes SymBall and BallPen that are used in the program. The third file, MovingBalls.java, defines the main program and is the only file that you will need to edit. You should run the program to see what it does.
The basic idea is that class SymBall defines "balls" (which are really just circles) that can bounce around inside "pens" that are defined by class BallPen. Furthermore, a SymBall can actually reflect itself to make a symmetric set of balls. The sample program applies a double horizontal/vertical reflection to all the balls, which produces a four-way symmetric pattern.
A BallPen is a rectangular area inside which balls move. The BallPen constructor specifies the (x,y) coordinate of the upper left corner of the rectangle and the width and height of the rectangle. The SymBall constructor specifies the pen in which the ball will bounce around. The BallPen class and the SymBall class each has its own small API consisting of instance methods that allow you to configure a pen and balls. In the MovingBalls program, the pen and balls are all created and configured in the method named createContents(). You can check out that method to see how its done.
Your job is to modify MovingBalls.java to make it do something original and hopefully attractive. Your version of the program should use at least three pens, each containing a number of balls. An example that uses three pens is shown at the right. You might prefer to use overlapping pens. You should study the BallPen and SymBall APIs to see what configuration options are available, and you should use a variety of configuration options in your program. You might consider trying rather large balls. You might consider using translucent colors. (Recall that the constructor new Color(r,g,b,a) can be used to create a translucent color.)