teaching machines

CS 145 Lab 1

Welcome

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

Unlike the natural and social sciences, computer science and programming are not topics addressed in many of our schools. Let’s take a moment to describe what we computer scientists do with each piece of software we write:

1. Get a big idea that we want the computer to execute.
2. Break down the idea into a well-defined process.
3. Communicate the process to the computer using a programming language.

Only step 3 really requires a computer. In fact, steps 1 and 2 are often best done away from the computer.

Syllabus

Read the syllabus. Your instructor is a crotchety old man about a few things, so make sure you know how to keep him pacified.

You’ll notice that there are a few things missing from the syllabus. That’s because some class policies will be determined by a steering committee comprised of your instructor and some students. If you are interested in participating in this steering committee, which meets 15 minutes each week, please let me know what times work for you. Enter your UWEC username instead of your name.

Lab Expectations

Each week we’ll meet in lab. Both your instructor and a TA will be on-hand. Labs contain individual or group programming exercises followed by checkpoints. At these checkpoints, you’ll need to get your solutions approved by your instructor to receive full-credit for the lab. In the event that you do not finish the exercises, you can finish it on your own and show your working solutions to your instructor in the first 20 minutes of the next lab.

Checkpoints will only be approved in our lab meetings, not in office hours.

Survey

Your instructor would like to know a bit about your background and gather some data for in-class exercises. Please take a moment to complete this short survey.

Discussion board

This is a big class. Please use the class discussion board for your non-personal questions. In fact, go ahead and make a post to the discussion board right now.

Where’s the discussion board? There’s a link to it in the syllabus.

Scratch

Let’s get a first test of a programming language. Find Scratch on your lab computer. It’s tucked under Applications, Department, Computer Science. Scratch is a visual programming language that lets you program animations of sprites (2-D images).

See if you can figure out how it works. I suggest you look thoroughly through language’s vocabulary to see what you are capable of doing. Accomplish at least two of the following tasks. Use a separate sprite for each one; the bottom right panel lets you load in different sprites.

1. Make a sprite teleport to random locations until spacebar is pressed.
2. Have your sprite count to 10.
3. Take your sprite for a walk, bouncing off walls as needed.
4. Draw a shape with your sprite.

Now, with a neighbor, work out this problem:

1. Make the sprite ask for your name, and then have him spell it out, one letter at a time.

Both of you should individually create the animation on your computer, but you are encouraged to discuss how to do it.

Java

We won’t be using Scratch any more this semester. So, why did we use it today? It turns out that industrial-strength programming languages have a lot in common with Scratch. In particular, they have:

• A notion of sequence or process. Do this first, and then do this.
• Looping.
• Conditional execution.
• Treating data symbolically with variables.
• Data is typed. For instance, some data is numeric while other data is true/false. (Did you see this in Scratch? Look in the sensing palette. Oval blocks are numeric, pointed blocks are true/false. They only fit in places that exhibit these respective shapes. Look in the Control palette.)

Scratch is a fun and representative way to communicate a process to the computer, but it’s vocabulary is small and you are limited in the kind of software you can create. Let’s trade Scratch in for a big language called Java. We’ll see all these language features very early on in our use of Java.

The Java editor we’ll use is called Eclipse, which is free, powerful, and intimidating. Go ahead and open it up. Follow these steps to get a working development environment:

1. You’ll be prompted to choose a workspace, the place where all your files are created and 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. Close the welcome screen that appears.
3. Within a workspace you can have many projects. We’ll just use one for this class. Create it by choosing File, New, Java Project. Enter cs145 as its name and click Finish.
4. Let’s complicate things one more level. Within a project, you have multiple packages. We’ll create a package for each lab and assignment. Create this lab’s package by right-clicking on the cs145 project node and choosing New, Package. Enter lab1 as its name and click Finish.

Eclipse has a lot of features. We’ll use some of them.

Now you are organized and its time to make some Java code. Right click on the lab1 package node and choose New, Class. Enter the name Printer and hit Finish. You should see an editor open with this text:

package lab1;

public class Printer {

}

This is Java. It’s not as pretty as Scratch.

The best way to learn how to program is to try stuff and break a lot of things. Try running the code right now by hitting the green play button in the toolbar. What happens? In Java, all runnable programs have to have an chunk of code named main. Let’s add this inside the curly braces:

public static void main(String[] args) {

}

Run it again. What happens? Pat yourself on the back if nothing does.

Now, let’s make our program do something. Like Scratch, we’ve got to call upon the vocabulary of our language to make things happen. Let’s print some things to the console with a line like this in main.

System.out.println(INSERT SOME STUFF HERE);

Try various kinds of stuff. You can have many System.out.println invocations. Try numbers. Try mathematical expressions. Try text — though you’ll need to surround text with double-quotes.