600.107: Introduction to Programming

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

Assignment 1: Controlling Flow

Out on: June 2, 2006
Due by: June 5, 2006 before noon (hard deadline)
Collaboration: None

Overview

Assignment 1 consists of several small programming problems that involve simple instructions and expressions, including variables, loops, and conditionals.

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. Finally, please include a brief comment explaining how to use your programs at the top of each Java file.

Problem 0: Signs of Evenhanded Oddness (20%)

Write a Java program Oddness.java that reads an integer n from the user and produces the following output:

For example, if the user enters -12 the program should print "Negative! Even!" on a single line and stop. On the other hand, if the user enters 1 the program should print "Positive! Odd!" on a single line and stop. Finally, if the user enters 0... Well, that's up to you. Make a good choice! :-)

Problem 1: Countdown (20%)

Write a simple Java program called Countdown.java that counts down from 10 to 1 and finally prints "Liftoff!" thus simulating this all important phase in any serious NASA project. Your program can only contain two separate output instructions, you must produce the countdown with a loop and a variable!

Problem 2: The 3n+1 Problem (30%)

Write a Java program Unsolved.java that first reads a positive integer n > 0 from the user. Make sure to check whether the user entered a positive integer, otherwise print an error message and ask for another integer. Once you get a positive integer, iterate the following process:

Once n is equal to 1, print the final 1 as well. Then print out how many iterations (executions of the loop body) were necessary to get to this point and end the program.

Interesting note: It's an open problem (and has been for some time) whether this sequence eventually goes to 1 for every possible starting value of n. The problem is known as the Collatz Conjecture; see also this website.

Problem 3: The Gravity of Things (30%)

Consider some object being dropped from a certain height h. If we ignore various details, the object will start to fall at an ever quickening pace until it hits the ground.

If we assume we're dropping the object on Earth, we have to apply an acceleration a = 9.81 m/s/s. The object starts at a velocity v = 0 m/s. To figure out the velocity v at a certain point in time t, we can use the equation v = a * t. To figure out the distance d travelled so far at a certain point in time t, we can use the equation d = 0.5 * a * t * t.

Write a Java program Gravity.java that reads an initial height h > 0.0 from the user. Until the object hits the ground, calculate its current velocity as well as its current height 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.

Note that this problem is a first step toward implementing the classic arcade game of Moon Lander in Java. I hope it'll be fun. :-)

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.

Updated: $Id: assignment-1.html 54 2006-06-03 03:13:49Z phf $ Validate: XHTML CSS
Copyright © 2006 Peter H. Fröhlich. All rights reserved.