Summer Session 2006: May 30, 2006 - June 30, 2006
Out on:
June 16, 2006
Due by:
June 19, 2006 before noon (hard deadline)
Collaboration:
None
Grading:
Documentation 10%, Style 10%, Functionality 80%
Assignment 7 is Assignment 5 in disguise. Well, not really "in disguise" I guess, it's Assignment 5 again, plain and simple. I am not even changing the text of the problems below! However, there are several things you can do now that were not allowed when Assignment 5 was first handed out:
break
to get out of a loop. Just make sure you know what you're
doing, it will show if you don't.
Note that these "new freedoms" don't really make things easier for you: Now you have to decide for yourself if a global, a return, a break, or a record is a good choice for a certain problem or not, we're not deciding for you anymore! And with that comes the chance to make new mistakes, mistakes that can cost points. Even for the radicals among you, I'd recommend being conservative: If you don't have a good justification for a global, a break, multiple returns, or a record, don't use it; if you have a good justification, put it into your code as a comment for us.
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.
In a certain one-dimensional world of length n,
a bunch of m alien
microbes
float about quite randomly.
At any given point in time, each microbe either stays put
or it takes a step to the left or the right.
Microbes stay put with a probabilty of p_put,
move left with a probability of p_left, and
move right with a probability of p_right.
Note that p_put + p_left + p_right = 1.0 so
each microbe must do one of these things
in every time step.
Microbes that "move left" at the left edge of the world
die, as do microbes that "move right" at the right edge
of the world; they "fall of the edge of the world" and
are never heard from again.
Your job is to write a Java program Microbes.java
that simulates the fate of these microbes in their world over
time.
The program should be menu-driven and allow the user to adjust
the various parameters of the simulation: Length of the world,
number of microbes in the world initially, the three probabilities
described above, the number of time steps to simulate, and
after how many steps to print statistics.
The program should start out with the following assumptions:
The length of the world is 256, the number of microbes is
4096, p_put is 0.5, p_left is 0.25,
and p_right is 0.25, there are 16384 time steps,
and we print statistics after every 1024 time steps.
Make sure to validate the input of the user appropriately; for example, the length of the world must be positive, the number of microbes must be positive, all three probabilities must add up to (about) 1.0, etc. Note that you can only check the latter right before you start a simulation, otherwise the user would always have to adjust all three parameters at the same time, that wouldn't be very friendly. The statistics you print should be as follows: The current time step, the total number of microbes remaining, the number of microbes that died left since the last statistics, the number of microbes that died right since the last statistics, and the number of microbes in each position of the world right now; the latter should be indicated as a "histogram" as the following examples show (for a world of length 3):
0: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX (4096) 1: (0) 2: (0)
This is the "state of the world" at time step 0 (yes, you should also print statistics before anything happens). There are 41 'X' characters because there are 4096 microbes in position 0 initially. One 'X' for every (part of) 100 microbes.
0: XXXXXXXXXXXXXXXXXXXX (1993) 1: XXXX (359) 2: XXX (299)
This is the "state of the world" at some later time step; as you can see the "total" population is smaller than initially, and some microbes have moved to other positions in the world.
0: (0) 1: (0) 2: (0)
Finally, if you ever reach a point where there are no microbes left as indicated above, you should stop the simulation and display final statistics. If you don't reach such a state, the simulation stops after the configured number of time steps, also with final statistics.
You should represent the "world" as an array of integers of
length n.
The value in each component of the array tells you how many
microbes are currently "living" at that particular position
of the world.
Initially all microbes live in cell 0 at the "left edge" of
the world.
Simulating a time step requires that you decide for each
microbe what it is going to do; look up the function
random() in the class Math to
figure that out.
Your simulation will process the array from left to right;
at each positon i there will be a number of
microbes (possibly none of course); for each microbe, you
need to figure out whether it stays put or moves left or
right; if there are x microbes at position
i you need x random numbers to
make these decisions.
It may be tempting to think that you can simply proceed
as follows: Generate a random number; if it is less than
p_put don't do anything; if it is greater
or equal to p_put but less than
p_put+p_left decrease the number of microbes
at position i and if there is a position
i-1 increment the number of microbes there;
similar procedure for moving right.
However, this does not work as you would
hope since now you may move certain microbes more
than once in a time step, which is not allowed.
Instead of modifying the current world, it is better to
keep creating "new worlds" for each time step.
You process the old world from left to right as above,
but you "copy" the microbes to the "new world" as your
random numbers indicate; after you're done, you can
"forget the old world" and make the "new world" the
"old world" for the next time step.
Write a program Freq.java that reads a sequence
of words from the user until input is exhausted.
Look up the methods hasNext() and next()
in the Scanner class to figure out how.
(Depending on your platform, you can end input either with the
combination CTRL-Z or the combination CTRL-D.)
After you are done reading, print out the list of words you
read together with a counter of how often each word occurred.
A "word" in this context is whatever the Scanner
methods above think it is, you don't have to make a decision,
it's "just a string" for you.
You need to maintain two arrays to make this work: An array with
all the words you've seen so far, and an array that tells you
how often you've seen a certain word. It's important that you
keep these arrays "synchronized" over time: Position i
in the frequency array should refer to the number of times you
have seen the word at position i in the word array.
Note that you will also somehow have to deal with the fact that arrays don't "grow" in Java. The bad option is to impose an arbitrary limit on the number of distinct words you can handle; if the user enters more distinct words than you can handle, you stop with an error message (but print the statistics so far!). The good option is to create new, bigger arrays if you run out of space, copy the old arrays over, and then make the new entry for the new word. The best option is to create those new arrays by doubling their size, that way you're doing the least amount of copying; but this is also the trickiest option to implement, so I would recommend going for "good" and having a finished program instead of going for "best" and not getting it done.
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... */