teaching machines

CS 145 Lab 1 – Variables and I/O

September 8, 2016 by . Filed under cs145, fall 2016, labs.

Welcome to the lab section of CS 145!

Lab is a time intended for you to work on programming exercises in a low-stakes environment and with lots of help at your disposal. This is how it works:

Many of these rules were crafted out of failures from earlier offerings of this course. These are designed to maximize the number of passing grades at the end of the semester.

Please form your groups and one of you log in!

Checkpoint 1

Person A types.

Recall the Madeup models that we programmed together on the first day of class. For instance, here’s the polygon program that we wrote:

This program reflects two crucial ideas relevant to this exercise:

Your task is to reverse engineer one of the following two models and write a Madeup program to generate it. Use variables and loops where appropriate!

Madeup is available at http://madeup.xyz. The example above contains all the ideas you need to go on, but more extensive documentation is available.

The first is a chain link:

The second is a node from a Flexi Puzzle:

When you have completed your model, invite your instructor or teaching assistant over to offer feedback and make note of your accomplishment in the gradebook. If both are working with others, and you are confident that you’ve satisfactorily completed this checkpoint, feel free to move on to the next.

Checkpoint 2

Person B types.

We now leave Madeup and use the programming environment that we’ll spend far more time in this semester: Eclipse. Open it and follow these steps:

  1. 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, which is accessible all over campus and even from home. Do not put anything on the C: drive, which gets wiped frequently by LTS, our system administrators. You are free to place your workspace on a removable drive or simply use Eclipse on your personal machine, but you are responsible for properly managing and backing up your files. We cannot support your personal hardware.)
  2. Create a new Java project in your workspace and name it labs145.
  3. Right-click on the src directory and create a package named lab01.
  4. Close Eclipse.
  5. Open Eclipse again and make sure you it loads the workspace you created a moment ago. Do you see your project and package?

Now, let’s write a program that prompts the user for two colors and mixes them together. This sort of routine is used all the time when enlarging images. New colors must be created for pixels that didn’t exist before, and these colors are calculated by blending the colors of the original pixels.

  1. Right-click on the lab01 package and create a new class named ColorMixer.
  2. Add a main method inside between the curly braces—which look like {}.
  3. Declare and assign a Scanner variable. Import the Scanner class.
  4. Prompt the user for a first color and store the inputs from the Scanner in three ints, one for each of the color’s red, green, and blue intensities.
  5. Prompt the user for a second color and store the inputs from the Scanner in three ints.
  6. Prompt the user for a proportion or weight by which to mix the colors. Assume the user enters a number in [0, 1].
  7. Mix the reds, greens, and blues together using the proportion. For example, if the proportion is 0.1, then the mix is 10% of the first color and 90% of the second. If the red intensity of the first color is 50, and the red intensity of the second color is 100, then the mixed intensity is 95. Store the results of your mixing in three double variables.
  8. Print out the mixed intensities.

Test your work with this mixing widget.

If you finish early, feel free to leave or reverse engineer the second model.