The purpose of today's lab is to give you familiarity with the systems we will be using throughout the semester. There are 3 components:

Read the instructions below carefully and complete each tutorial and exercise as indicated. In this course you will often work as part of a pair, but for today, please work on your own (asking lots of questions of those around you!)

Using these Lab Exercises

Please keep the following things in mind as you do the lab exercises in this course:

Operating Systems

To design and implement algorithms on a computer, you must first know how to control the basic operations of the computer. This is one of the fundamental purposes of the operating system. Study the tutorial appropriate for the operating system that you will be using in these labs (only!). It is important for craftspeople to know their tools well, so we suggest that you at least review the material even if you’ve used the system in the past.

Eclipse - An Integrated Development Environment (IDE)

While it is possible to write programs using only simple text editors, it is often preferable to work within a system specifically designed for writing code. This is the purpose of an IDE. IDEs usually provide source code editors (including some built-in features making it easier to spot syntax errors), the ability to run your code as a program, and a means of helping to debug your program.

In this course we will be using the Eclipse IDE, together with a plug-in (that is, an extension of Eclipse) called PyDev which can be used for Python code development.

Do This ...
Complete the Eclipse introduction

Using what you have learned in the Operating System (Linux, Windows or Mac) and Eclipse introductions you should now be able to complete the following exercise:

Exercise 1.1

Do the following things on your development system:

  1. If necessary, create a new Eclipse project named cs108 and a folder inside that project called lab01.
  2. Use Eclipse to create a text file in your lab01 folder called aboutMe.txt that contains:
    • your full name,
    • why you are taking this class, and
    • your major (or ones you are considering) together with an explanation of why you have chosen (or are considering) that major.
  3. Save your file (File→Save)
  4. Find your file in the directory system and verify that it contains the correct information (e.g., on UNIX, use the cat command from the terminal window to check the file’s contents).
You will submit this file at the end of the lab.

A Python program using turtle graphics

Python is a powerful programming language with many built in features and commands. For example, Python gives the programmer the ability to evaulate mathematical expressions, receive input from the user, and print values to the console window with basic commands.

More advanced features are also available by making use of previously created modules, which can be thought of as collections of code. Today, we will use a module called turtle which gives us access to a collection of functions for Turtle graphics.

Exercise 1.2
  1. Create a new file in your lab01 folder called exercise2.py . NOTE: Rules for Python file names:

    • Must never contain spaces
    • Should start with a lowercase letter
    • Should end with .py

    Double check that you named the file correctly. If you did not, right-click on the name of the file and choose Rename... to correct your file name.

  2. Copy and paste the following Python code into your file:

    ''' A first turtle graphics program
    Created Fall 2014
    Lab 01
    @author: Serita Nelesen (smn4)
    '''
    
    # Gain access to the collection of code named "turtle".
    import turtle
    
    # Give the name "window" to the screen where the turtle will appear.
    window = turtle.Screen()
    
    # Create a turtle and name it bob.
    bob = turtle.Turtle()
    
    # Tell bob the turtle to move forward 250 pixels.
    bob.forward(250)
    
    # Keep the window open until it is clicked.
    window.exitonclick()
  3. Click the green arrow button (known as "Run"). You may have to specify a Python Run.

  4. Watch the "turtle" draw a line across the screen.

  5. Click anywhere on the screen to close the window.

Take a minute to study this code. At the top, contained within triple quotes is a “docString”. Every file you ever submit *must* have such a docstring that includes the following features:

The lines beginning with # are comments , that is, information ignored by Python but useful for the programmer. Good code is always carefully documented. By convention, comments describe the code that follows.

In the code above we tell Bob the turtle to move forward, but there are other commands we can give Bob:

Do this...

Let's extend the sample code to have the turtle draw a star instead of the current line. You can do this as follows:

  1. Update the doc string at the top of the file to indicate that this program will draw a star.

  2. Add your name and login id to the list of authors. You can do this by adding another line below the current authors: @author: Your name (your login id)

  3. Give the turtle commands that implement the following algorithm.

    1. Move forward 250 pixels
    2. Turn right some number of degrees
    3. Move forward 250 pixels
    4. Turn right some same number of degrees
    5. ... finish the star

    Hint: How many degrees are there in a circle? And how many points in the star?

When your turtle correctly draws a star, make sure to save your program. Your file is probably is already saved, but it never hurts to check. You can tell if a file is saved by looking for an asterisk (*) next to the name of the file in the tab at the top of the screen. If there is no asterisk, the file has already been saved.

There are many more commands that you can give a turtle. These are described in the Application Programmer Interface (API) for the module: Turtle graphics. An API gives the names of commands that are available in the module, as well as information about what inputs are required and what outputs can be expected. The API for all of Python is available here: Python documentation

Right now, this documentation is probably overwhelming, but as we proceed through the course you will learn how to use this resource to find helpful modules, and then how to use those modules effectively.

Exercise 1.3

Use what you have learned to have the turtle draw the picture on the right. Create a new file for this program called exercise3.py and include appropriate header documentation. Then implement an appropriate algorithm, such as the one given here.

  1. Gain access to the turtle module.
  2. Create a window and a turtle.
  3. Have the turtle draw a “C”.
  4. Pick up the pen.
  5. Move the turtle to the top of the “S”.
  6. Put down the pen.
  7. Have the turtle draw the “S”.

The steps of the algorithm should appear as comments in your code to help keep track of what is happening where.

Checking In

We will grade these exercises according to the following criteria:

Submit your solutions to these lab exercises using the appropriate tool.

If you’re working on a lab computer, don’t forget to log off of your machine when you are finished!