teaching machines

CS 145: Lab 1 – Variables and Scanner

September 8, 2017 by . Filed under cs1, cs145, fall 2017, labs.

Welcome to the first lab 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. Read each of these out loud with your lab partner and check them off to acknowledge your understanding:

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.

Checkpoint 1

Person A types. Visit Madeup.

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 demonstrates two foundational ideas in computer science:

For your first checkpoint, you will apply these ideas to generate another model.

Either create a chain link:

Or create this funny symbol, which is used in the United Kingdom to mark a mechanic’s shop that can certify your vehicle:

Notice the rounded corners. These were automatically added by appending a named parameter to the dowel command:

dowel maxBend:1

So, use only a single yaw command to make each sharp bend, and then let the dowel command smooth it out.

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 your instructor and TA are working with others at the moment, and you are confident that you’ve satisfactorily completed this checkpoint, please move on to the next checkpoint and catch them when they become available.

Checkpoint 2

We now leave Madeup and switch to a language that we will use most of this semester: Java.

In this course, we ask you to use the Eclipse develop environment to write Java code. Eclipse is one of many possible tools we could use to write Java. Netbeans and IntelliJ IDEA are also quite popular. There is no single best tool. We choose Eclipse because it has a very accommodating license and is widely used in industry.

Open Eclipse and follow these steps:

Now that we’ve got a place to store our code, let’s write some!

Color Mixer

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 scaling images. New colors must be created for pixels that didn’t exist before, and these new 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 with Control-Shift-O or Command-Shift-O. That’s the letter O, not the number 0.
  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. Assume the color intensities are in the interval [0, 255].
  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. Compute the blend color as a weighted average of the input colors. Mix the reds, greens, and blues independently. If the proportion is 0.1, then the mix is 10% of the second color and 90% of the first. For example, If the red intensity of the first color is 50, and the red intensity of the second color is 100, then the red intensity of the blended color is 55. Store the results of your mixing in three double variables.
  8. Print out the mixed intensities and compare them against the little utility below. Your numbers may not match exactly, but they should be close.

Show your working solution to your instructor or teaching assistant. Once you are checked off, feel free to leave.