Introduction |
Please print a copy of this assignment, read it carefully, and
highlight material you think will be useful to you while you
are working on the program or submitting it.
This programmmng assignment is a continuation of Program 2. It is designed to ensure that you know how to write programs that combine the standard control structures in Java: blocks, ifs, for loops, and break statements (to terminate for loops); and the other statements: declarations and expression statements. You will continue to write, test, and debug programs using iterative enhancement. Finally, work on writing the clearest and simpliest code possible, declaring the appropriate number of variables (to have their natural type, and be aggressive with final) in their appropriate block (smallest scope). You will write three programs in this assignment. As always, you can check the behavior of your programs against mine by downloading my executable zip file Program #3 Executables and unzipping it. See Program #1) for details on how to run these executables on both PCs and in Eclipses (PCs and Macs). Remember, you can run these programs, but not examine their source (Java) code. Copy the input/output form of the executable programs in the programs that you write: use exactly the same prompts and messages. For the Cannon and Rocket programs, there are executable versions for the kernel and each enhancement. For your information, I am listing below the number of lines in my solution programs. These programs are formated in the standard way. I am counting only lines with code (even if the only code on the line is a brace that closes a block); but I am not counting blank lines nor lines filled with comments. My "cannon" program is 34 lines; my "rocket" program is 47 lines; my "average" program is 47 lines. Your programs might be smaller, and they might be larger; but if your program starts going over 2-3 times the size of mine, you might want to rethink it (or come get some help). Please follow the instructions below for each program: finish each enhancement before continuing to the next one (including printing whatever messages it displays in the console, copied exactly). Feel free to use the infinite for loop and if/break statements to write all loops; when you are finished, you may simplify these loops (to general forwhile or do loops); or, if you feel comfortable with these more advanced loops, write them from the start. In each of these programs, please pay particularly close attention to the following style principles (discussed in the lecture on Coding Style).
To work on this assignment, create one Java project (call it Program3) and create three new Java classes in it (as you did for one class in Program #2). Each class will contain a program that you will write to solve one problem; name the classes Cannon, Rocket, and Average. Write, run, and debug each class/program as you did in Program #2. 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). |
Physics Simulation and Optimization |
You will write a program that determines the angle to fire a cannon
so that its projectile hits the center of the entrance to a cave.
First we will discuss the relevant laws of motion and learn how to write code
that simulates the flight of the projectile.
Then we will write code that searches for the correct angle to fire the
cannon, by repeated simulating the projectile for a range of angles.
This general coupling of a simulation and an optimization
(here, finding the optimal angle to shoot the cannon) is a powerful
problem solving technique.
Here is a a picture with some relevant information. Note that the trajectory of a cannon shell (with no air resistance) is a parabola.
To simulate the flight of a projectile from a cannon, we must know its
speed (s in feet/second) and angle (θ in radians); and
we must choose a time increment (dT in seconds) for our simulation.
At the start of the simulation, the x and y coordinates
of the projectile are both 0 (feet) and the simulation time
(t) is 0 (seconds).
For each time increment
Design, code, test, and debug this program using
iterative-enhancement, as 5 mini-projects.
Test each project (compare it to my executable) to ensure that it is correct
before proceeding to the next enhancement.
This is the same methodology that we will use for larger programs; so, it is
a good idea to practice this technique here, where the program is small,
even if you can write the entire program all at once.
Then write a loop that simulates the projectile using the information
above until its y coordinate goes negative, printing the time,
and x and y coordinates during each iteration.
This is a pretty big kernel, with lots of details.
Enter a few speeds and angles to test it (and compare it against
my executable).
Ensure that for each new θ the simulation starts the cannon
shell at the origin at time zero (use tracing).
|
Simulating the Flight of a Rocket |
Write a program that performs the following tasks.
th - cd v2 a = ------------- - g mWe recompute this formula for every time interval during the simulation. In it a, the acceleration for that time interval, is computed from
Design, code, test, and debug this program using iterative-enhancement, as 6 mini-projects. Test each project to ensure that it is correct before proceeding to the next enhancement. This is the same methodology that we will use for larger programs; so, it is a good idea to practice this technique here, where the program is small, even if you can write the entire program all at once.
Submit ONLY THE FINAL ENHANCEMENT of the program: the one meeting the complete specifications (note that the program you submit SHOULD NOT print any intermediate results unless the user explicitly requests tracing). Test your debugged/completed program for a thrust of 100 lbs, a time of 10 secs, and a mass of 1 lb and compare it to the results of my excutable; for full credit your results should match exactly (certainly to most of the many digits printed by a double). If you have any questions about my output, ask me about it. |
Average Upgrade |
Don't start this project with the new project folder.
Instead, download the Average
project folder, unzip it, and use the unzipped folder as a project
folder.
When compiled and run, it prompts the user for a file name and then processes exam scores in that file to compute the average of all the exams. The first value in the file is the maximum score possible for the exam, and almost all the remaining values are legal exam scores. The final value in the file must be -1: such a value is called a sentinel; it marks the end of data in the file; it is not a value to be processed when computing the average. The Average project folder also includes a few data files (all ending in .txt) that you can test this program on. When prompted for a file name, type one of these names (for this too work, the files need to be at the top level in the project folder). Don't worry about the file-reading part of this code, because it is correct and does not need to be changed: just concentrate on the overall looping structure of the program and add Java declarations and statements at appropriate locations to make the following changes. Again, make these changes one at a time, designing, coding, testing, and debugging each enhancement before proceeding to the next one. Before starting to write your program, run mine a few times to familiarize youself with it.
|