You will be provided with a partial implementation of a simple program, written in Java. Your task is to complete the program.

Light Blocks

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:

  • colored "flashlights"
  • transparent blocks that pass light straight through
  • color filters that pass only a selected color of light
  • two kinds of angled mirror blocks that reflect incoming light 90 degrees to one direction or the other beam splitters that split an incoming beam of light into two beams, emerging at plus and minus 90 degrees to the original, or that add two incoming beams together to form a single stronger beam Some of these blocks are already implemented in the simulator. Your task for this assignment is to implement the remaining ones.

Setup

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

Your Assignment

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.

Sample Solution

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.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.