600.107: Introduction to Programming

Summer Session 2006: May 30, 2006 - June 30, 2006

Assignment 2: Lost Control? Tokyo Drift!

Out on: June 5, 2006
Due by: June 7, 2006 before noon (hard deadline)
Collaboration: None
Grading: Style 10%, Functionality 90%

Overview

Assignment 2 consists of two slightly bigger programming problems, now involving do and for loops as well as the concepts from Assignment 1; note that you don't have to use those loops, but you can. If you want you can put assertions into your code as well, but you don't have to either. However, if you do put assertions, they must make sense! So don't just put them without knowing why, that way you'll lose points.

Also note that starting with this assignment we'll look at the style of your programming as well. If in doubt, you should always try to write the simplest, most consistent code you're able to come up with. And please start using "readable" names for your identifiers, somewhat like we did when I wrote Gravity.java in lecture.

Notes: If you have problems with any of these tasks, please contact the staff or everybody in class using the mailing lists! Do this early so you have a shot at finishing the assignment. Also, if you can't solve one problem, feel free to skip it and work on the problems you can solve. Handing in something is much better than not handing in anything.

Problem 0: Basic Statistics (40%)

Write a Java program Stats.java that reads a sequence of integers from the user. The sequence ends when a negative number is entered; that negative number is not part of the sequence. Compute and print out the following:

Make sure your output is readable: Print something like length = 12 instead of just 12 without any indication what 12 is! Make sure your program gives useful error messages as well: The specification above is intentionally not quite complete and part of the assignment is to figure out the error cases and handle them nicely.

Note: Computing the standard deviation is harder than the other parts of this problem. You are not allowed to use advanced features such as arrays or lists! It is possible to compute the standard deviation without them, just a little tricky. If you can't get that part done, rest assured that it's not worth more than the other parts, so you're not losing too many points.

Problem 1: Gravity + Thrust = Explosions (60%)

This problem asks you to attack the Gravity.java problem from Assignment 1 again, but from a different perspective. Note that you may have done parts of this new version of the problem already for the previous assignment. In that case you have less work now (but you had more work than necessary last time).

We will again consider some object, some kind of space ship in fact, being dropped from a certain height. However, instead of using the time-dependent formulas we used for the last assignment, this time around we're going to explicitly compute velocity and height in terms of incremental changes.

Check out the solution for the old problem that I hacked in lecture. Note that it doesn't compute a "new" velocity and a "new" distance from the values these variables had previously! It simply uses the variables as more readable "placeholders" for the expressions themselves.

For this version of the problem, you start at a height h chosen by the user with velocity v = 0 m/s at time t = 0 s. For t = 0.1 s, you compute the "new" velocity by adding "0.1 s" worth of acceleration a = 9.81 m/s/s to the previous velocity. Then you compute the "new" height by subtracting "0.1 s" worth of "new" velocity from the previous height. The simulation now computes changes in discrete time steps, which was not necessary before. The output format for the simulation is still the same as on the last assignment.

I highly recommend getting everything up to here done before trying the rest of this problem!

So far the new program would compute pretty much the same as the old one, so we're throwing in a twist: Thrust! Aside from entering a starting height, the user can also enter a point in time "tf" (in seconds) at which the space ship is supposed to fire its thrusters. We model thrust as another acceleration "at" which is also entered by the user. Neither of these quantities can be less than zero for obvious reasons.

The overall simulation now takes the following form: From t = 0 s until the thrusters fire, the space ship just falls under the force of gravity. Once we pass time "tf", the total acceleration on the space ship is gravity minus the acceleration "at" of our thrusters. Depending on the speed we had at "tf" and the power of our thrusters "at" there are several possible outcomes of the simulation:

As mentioned above, the output of your simulation should be in the same format as for the previous version of this problem. However, you should also indicate at what point the thrusters were fired (just print "thrusters fired" on a line by itself when it happens) and you should indicate the final outcome of the simulation once it's over (i.e. hovered, escaped, landed, crashed).

Just to be complete: Your program for this problem should be called Thrust.java and it reads the initial height, the time at which to fire thrusters, and the acceleration of those thrusters from the user. Until the the simulation is over, calculate the current velocity and the current height of the space ship in steps of 0.1 seconds. Print the results in tabular form: The columns are time, velocity, and height; there's one row for each 0.1 seconds, giving all three items of data. Also print a line to show when thrusters were fired and to indicate the outcome of the simulation.

We're getting pretty close to having a moon lander game, all that's missing now is fuel and graphics (and maybe sound? but that's not realistic, is it?).

Deliverables

Submit your solutions by email to the submission address listed on the course website. Please make sure that you answer written questions in the text of the email. Please make sure that you attach programming questions as separate somename.java files. Note that you'll get a confusing automatic reply that I will explain later.

Finally, please include your name and email address as well as a brief comment explaining how to use your programs at the top of each Java file. Here's a template you can clone:

/*
 * CS 107
 * Assignment x: Problem y
 *
 * Your Name Here
 * Your Email Here
 *
 * Description of your program and any comments you may have
 * for the grader go here...
 */
Updated: $Id: assignment-2.html 88 2006-06-05 21:24:01Z phf $ Validate: XHTML CSS
Copyright © 2006 Peter H. Fröhlich. All rights reserved.