Lab 2: Advanced Arithmetic and Object-Oriented-Programming

Goals

After the lab, you should know how to

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

Objective: Review

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 assign values to the variables a, b, and c. Then, print the result of a Python expression that has no parentheses, using those three variables. Then, show a different result from the same expression with one added pair of parentheses. Finally, go back and--instead of hardcoding the values for a, b, and c--prompt the user for values for the variables.

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

    Enter a: 12
    Enter b: 34
    Enter c: 5
    The result of <display your equation here> is 51
    The result of <display your equation with parentheses here> is 15
    

    Note: When you demonstrate your program, think about what are good test cases for this program. What answers do you know?

  2. (12 pts) Write a program to help a party of friends split a restaurant bill equally. The program should ask for the total bill amount, the percent tip to add, and how many people are splitting the bill. Here is what two runs of your program should look like:

    Bill amount in dollars: 55.34
    Percent tip: 20
    Number of people: 3
    
    The tip is 11.068000000000001 dollars
    The total cost is 66.408 dollars
    The cost per person is 22.136 dollars
    

    Bill amount in dollars: 162.29
    Percent tip: 15
    Number of people: 8
    
    The tip is 24.3435 dollars
    The total cost is 186.6335 dollars
    The cost per person is 23.3291875 dollars
    
  3. (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.

  4. Reference Material for Graphics Programming

  5. (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.

  6. (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.

  7. (16) Write a program that displays a canvas, requests users click where a line, by its two endpoints, should be drawn on a canvas, draws the points, and then draws the line that connects those two drawn 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.

  8. (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:
    • 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 on your image. You gotta be quick! (I had to take several tries. If it's not what you expect, click the left arrow button to go back.)
    • Click the "Take Screenshot" button.
    • 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!

    If you are not in the lab, email me a screenshot taken on your personal computer.

Objective: Creating a New Web Page (8)

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

If you had errors/issues in your web page, fix them first.

  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 lab2 web page in the browser. Recall the URL for this page. You may want to review Lab 1.
  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. Make sure you're in your lab2 directory.

    Clean up

    1. jEdit makes backup files and appends "~" to the name of your file. Delete any "~" files from your lab directory.
    2. Move graphics.py into your cs111 directory. (see mv. Note where the cs111 directory is with respect to where you're running this command from.)
    3. Delete any image files that are in your lab2 directory because they'll screw up your printing.
    4. If you have a file graphics.pyc (by accidentally running using Python2 instead of Python3), you should delete that file before printing.
      In other words, you should only have the .py files you wrote, the .out files you created, and a __pycache__ directory in your directory when you print. (The latter is not printed.)
  2. Create the printable lab assignment, using the command:
    createPrintableLab <labname>

    The file will be in your cs111 directory.

  3. View your file using the evince command. The file should be in your cs111 directory (e.g., ~/cs111/lab2.pdf). It should only be a few pages. If there are issues with your printout (most commonly, images or graphics.py in your lab2 directory), go back and fix them.
  4. Move graphics.py back into your lab2 directory after printing.
  5. 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.

Labs are due at the beginning of Friday's class. The electronic version should be in the turnin directory before 2:25 p.m. on Friday.

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

Grading (100 pts)