teaching machines

CS 145 Lab 1 – Data and Operations

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:

• Work in pairs, even if you don’t want to. Groups may not be larger than 2.
• Work at one computer.
• One of you is partner A. The other partner B.
• Partner A runs the computer through the first checkpoint. Then B through the second.
• Talk to each other.

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. We cannot support your personal machine.
2. Do not use the C drive for anything. Non-system files are wiped from it weekly.
3. Create a new project in your workspace and name it cs145. You will never need to create a new project again for this class.
4. Right-click on the src directory and create a package named lab01.
5. Close Eclipse.
6. Open Eclipse and make sure you point it to your overall workspace. Do you see your project and package?

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 of the three color channels 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 100% – 70% = 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. Calculate and print the mixed color, with a statement for each of the three channels. Hit the green play button in the toolbar to run your code, and find the results in the console. Enter the values you see printed in your color picker. Is the blend what you expected?

Now, let’s make the code treat our data symbolically instead of using literal values. We’ll introduce variables to hold our values. Consider the following unrelated code snippet, which doesn’t use variables:

System.out.println(257 - 34);

Those literals offer the reader of our program very little insight into what our program is trying to do. We can give names to our data like this:

int hitPoints = 257;
int damage = 34;
System.out.println(hitPoints - damage);

Does that help communicate the meaning of our program? Variables are great for making our code more readable.

Do something similar with your color calculation. Store your six base color values in int variables. Store your mix percentage in a double variable.

Checkpoint 2

Person B types.

Create a class named Remainder and give it a main method. Create two int variables: one for a numerator and one for a denominator. Give them some values that will be easy to test with.

Create a third int to hold the quotient. Assign to it the number of times the denominator goes completely into the numerator.

Create a fourth int to hold the remainder. Assign to it the amount left over when dividing the numerator by the denominator. Use only the standard arithmetic operators: +, -, *, and /. Print this value.

This remainder calculation ends up being quite useful for an astounding number of tasks. Most languages even have an operator for it: int remainder = numerator % denominator. Add a second print statement that uses this operator. Do the two results match up for a variety of numerators and denominators?