Introduction |
This programming assignment is designed to ensure that you know how to
write "small" programs that use pre-written classes (either in the Java
library, or by me).
The constructors and methods for these classes are are documented in Javadoc
(see the Javadoc of Sun's API and Javadoc of Course API
links off the course web index), which you are expected to read during
this assignment.
It also requires that you know the standard control structures in Java,
and will give you the opportunity to explore using the debugger on these
programs.
Finally, you'll practice writing, testing, and debugging programs using
iterative-enhancement.
You will write three programs in this assignment. As always, you can check the behavior of your programs against mine by downloading and running my Executables, to help you understand the specification of the problem and observe the programmer/user interaction that you are to implement. The cardiac program includes various data files. Write all your code in the main method of a class (the e program also asks you to write one static method. You will not need to write other methods or classes, nor code dealing with arrays (nor should you). Because this assignment is straightforward, concentrate on using good programming style. Pay particularly close attention to the style principles discussed in the Coding Style lecture.
Write each program in its own project folder: you should name them accordingly (e.g., e, dicewar and cardiac); when you are finished with each, zip it and submit it via Checkmate. Each pair should submit one project: either partner can submit it. Of course, both partners names should appear in the comments of each program. Carefully read the last two sections, on Extra Credit and Time Management. These important sections are relevant to all programming assignments, but appear only in this one. |
e: a mathematical constant |
Write a program that approximates e by computing the exact
rational (fraction) of N terms in its series.
Using the infinite series, e = 1 + 1/1! + 1/2! + 1/3! + ....
To compute this series to N terms we have
e (to N terms) = 1 + 1/1! + 1/2! + 1/3! + ... 1/N!.
Your program must first prompt the user for N.
It must also prompt the user to determine if the program's behavior is to be
traced.
The program then computes the numerator and denominator of the sum,
using objects constructed from the BigInteger class.
Ultimately the program prints the final approximation for numerator and
denominator for e, and if tracing is turned on, all the intermediate
numerators and denominators (such a facility is useful for debugging
purposes, showing the intermediate values in a computation rather than
just a final result).
Here is a sample interaction
Here is a plan for writing this program via iterative-enhancement.
First, write/test/debug a static method that computes factorial: its
prototype should be BigInteger factorial (int).
Second, prompt the user for N and whether to trace the code, and then
write a loop that declares/initializes (and prints, if tracing is on) the
numerator and denominator of each term in the series (1 and 1, 1 and 2,
1 and 6, 1 and 24, etc.)
Third, declare and initialize the numerator and denominator of the sum,
and update the sum during each iteration of the loop (printing each
new sum if tracing is on).
Note
Note I found BigInteger.ONE (check out this static final field in the BigInteger class in Javadoc) useful in a few places. |
Dice War |
Write a program that simulates playing games of dice war; the program should
also collect certain statistics while it is playing these games and report
them after the required number of games have been played.
In a game of dice war, each player starts out with his/her own dice (2,
6-sided) and some number (entered by the user) of chips.
Each player roles his/her dice.
If one player's pip sum is higher, that player gets a chip from the other
player.
Whenever one player has no chips left, the game is over (and that player
has lost; and the other player has won).
Your program must prompt the user for the number of games to play, and the number of chips with which the players start each game. It must also prompt the user to determine if the program's behavior is to be traced. The program then simulates that many games of dice war, using object constructed from the DiceEnsemble class: one pair of dice for each player. It keeps track of the number of times each player wins, the length (number of dice rolls) of the shortest and longest games, and the total number of dice rolls (over all the games). It also uses a Timer to keep track of how long (in clock-time) it takes to play all the games. Try to use the DiceEnsemble objects themselves to keep track of some of the required information, so you do not have to declare extra variables. Ultimately the program prints
Here is a plan for writing this program via iterative-enhancement. Write a kernal program to prompt the user with the number of starting chips and then play one game. Second, add tracing to that game. Third, prompt the user for the number of games to play, and then play that many games (verify that it is correct via the tracing). Fourth, add code for keeping all the statistics. Fifth, add code for timing the game. Finally, enhance your program to produce the exact output required. Explore the DiceEnsemble and Timer classes, and use them effectively to minimize the variables and code you must write. I found Integer.MAX_VALUE (check out this static final field in the Integer wrapper class in Javadoc) useful as an initialization for computing the smallest-length game. |
Implantable Cardiac Defibrillators |
Write a program that simulates the working of an
Implantable Cardiac Defibrillator (ICD).
An ICD is a small electronic device placed in the chest cavity of a patient
who is suffering from arrhythmias (heartbeat irregularities).
This device constantly monitors the electrical output of a beating heart:
if it detects a bradycardia (heart beating too slowly) it acts a pacemaker;
more importantly, if it detects a tachycardia (heart beating too fast to
pump blood effectively: in extreme cases this results in ventricular
fibrillation) at which point it acts as a defibrillator by supplying a
large shock to the heart in an attempt to restore a normal rhythm.
This shock is described by patients as feeling like a kick in the chest
-although many patients are unconscious by the time action is taken on
the detected arrhythmia.
If you are interested, you can read more detailed information on
ICDs.
The basic algorithm inside an ICD computes the zero-crossing count (ZCC) of the electical signals it samples while it is monitoring a heart. Each signal should be between the value of -100 and +100 inclusive (if not, the ICD is receiving faulty signals, and it should shut itself off). During an interval (typically lasting a few seconds), whenever the signal value goes from positive to negative or negative to positive, the ICD increments its ZCC. For the purposes of this assignment, we will treat 0 as a positive number. At the end of an interval, the ICD checks to see whether its ZCC is within a normal range: in a bradycardia the ZCC is too low (equals or falls below some threshold); in a tachycardia the ZCC is to high (equals or exceeds some threshold). If the ICD detects either of these conditions it takes the necessary action; then it resets the ZCC and samples the heart signals for another interval. Of course, real ICDs have evolved to exhibit much more sophisticated behaviors.
For example, if the ICD is using an interval length of 10, the
following table labels each sample, shows the signal value for that sample,
and the current ZCC.
This program will simulate the simple ICD algorithmg, allowing us to test it on various data files that represent samples taken of the actual electrical signals of a monitored heart. It should operate as follows
|
Boiler Plate Comment |
Start each of your files with a comment like this one.
////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////// // // Program : Craps Statistics // // Author : Richard E. Pattis // Computer Science Department // School of Information and Computer Science // University of California at Irvine // Irvine, CA 92617-3435 // e-mail: pattis@ics.uci.edu // // Maintainer : Author // // // Description: // // Craps prompts the user to enter the number of games to play. It then // plays (simulates) that many games of craps, keeping the win/loss // information. At the end, it displays these statistics. // // Craps is a dice game. The thrower loses if he/she immediately rolls a 2 // (snake eyes), 3, or 12 (box cars). The thrower wins if he/she immediately // rolls a 7 or 11. If the thrower does not immediately win or lose, the number // thrown becomes the "point". Afterwards, the thrower tries to make his/her // point by rolling that same value again (and winning) before rolling a 7 // (and losing). When trying to make his/her point, the thrower keeps rolling // if he/she rolls any number other than the point or 7. // // This version of Craps Statistics uses the DiceEnsemble and Timer classes // to create objects that reduce the complexity of the code while increasing // the information it computes. Note that because dice.roll() returns the // dice ensemble, we can write the single declaration // int roll = dice.roll().getPipSum(); // instead of having to write two statements (if dice.roll() was null) // dice.roll(); // int roll = dice.getPipSum(); // // Known Bugs : None // // Future Plans : None // // Program History: // 8/ 8/00: R. Pattis - Operational in C++ // 5/15/01: R. Pattis - Translated to Java // 5/16/01: R. Pattis - Changed identifiers to conform to Java style // ////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////// |
Extra Credit |
Programming assignments must be turned in on time: you can get partial credit
for a partially completed assignment, but it must be turned in on time; I
will accept no late homework unless you have an official excuse
(and it is best contact me as soon as the problem arises, not after the
due date).
In fact, there is another incentive to finish not only on time, but to
finish early.
In all programming assignments, if you turn in everything at least 24 hours before it is officialy due, you will receive 2 point of extra credit. If you turn it in 48 hours (or earlier), you will receive 4 points of extra credit. (There is no more extra credit for early turn-ins; I recommend NOT turning it in more than 48 hours early -specifically, wait until you receive your graded previous program before turning in a new one). This is equivalent to almost one full grade improvement on a 50 point programming assignment. In previous quarters that I have taught, almost 75% of the students completed their assignments early and received some amount of extra credit There are two main advantages to planning on finishing early. First, if you run into a major problem, you will have extra time to solve it before the actual due date: and even experienced programmers frequently run into such problems. Yes, this means you! Second, and more importantly, if you are racing to finish before a deadline, stress levels can go through the roof, and you become less interested in learning the material (and the whole purpose of these programming assignments is to learn the material) and more interested in just getting it finished. If you do not learn the material, you will be at a major disadvantage for all subsequent programming assignments and tests, because of the cumulative nature of the material in this course. Therefore, plan to finish every assignment by Tuesday or Wednesday evening. Programming assignments sometimes also include an extra credit section worth 1-2 points. These are designed for students who finish early and want to continue exploring programming within the context of the assignment. The points are to acknowledge, in a small way, their extra effort. The dice war problem has 1 point of extra credit. To earn it, at the bottom of the main comment, include a section labelled Extra Credit. In this section, estimate the average number of dice rolls for a game where each player starts out with 1,000,000 chips. Explain the details of how you arrived at your estimate. Do not discuss your estimate with anyone but the student you are pairing with.
|
Time Management |
One of the hardest parts of being in college is learning how to manage your
time.
Time management is especially important in programming courses (and in the real
world, when you are working on complicated projects with hard deadlines).
The difference between good and bad time management can have a profound impact
on how much you learn in this course, how well you perform in it, and how
much effort you actually need to expend to do well.
I will try to divide most programming assignments into a series of smaller tasks, each that can serve as a milestone; when solved in sequence, these tasks will complete the entire assignment. We will discuss such a divide and conquer method more formally, calling it iterative enhancement. Generally, it is best to spread out the work on a week-long assignment. Most assignemnts become available on Wednesday in lab; we will overview the assignment in lab and you will start working on one aspect of it then as well; assignments include executable files, so you can see how the program should behave (including its input and output -which you must match). We can discuss the assignments in lecture on Thursday, before the weekend. You should plan to complete at least half the programming assignment over the weekend, and then plan to finish the rest early in the week (by Tuesday or Wednesday, to get any extra credit). Some students look at an assignment and think that it is best done in one sitting. If you can do so, great; but, if you plan to work this way, do the one sitting over the weekend, not Thursday night! In this way, if you are wrong about the amount of time that it will take, you will still have adequate time to complete the assignment. By meeting these time goals, you will both maximize what you learn and minimize your anxiety and the time that it takes for you to do the learning. Remember that assignments must be turned in on time: you can get partial credit for a partially completed assignment, but it must be turned in on time; I will not accept any late homework unless you have an official excuse. Finally, if you find yourself falling behind, seek help immediately (from me, the CAs, or even other students in the course -when appropriate). When the real programs start, we will discuss what kind of help you can get legitimately, and what kind of help constitutes cheating. |