You will be provided with a partial implementation of a simple program, written in Java. Your task is to complete the program.
The research and development department of the HasBeen Toy Company has come up with a wonderful new idea: building blocks with transparent plastic windows on each of 4 sides, and filled with small flashlights, mirrors, and other optical components. The blocks themselves are just slightly translucent, and so appear to glow with the colors of whatever light strikes them from other blocks.
The company has asked you to write a small simulator to show what can be done with these blocks.
There are several kinds of blocks available:
Copy the files attached into your own working directory. These files implement an applet/application that simulates the Light Blocks toy. Most of the files are contained in a zip file. Unpack it with the command
unzip LightBeams.zip
If you compile and run this code, as is, and compare it to the solution attached, you will see that several components are missing. Specifically, the splitters and opaque blocks still need to be implemented.
Your task is to add these components, adding classes named as follows:
OpaqueBlock. An "extreme" filter, this block passes no light at all. EastSplitterBlock, NorthSplitterBlock, SouthSplitterBlock, WestSplitterBlock A Splitter block splits an incoming beam from one direction into two parts, each emerging at 90 degrees to the original. It can also reverse the process, combining two beams from those directions. Incoming beams from the 4th direction are blocked. There are actually 4 splitters, distinguished by the direction of the splittable beam. These are named with the direction of the incoming beam to be split. Thus, a NorthSplitterBlock will split a beam from the North into two identical beams (each half the intensity of the original) emerging from the West and East. Incoming beams from the West and East are added together and emerge at the North. Incoming Beams from the South are blocked.
Note that all of these classes will be subclasses of LightBlock. You will want to study that class's interface and the existing subclasses of LightBlock to get a feel for the protocol involved.
You will also need to modify LightBlockRegistration.java, a class that basically keeps a list of all the different kinds of blocks that have been implemented.
This program can be run in two ways. It can be run as an ordinary Java application. The main class is LightBeams.LightBeams. Alternatively, it can be run as an applet by packing it into a Jar file and launching it from a browser (which is how I have chosen to make my solution available to you). Debugging is usually easier for applications than for applets, so I recommend that you stick with that mode for your own compiled versions.
In the sample solution click on a block to change its state
Flashlights shine colored light to the right
Transparent blocks allow light to pass in any direction
Filters pass only a selected color. The opaque block is a special case of a filter - it passes no light at all.
Mirrors reflect the light at 90 degree angles.
Beam splitters will split an incoming beam into two identical (half-strength) beams emerging at right angles to original, or will combine two beams into one.