Creating a Program from Scratch Mini-Lab

Pam Cutter
Kalamazoo College

 


Starting from Scratch

The goal of today's Mini-Lab is to get some practice creating a program given only a specification of a problem. You should learn how to utilize BlueJ to help you solve problems you encounter outside of this class. The point is to see that you CAN write useful programs without having a lot of complicated code given to you!

 

Option 1 — Simulating the Motion of a Ball

Write a program to simulate the motion of a ball being throw up into the air. The program should display the position and velocity of the ball at regular time intervals over a certain period of time. Let XT denote the ball's position at time T, and VT denote the velocity of the ball at time T. Using the following difference equations for motion from physics, we can determine the position and velocity of the ball at particular times. These equations represent a discrete approximation of a continuous system, and smaller values of &Delta T will result in a more accurate simulation, at the expense of more simulation steps.

XT+&Delta T = XT + VT * &Delta T
VT+&Delta T = VT + A * &Delta T,
where A is acceleration due to gravity (-9.8 m/s2) and &Delta T is the change in time. X0 and V0 represent the initial position of the ball and the initial velocity, respectively, with which the ball is thrown. You should run your program several times, with different initial positions and initial velocities, as well as different time increments and durations.

Given an initial position of 0 m and an initial velocity of 10 m/s, a sample output may look something like:

Time: 0.0    Position: 0.0      Velocity: 10.0
Time: 0.2    Position: 2.0      Velocity: 8.04
Time: 0.4    Position: 3.608      Velocity: 6.08
Time: 0.6    Position: 4.824      Velocity: 4.12
Time: 0.8    Position: 5.648      Velocity: 2.16
Time: 1.0    Position: 6.08      Velocity: 0.20
Time: 1.2    Position: 6.12      Velocity: -1.76
Time: 1.4    Position: 5.768      Velocity: -3.72
Time: 1.6    Position: 5.024      Velocity: -5.68
Time: 1.8    Position: 3.888      Velocity: -7.64
Time: 2.0    Position: 2.36      Velocity: -9.60

 

Option 2 — Word Count

Write a program that counts the number of words in a line of text. You may want to refer to the documentation for the Java String class, which can be found at: http://java.sun.com/javase/6/docs/api/java/lang/String.html. There are several variations on how you might approach this problem, listing below in order of increasing complexity. As a reminder, if you are representing individual characters as the primitive type char, you should use the == and != operators. If you are representing them as the object type String, you should use the equals method.

Variation 1: Assume there is exactly one space character between each word. By examining each of the characters in the text, and counting spaces, you count the words. (Almost. Don't forget to count the last word!) For example, given the text "COMP150 is the best class I have ever taken!", the program would output 9.

Variation 2: Don't assume there is exactly one space between the words. When examining each of the characters, you must have some way to determine whether the character is at the beginning of a word, in the middle of a word, or a space.

Variation 3: Print a list of each of the words in the string along with how many times each word appears. For example, given the string "Dog. Big dog. Little dog. Big dogs and little dogs. Go, dogs. Go.", the program would output:

Dog     3
Big     2
Little     2
dogs     3
and     1
Go     2

 

Hand in any classes you developed for the program you wrote.