teaching machines

CS 145 Lab 1

January 19, 2012 by . Filed under cs145, labs, spring 2012.

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.

Note that only step 3 really requires a computer. Accordingly, many computer scientists argue that the computer is a minor point in the study of computer science.

Syllabus

Read the syllabus. It’s short. Your instructor is a crotchety old man about a few things, so make sure you know how to keep him pacified. Take special note of the academic integrity policy. Computer scientists have powerful tools for detecting copied code.

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 every three weeks or so, 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 already completed solutions to your instructor in the first 20 minutes of the next lab. That means you will have to finish your checkpoints outside of lab before our next meeting.

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 all homework- and content-related questions. To familiar yourself with it, please go ahead and make a post to the discussion board right now, with the tag #lab. Send a note, rather than a question. (Be warned, Piazza defaults to sending out a copy of new posts to your email. You may get a few emails today.)

Where’s the discussion board? There’s a link to it in the syllabus. You also should have received an email invitation last week asking you to set up your account. If you have only recently added the class (and did not get this email), please let your instructor know.

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 take a moment to look thoroughly through the language’s vocabulary to see what you are capable of doing. Then, 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. (Look under Motion for movement, Operators for randomness, Control for repetition, and Sensing for spacebar detection.)
  2. Have your sprite count to 10. (Kudos to you if you use a variable and a repeat structure.)
  3. Take your sprite for a walk, bouncing off walls as needed. (Look under Motion to move and bounce, and Control for repetition.)
  4. Draw a shape with your sprite.

Now, with a neighbor, work out this problem:

  1. Make the sprite ask the user for a word, and then have it spell it out, one letter at a time. Try to make it work for any word the user types in. (Look under Sensing and Operators.)

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

Checkpoint #1: show your instructor your three animations.

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:

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.

Caveat coder

First class

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.

Checkpoint #2: show your instructor your working code.

TODO

Our goals today were to introduce ourselves, get a taste of programming, and leave you with a working Eclipse configuration. We’ll get more into Java in lecture. Here’s your TODO list: