Skip to main content.

Lab 2: Advanced Arithmetic and Object-Oriented-Programming

Goals

After the lab, you should know how to

  1. more advanced arithmetic problems
  2. using an API to solve problems
  3. creating basic web pages to display images

Objective: Review

Review the slides for today.

Objective: Set Up

Run runHelpClient &

Linux: Set up for Lab 2

  1. Create a directory called lab2 in your cs111 directory. Your programs and the output for this lab will all be saved in the lab2 directory.
  2. Copy the graphics.py file from the directory /csdept/courses/cs111/handouts/lab2 into your lab2 directory.

Objective: Programming in Python

We'll practice writing several Python programs, each in their own text file. Name the files lab2.1.py through lab2.7.py.

Your programs will be graded on correctness, style, and how well you tested them. Make sure you adhere to the good development and testing practices we discussed in class. Your code should be readable and your output should be useful and well-formatted.

After you've developed a correct solution to each program, restart IDLE or close and reopen the IDLE "shell" by running the program again (using F5), demonstrate that the program works using several good test cases, and save the output to a file named lab2.x.out, where x is the problem number.

You can use Python Visualizer to help you see what is happening in your program. This is the visualizer used in the text book.

  1. (15 pts) Write a program that demonstrates the importance of operator precedence. Your program will get three integers from a user and assign them to the variables a, b, and c. Then, print the result of a Python expression that has no parentheses, using those three numbers. Then, show a different result from the same expression with one added pair of parentheses.

    Example output (with fake numbers--I'm not showing an equation):

    Enter a: 12
    Enter b: 34
    Enter c: 5
    The result of <print out your equation here> is 51
    The result of <print out your equation with parentheses here> is 15
    
  2. (13 pts) Distributing Greatest Hits Albums. A band is putting out their Greatest Hits album and needs to know how many cds their album requires. Bands like the Ramones can fit a lot more tracks on a cd than a band like Led Zeppelin.

    Write a program that takes the number of greatest hits and the size of the cds (in terms of the number of tracks) and determines how many cds are needed and how many tracks will have to wait for the next Greatest Hits album.

    This program determines the number of CDs in a Greatest Hits album.
    
    How many greatest hits/tracks do you have? 24
    How many tracks fit on a cd? 10
    
    Your album requires 2 cds
    4 tracks will have to wait for the next Greatest Hits album.
    

    Note: you may have some grammar issues in your output. We don't know how to fix those yet.

  3. (15) "Mind-blowing Math Trick Can Tell Your Age and Shoe Size" You may have heard about this silly trick. You probably didn't think about implementing it in a program, but you can!

    There are several steps, but they're all relatively simple to implement.

    1. Ask the user for their shoe size--this should be an integer--and the year they were born.
    2. multiply the shoe size by five
    3. add 50 to that number
    4. take that total and multiply it by 20
    5. add 1,017 to that total
    6. subtract the year you were born

    The result: the first two numbers of the four-digit number are your shoe size and the last two numbers are your age (if your birthday hasn't happened this year yet). Extract those two numbers and present them to the user.

    Example Run:

     This program will figure out your shoe size and your age.
     Enter your shoe size (not half sizes): 9
     Enter the year you were born: 1996
     
     Working...
     Multiply shoe size by five
     45
     Add 50 to that number
     95
     Take that total and multiply it by 20
     1900
     Add 1017 to that total
     2917
     Subtract the year you were born
     921
     
     Your shoe size is 9
     Your age is 21
     

    Reference Material for Graphics Programming

  4. (10) Using the graphics module, draw a yellow circle with radius 30 and a red square of width 50 in a window that is 400x200 with the name "Practice". Position the circle in the upper-left quadrant of the canvas and the square in the lower-right quadrant.

    Please add two calls--one to getMouse() and one to close()--on your GraphWin object at the end of the program. It won't make a difference for you in IDLE, but it does make a difference for me when I run the programs automatically.

    There will be no IDLE output for this program.

  5. (10) Using the graphics module, draw the beginning of a snow-person. Create a canvas with the title "Snow Person". Draw a white circle of radius 50. Clone the circle and move the cloned circle above the first circle (so that the circle appears to be sitting on top of the original circle). Repeat with a third cloned circle. Draw two black, filled-in circles for eyes in the top circle. Note that you should draw one eye, then clone the eye and move it to the appropriate place.

    Please add two calls--one to getMouse() and one to close()--on your GraphWin object at the end of the program. It won't make a difference for you in IDLE, but it does make a difference for me when I run the programs automatically.

    There will be no IDLE output for this program.

  6. (16) Write a program that displays a canvas, requests users click where a line, by its two endpoints, should be drawn on a canvas, and then draws the line that connects those two points. (How can the user specify the points of the line?) Display the directions to the user on the canvas in a Text object, and update the directions as the user clicks.

    Example screen shots:

    Again, please add the calls to getMouse() and close() on your GraphWin object at the end of your program.

    There will be no IDLE output for this program.

  7. (16) Create a program that draws "something significant", such as a scene, a house, or a face, using the graphics library. Here are some guidelines to follow:
    • Your program should keep the drawn face/house/scene on the graphics window until the user clicks the mouse. Then the graphics window should close and your program should end.
    • Use a variety of colors and several different types of shapes.
    • For symmetrical features (such as eyes or ears), remember to use the clone method to make a copy of the original shape. Then draw the cloned shape in the window and move it to the desired location.
    • You may want to use the setCoords method of the GraphWin object to adjust the coordinates of the window. (See the reference above for more information about setCoords.)

    Again, please add the calls to getMouse() and close() on your GraphWin object at the end of your program.

    Some previously created images to inspire you:

    Screen Capture:

    • To save the image you created, click on "Applications" in the upper left, click "Activities Overview", and search for "Screenshot". Click on the "Screenshot" application.
    • Select "Grab the current window" option.
    • Click the "Take Screenshot" button.
    • Click on your image.
    • Save the image in your public_html directory. Under the "Save in folder", click "Other", and then navigate to your home directory and then go to public_html. Then click "Open". Now, "public_html" should show up in "Save in folder". Then, give the screenshot a descriptive name, e.g., "myHouse.png". Then, click "Save".

    You've saved the image!

Objective: Creating a New Web Page (5)

It's time to revisit our web pages. Note that there are links in the directions for more info.

  1. Go into your public_html directory.
  2. Copy your index.html file into a file called lab2.html, still in the public_html directory (i.e., not in a new directory)
  3. If you haven't already, copy the screen capture image from your lab into your public_html directory.
  4. Open lab2.html in jEdit.
  5. Modify the Lab 2 web page to have an appropriate title, heading, and information about the image you created during this lab.
  6. View your web page in the browser. Recall the URL for this page. You may want to review Lab 0.
  7. Modify your Lab 2 web page to display the image you created.
  8. Clean up your Lab 2 page so that it only contains the image, a link to your home page, and any appropriate text. (In other words, get rid of the stuff that should only be on your index.html page.)
  9. Modify your index.html page to link to your Lab 2 web page.
  10. Verify your web pages look correct in the browser. Make sure that the URL in the location bar starts with "cs.wlu.edu" (or similar) and not "file://" to verify that anyone on the Web can view your web pages.

Finishing up: What to turn in for this lab

Review the Unix commands for submitting your lab.

Note that each command below links to a page with more information about using the command.

  1. Submit your lab2 directory for grading by running the turnin.sh lab2 command. If you have every thing set up correctly, this will copy your lab2 directory into your turnin directory so that you and I (only) can see your submission. (If you run the turnin.sh script again, it will copy the current directory and create a backup of the previous submission.)
  2. Clean up: jEdit makes backup files and appends "~" to the name of your file. Delete any "~" files from your lab directory.
  3. Delete graphics.py and any image files that are in your lab2 directory because they'll screw up your printing. Alternatively, you can mv graphics.py into the cs111 directory.

  4. Go into your cs111 directory. Create the printable lab assignment, using the command:
    createPrintableLab <labname>
  5. View your file using the evince command. If there are issues with your printout, go back and fix them.
  6. Print the file using the lpr command introduced in the last lab.

Labs are due at the beginning of Friday's class. You should hand in the printed copy at the beginning of class, and the electronic version should be in the turnin directory before 1:30 p.m. on Friday.

Ask well before the deadline if you need help turning in your assignment!

Grading (100 pts)