teaching machines

CS 145 Lab 1 – Data and Operations

September 6, 2014 by . Filed under cs145, fall 2014, labs.

Welcome to CS 145, a class where you learn to teach machines. You won’t just use them. You’re going to become a developer.

The lab portion of this class is a time for us to throw some problems at you and for you to clarify your understanding, collaborate with others, get things wrong, and ask lots of questions.

Your work for each checkpoint section is inspected by your instructor or teaching assistant. Checkpoints are only inspected during lab—not office hours or any other time. If you do not get them inspected before the end of lab, you may do so in the first 15 minutes of the next lab.

Setup

First:

Second:

  1. Open Eclipse. You’ll be prompted to choose a workspace, the place where all your code is stored. Enter H:\workspace. The H drive is your university drive, so it’s likely to be accessible elsewhere. You are free to use a removable drive or your personal machine, but you are responsible for properly managing and backing up your files.
  2. Do not use the C drive for anything. Non-system files are wiped from it weekly.
  3. Either create a new project or import your Bitbucket repository into your workspace. There may be some version troubles if you use different versions of Eclipse with one workspace.
  4. Create a package named lab01.

Checkpoint 1

Person A types.

Devise an algorithm to mix colors together. Colors are commonly represented in software as combinations of red, green, and blue light. The intensity of each light is a quantity in [0, 255]. Open up a color picker in an image editor or online, and pick two colors that you’d like to mix together.

Ignoring the computer for a second, how would you calculate the intensities of the blended color by hand? Suppose you want 70% of color A to be mixed with 30% of color B. Once you have an algorithm on paper, then go to the computer and translate it into Java.

Write your code in the main method of a class named Blend. Implement your algorithm such that the base colors and level of mixing can be easily changed.

Print the blended color intensities and find it in your color picker. Is the blend what you expected?

Call upon your instructor or teaching assistant when you are ready to show your work.

Checkpoint 2

Person B types.

Back in the 1700s, a scientist named Lambert modeled the scattering of light across a highly-faceted surface. The premise of the model can be stated in few words: the proportion of light that a surface receives from a single light source depends only on how much it faces the light source.

When a surface is fully facing the light source, what proportion of the light should be received at the surface?

Now, turn the surface away a little bit. How should that proportion change?

Keep turning. When does the surface no longer receive light?

The Lambertian model can be formally described as a function of the angle between two vectors: the surface’s normal (a vector that is perpendicular to the surface and describes its orientation in space) and a vector to the light source. When the surface faces the light source, the angle between the vectors is 0, and 100% of the light reaches the surface. When the angle hits 90 degrees and beyond, the surface points away from the light, and 0% of the light reaches the surface. Test this out with a book and some pens and pencils.

Between 0 and 90, the proportion of light diminishes with the cosine of the angle. The entire function is shown below, with the angle in [0, 180]:

diffuse_plot

Write in class Lambert a main method that computes and prints the proportion of light received for an angle entered by the user, i.e., it computes the function shown above. You can get user input with the following code snippet:

Scanner in = new Scanner(System.in);
System.out.print("Enter angle in degrees: ");
int degrees = in.nextInt();

To make Scanner accessible, you will need to import it. You can do so in Eclipse by hitting Control-Shift-O. That’s O as in Oprah.

Three methods of the Math class are extremely helpful here: toRadians, cos, and max.

Don’t forget: solve the problem in small steps and test often.