Compsci 101, Fall 2017, Lab 1

Getting Credit for Lab 1

In each lab for Compsci 101 there will be a form to complete to earn credit for the lab.

To get credit for lab 1, you will need to enter your names and netids, and the answers to several questions on an online form.

Fill out this form for credit for Lab 1

Getting Started Programming

Before coming to lab you should have attempted to try out the VM we are using for this course. Instructions are here on the resource page.

In this lab

If you have any questions about the course, post your questions to Piazza the course discussion forum. If you are not on you'll need to sign up for an account for CompSci 101 Fall 2017 using your Duke email address. See the message on Sakai on how to sign up.

PART 0: Try out Python on the VM

To write programs, you will need some software tools, just as writing a paper requires a wordprocessor (pretty much), writing software requires using other programs. These allow you to edit your program, translate it into a simpler representation that your computer can understand, and run it. These initial steps may seem a little overwhelming, but they only need to be done once to get things started and working together.

Click here for Setting up Python on the VM If you don't get this done in lab, find a UTA during consulting hours or see one of the course instructors during office hours to get this done.

VERY IMPORTANT : It is important that you do not change the default workspace path in Eclipse that the VM gives you.

In setting up your environment, create a PyDev Project called SampleProject (you may have created this in the instructions on the Resource page).

Create a new Python program in this project called helloMessage that just prints any message you want to spread around, such as "Duke has a stellar team this year."

You will need to create a new PyDev Module that just prints that message.

Answer the following two questions

  1. When you run your program to print your message, where is it displayed in Eclipse?
  2. Cut and paste your code for helloMessage into the google form.

PART 1: Snarfing and Running a Python program

"Snarfing" a program is just our cool way of creating a program that you will download or copy to your computer. By snarfing this complete python program, it will already be ready to run and you can then modify it to see how you can change it.

  1. Download (or "snarf") the project (code and related files) for Lab 1 through Eclipse and run it as discussed below.
    See this site for help with Snarfing. In general to snarf projects, click Browse Snarf Sites, then click Add a new project site. You should add this site:
  2. http://www.cs.duke.edu/courses/compsci101/fall17/snarf
  3. Once the project fall17_cps101_lab01 has been created you are ready to test your installation and experiment with Python code.
  4. If you have trouble with snarf, you can also get the file graphing.py here.

Experimenting with Python Code

To run the code, open up the project file in src called graphing.py by double-clicking on it, then use the green Play button on the toolbar. You may have to choose what kind of Python run to use depending on your Python settings (choose Python Run if you have a choice). Running the program should create and display a graph. You may get a small rocket-shaped icon representing the open graph. If that worked, everything is installed correctly!

You should see a graph that looks like the picture below.

picture

To experiment with the code, look at the file graphing.py, and try to figure out generally how it works. To direct your experimentation, do the problems below that require you to change the file graphing.py, run it to see the results, and enter your answers in the online form.

  1. Currently, the program plots the expression y = x2 - 7x + 20 over the range, x-values, [0, 5]. Change the program so it graphs the same expression but instead between the x-values 0 and 20. This will require finding the part of the program that calls the function displayFunctionGraph and changing the first two parameters of the call.

    What changes made the plot go from 0 to 20?
  2. Change the program to plot the expression y = (x-4)3 over the range [0, 20].

    This will require changing the body of the function named functionToGraph. Note that to raise to a power use the ** operator, e.g., 2**5 is Python for 25.

    What changes were needed to graph y=(x-4)3? Describe in words what the curve that appears looks like.

  3. Change the program to plot a graph of y = sin(x) over the range [0,20]. To use the sin function you'll need to write math.sin(x) because sin is in the math library.

    How many complete cycles of the sine wave are there between x=0 and x=20 according to what you see?
  4. Graph a function where the y-value is chosen at random by changing the body of functionToGraph to the following:
        return random.randint(0,20)
    


    What do you see with the random function and why?

PART 3: Scratch Programming

Create a sample scratch program, upload it to the Scratch site enter the link to your scratch program in the online questionaire form.

Specifically, answer these questions in the online form:

  1. Give the name of the control block that lets you repeat code a specified number of times?

  2. Explain what the code would be if you wanted to press the letter H and your sprite would say "hello".

  3. Enter in the URL for your Scratch world.


See above for how to get credit for this lab. You will need to answer questions in a google form.