Introduction |
This programming assignment is designed to ensure that you know how to write
simple classes: both classes filled with static methods (referenced by the
class name) and "normal" classes (from which one can construct objects
storing state in instance variables and call their methods on these
objects).
Primarily, you will be writing methods (either static or not) and write a few
constructors and specify/use a few fields (mostly instance variables).
Of course, you will continue gaining experience with the standard control
structures in Java (blocks, ifs, loops/breaks) as well as the more basic
Java features (declarations and expression statements using arithmetic,
state-change, relational and logical operators).
To write a class using iterative enhancement, we use the following process.
You will write three classes in this assignment. In all cases I will provide driver programs that you will use to test your classes. As always, you can check the behavior of your programs against mine by downloading, unzipping, and then running the file Program #5 Executables, to help you understand the specification of the problem and observe the programmer/user interaction that you are to implement. See Program #1 for details on how to run these executables on both PCs and in Eclipse (PCs and Macs). Remember, you can run these programs, but not examine their source (Java) code. Note how packages are used in this assignment. In each package the driver program is complete, and you must write only one class that it drives: these classes are either empty or need to be revised. All other classes provided are complete. Please be careful when deciding whether variables are instance variables, parameter variables, or local variables, and using each of these. To start working on this assignment, download Program #5 Project Folder which contains the outlines of classes you should write for this assignment and the driver programs that you should use to test these classes . Write, run, and debug each class/program. When you finish each part, submit its .java file. Only one programmer of the pair should dropoff the programs: the same one for each part. It doesn't matter which of the pair submits, but that person should submit all the parts. Of course, each program should contain both student names (in the comment: the same one you cut, pasted, ane filled in at the top of each program in Program #1). |
Static Library |
Write the methods in a class named SUM (abbreviating Static Utility
Methods) that contains the four methods described below.
The Driver class prompts the user for information to use when calling
these methods and prints the results that they return (so you can more
easily debug them).
First, change the SUM class to be in a different package: one using your UCI user ID (something like edu.uci.pattis) and use this package name when you import it into the Driver class. To do this, change the name of the package in the SUM class, then let Eclipse fix the problem (click the red X and select the first option: Move SUM.java to package (your package name)). Please write the following methods to meet the specification stated. Throw no exceptions except for the ones mentioned below. As is normally the case, none of these methods do any input/output other than through parameters and returned values.
|
BigRational: Infinite Precision Rationals |
Write a class named BigRational by updating the class named
Rational.
This assignment is actually a bridge between using/writing classes, because
you are actually just translating the Rational class
into BigRational, which contains all same the methods, but with
translated instance variables and bodies.
In BigRational instead of using two int instance variables for
storing the numerator and denominator (as Rational
does), use two BigInteger instance variables, by importing and using
the java.math.BigInteger class.
In BigRational, these instance variables can store integers with any
number of digits (tens, hundreds, thousands, etc.)
Refer to the Javadoc of BigInteger throughout this assignment (you
might even want to print a copy for quicker reference).
What you must do is translate the Rational class so that instead of using ints it becomes the BigRational class and uses BigInteger. Operators applied to int variables must be translated into method calls on BigInteger variables. Most operators on ints are available as equivalent methods on BigIntegers. Use the BigRational.java file provided; mostly it is a copy of the Rational.java file); you must translate all the Rational constructors and methods. The project file also contains a Driver class, which compiles correctly when the the BigRational class compiles correctly. Here are some general hints for translation.
|
Ball: For Bouncing Ball Simulations |
Write a simple class named Ball, which we can use to simulate a
bouncing ball (there are two drivers for simulation: one textual
-in the Ball class- and one graphical).
We characterize a ball's state by five values; its
Fill in the fields (instance variables), constructor, and methods in the Ball class.
Then test your Ball class using the graphic driver. For the colors, use only positive int values 0 to 255; for the size use a reasonable-sized positive integer (e.g., 15 pixels); for the height, don't exceed 10. for the elasticity, generally use a double value 0 to 1.; note that even with a perfect bounce (elasticity = 1.), because of the way the equations are simplified (not exact in terms of bouncing) some ball(s) may eventually come to rest and some not; try an elasticity > 1. Actuallly, if you specify -1 for any value, a random number in the appropriate range will be use; the default values are -1 for everything except elasticity. Experiment by running the program with -1 and other values. Note that I have written all the correct header methods inBall, so this part compiles correctly. Ball is the the only class that you must change, by filling in the instance variables, and bodies of the constructors and methods. Try debugging your ball using the Construct One button first, then test it later with Construct Many. |