These assignments will have you playing more with pointers and arrays since that is what we have been talking about. All of these assignments will also take command-line arguments instead of reading input from the user with scanf.
After each problem description I've given you a sample output file. You should produce something similar to my output.
As a side note, be sure to save the code from all the programs you do. It is quite possible that later in the semester other assignments will include things that overlap with them and your code will be reusable.
File Stats:
There is a utility in Linux called wc that stands for word count. You can run it on one or more files and it will tell you how many lines, words, and characters there are in the files. For this assignment option you will do the same thing, only I also want you to count how many time each letter occurs in each file (regardless of case). The files you should work on will be specified on the command line. Your program should read through each file and count up how many characters, words, and lines there are as well has how many times each letter occurs. You will print this information for each file followed by a grand total for all the files. Your program might be invoked as "scala wc.scala *.txt", which would go through all the txt files in the current directory.
You will consider any string that is between whitespaces to be a word. Check the provided output for details on what that should look like.
Mandelbrot Set:
This program will use a GUI and graphics to draw a standard fractal called the Mandelbrot set. This is a rather famous fractal produced by a simple equation. This equation is z_n+1=(z_n)^2+c. The trick is that z and c are complex numbers (they have both real and imaginary parts). Also, z_0=c. For every point c in the complex plane, you can run through this series and it will either run off toward infinity or it won't. If it does run off it is not part of the Mandelbrot set. If it stays bound, it is part of the Mandelbrot set. Obviously, you can't run the equation forever so the user will tell you the maximum number of times to try as a menu option. If the magnitude of z (distance from the origin in the complex plan) ever gets bigger than 2, you should stop the calculation and go to the next point because that one will just get larger and larger.
Your GUI should allow the user to set five values: maximum iterations, real minimum, real maximum, imaginary minimum, and imaginary maximum. You will make an image that is the size of the panel that is drawing the fractal. Then you will calculate how many iterations it takes to go outside of magnitude 2.0 for that point up to the maximum allowed. Set the color of the proper position in the image according to how many iterations it went. Good starting values are 150, -1.5, 0.5, -1, 1. As you zoom in you will need to make the maximum iteration count higher to continue to see details.
For extra credit, make it so that the user can zoom in on the set by clicking/dragging.
Graphics Problem:
This problem will have you writing part the beginning of a ray tracing program. There is a PDF document linked to below that has the math required for doing this. Your application needs to have a number of different options that users can get to through the menu.
The first option will have the user enter x, y, and z coordinates for three points. These define a plane as well as a triangle. For basic credit you only have to render it as an infinite plane, not a triangle. The second allows the user to enter a center point and radius for a sphere. The third and fourth options interact with files. The last option will go through and render a gray-scale image of the scene with the specified geometry. Make an image the size of your custom drawn panel and set each pixel to be a shade of gray based on the distance from the viewer. Use the s parameter from the document below as the distance.
There are two options for extra credit on this project. The first is to implement triangles properly so that you check the bounds of the triangle instead of doing the whole plane. The second is to allow the camera to be moved around. Remember to put a big comment at the top of your code letting me know that it includes the extra credit if you do it.
Biology/Chemistry Problem:
In this problem you will model diffusion of particles and how that can lead to crystal growth. You will write a GUI that has a custom drawn panel that draws an image. You can pick how large the image will be, but it should be at least a few hundred pixels in width and height.
The menus for this application should have at least the following options:
A source is just an x,y position in the image. For this "simulation", you will start a "particle" at one of the sources and let it drift around until it comes in contact with another particle. This might sound complex, but it isn't. Say you have one source at 5,5. You start your "particle" there just by keeping track of 5,5 in two varialbes. Then you pick a random number between 0 and 3 (util.Random.nextInt(4)) and depending on what that number is you try to offset your particle either up, down, left, or right. If we got a move to the right our particle would now be at 5,6. The procedure is repeated until the move would put the particle in the location of another particle. At the beginning the only particles out there are seeds which you have placed. If a particle tries to move onto something, it attaches there and is drawn on the image. If a particle ever tries to move out of the raster bounds, simply don't move it and pick another random number.
The clear image option fills the image with black.
The clear sources option gets rid of all current sources.
When the user selects "Add Source" you should make it so the next click on the image adds a source at the location of the click.
When they select "Add Seed" you should make it so the next click puts a non-black pixel on the image at the click location.
When they do "Free Particles", you will ask them how many particles to free. Then you will repeat the following procedure that many times. First pick a source at random from the sources you have. If they haven't made a source yet, give them an error message and go back to the menu. Then start a particle at the source location and let it do a random walk as described above until it attaches to a non-black point in the image.
If the user selects save sources or load sources you should interact with a file to either save off their current list of sources or load in a file with sources.
For extra credit you can have your new particles color coded by the point from which they were released. This will allow you to visualize where the particles end up. You could also color code them by "when" they were released so the color coding changes as you move away from the seed.
Tron Light Cycles:
For this option you will implement the classic Tron Lightcycles game as first depicted in the original movie (Light Cycles). Your version will be much simpler that what is seen in the movie or many other implementation. In your game each "cycle" will be a single pixel that draws a line behind it when it moves. You will have two users with different colored lines that are controlled with two keys each. One key will turn the cycle 90 degrees left while the other turns it 90 degrees right. If either one hits the edge of the screen or a line left by itself or another cycle it dies and the other player wins.
For extra credit, make a computer AI that doesn't completely suck so that one or more humans can play against one or more computers.
To do the animation you will need to have a javax.swing.Timer. The following code will allow you to make one of these, then start it.
val timer=new javax.swing.Timer(100,Swing.ActionListener {
// Stuff you want to have happen regularly.
})
// Other code
timer.start()