# teaching machines

## CS 145: Lab 1 – Variables and Scanner

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:

• You complete a few checkpoints, which consist of short programming exercises.
• Questions are always fair game. Your instructor and teaching assistant are here to help.
• You work with a partner of your choosing, but this must be a different person each week. Talking through technical matters with other human beings is a key part of lab.
• Groups of three are not allowed except by permission from the instructor.
• You work at one computer only.
• One person types for the first exercise. The other for the second checkpoint. And so on.
• You must show your work to either your instructor or your teaching assistant to receive credit.
• Both you and your partner must be present during checkoff time to receive credit.
• Checkpoints are only checked during lab, not during office hours or over email.
• If you don’t finish your checkpoints during a lab meeting, you can complete your work before next lab and get checked off during the first 10 minutes of the subsequent lab. No credit is granted if the work is not already complete before lab starts.

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:



moveto 0, 0

n = 8

repeat n
move 2
yaw 360 / n
end

nsides = 5
dowel

var mupDiv = jQuery('#mup_dowel');
mupDiv.closest('pre').replaceWith(mupDiv);
document.getElementById('mup_form_dowel').submit();

moveto 0, 0

n = 8

repeat n
move 2
yaw 360 / n
end

nsides = 5
dowel



This program demonstrates two foundational ideas in computer science:

• variables, which give symbolic names for to the “actors” in our computation and which also allow us to tweak values in just one spot in our code
• loops, which make mind-numbing repetition more mind-delighting

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:

• 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.)
• Create a new Java project in your workspace and name it labs145.
• Right-click on the src directory and create a package named lab01.
• Right-click on lab01 and create a new class named Smile. Give it the following code:
package lab01;

public class Smile {
public static void main(String[] args) {
System.out.println(":)");
}
}

• Run Smile by clicking the green play button in the Eclipse toolbar. Confirm that you see output in the console.
• Close Eclipse.
• Open Eclipse again and make sure you it loads the workspace you created a moment ago. Do you see your project and package? Does the code in Countdown still run?

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.