This is the multi-page printable view of this section. Click here to print.

Return to the regular view of this page.

Assignments

Class assignments will be aggregated on this page.

1 - Assignment 1 - Problem Statements

Practice creating a well-defined problem statement, and then implementing it.

“Programming all too often begins without a clear understanding of the problem.”

– J.M. Yohe1

A good problem statement has the following structure in clearly-labeled sections2:

  1. Concise and precise statement of the problem.
    • Provide a summary of the problem. What is the problem you are trying to solve?
    • If appropriate, include relevant calculations or computations provided by your customer/boss/instructor.
    • Do not write the precise algorithm you will implement.
  2. Clearly specified input format. Input includes function arguments, file contents, or user console input. What does the data look like? For example:
    • three integer values separated by spaces on a line.
    • each bank record consists of the id number (integer), a name (string), and current balance (a float). These values are read from a file. Each line corresponds to a single record, and the values are separated by commas.
    • the user will be prompted to enter their first name, last name, and date of birth in the console as strings.
  3. Clearly specified output format.
    • Output could be printed to the screen, text written to a file, or the values returned from a function.
    • As with input, describe what does this data look like?
  4. Define exceptional conditions, e.g., things you should check for (like invalid values) and what you should do if they occur. For example:
    • The user enters a float when an integer is expected. What will you do?
    • The user provides a filename to read that doesn’t exist. What will you do?
  5. Sample input. This needs to be as exact and precise as possible. For example (matching #2 above):
    • 22,56,999
    • 850123456,"Bob Smith",500.00
    • First name: "Bob", Last name: "Smith", DOB: "01/02/2004"
  6. Sample output. Exact and precise as possible as with the sample input.

Part A - write a problem statement

This will be completed in class.

With a partner (required): Write a problem statement using the best practices described in class. Here is the high-level problem:

Write a program where the user enters three side lengths and the program says if they can form a triangle or not. This is the Triangle Inequality Theorem.

Suppose we label the numbers a, b, and c. They can form a triangle if:
a + b ≥ c, and
b + c ≥ a, and
a + c ≥ b.

The user will input the values from the console. The three sides should be entered separately. Your program must accept the input values, produce a result, and then terminate. You may include a loop that allows the user to repeat the process multiple times before quitting. It’s up to you.

Use this plain text template to write your problem statement. You must save it as a plain .txt file, not Word or anything else.

Grading

I expect that each spec.txt will be different, and that is okay. I am looking for the completion of the problem statement sections that address the functionality required.

Rubric

  • (6 pts) All elements of the template are completed. Sample input values and sample output values must include normal and exceptional behaviors.
  • (1 pts) At least two exceptional conditions are specified.
  • (3 pts) Correct spelling and grammar.

Submission due by Thursday, February 6

Each partner submits their team’s spec.txt to the Canvas assignment 1.A page.

Part B - Implementation

Individually create triangle.py and implement the problem you specified in spec.txt.

The goal is to warm-up your Python skills, and to see how the implementations of two people working from the same spec will be different.

You may not share code under any circumstances. Getting answers to simple Python questions from your peers is fine. Review the Syllabus section on Collaboration and Cheating for what is allowed and not allowed. If in doubt, ask me – I promise I won’t be mad if you ask.

Grading

You are graded on the correctness of your code, not the correctness of the spec.txt. If you discover an error in your spec.txt, you may communicate it with your partner.

Rubric

  • (10 pts): Functionally correct implementation that (a) accepts user input according to the format in your spec, and (b) produces correct output as to whether the input can form a triangle or not for several valid input values.
  • (5 pts): The program detects and handles at least two exceptional conditions you specified in spec.txt. “Handling” means it does not crash with an exception, but terminates or continues gracefully.

You will receive 0 points if your program does not run due to syntax errors or does not complete under “normal” conditions.

Submission due by Sunday, February 9

Submit your triangle.py file to the Canvas assignment 1.B page.


  1. Yohe, J. M. (1974). An Overview of Programming Practices. ACM Computing Surveys (CSUR), 6(4), 221–245. ↩︎

  2. Ledgard, Henry F. (1975). Programming Proverbs. Hayden Book Co. ↩︎

2 - Assignment 2 - Unit testing

Practice implementing and testing simple functions.

Objective

The goal of this assignment is to gain more practice with the tools we have used so far, including implementing a unit test with pytest.

Setup

  1. Create a new project directory named assn2/ or something similar.
    • Mac and Ubuntu on VirtualBox users: Open that directory using PyCharm as usual.
    • WSL users: Follow the setup process as the bottom of the page.
  2. Download grades.py to the assn2/ folder

Instructions

The assignment is to be completed alone. Refer to the Syllabus for policies on what is allowed and what is not.

Implementation

  1. Put your name at the top.
  2. You may not change the main() function in any way.
  3. You may add to the __main__ block to help test if you want.
  4. Complete the functions calculate_average() and determine_grade() according to their docstring descriptions:
    • The docstring tells you what the function must do, the parameters passed to it, the type that must be returned, and any exceptions you need to raise.
    • You may not add any parameters, change the return type, or add to or alter the exceptions required.
    • You must not call print() or input() from these functions.

Testing

  1. Create a test file for grades.py.
  2. Put your name at the top of your test file in a comment.
  3. Write one or more test cases in your test file for calculate_average().
    • The test case must invoke calculate_average() by providing a list, e.g., calculate_average([1,2,6]). Use assert statements to ensure the computed value is correct. You should have multiple asserts to check the calculation.
    • You must write test cases that check the exceptional conditions that raise value errors. Refer to the lab on testing for exceptions.
  4. Write one or more test cases in your test file for determine_grade(). This function does not knowingly raise exceptions, so you do not need to test for them. Test only for expected output given an input.
  5. Run your test file using pytest.

Rubric

  • (15 pts): Your implementation of calculate_average() passes my test cases, which exercises all the details of the function’s docstring.
  • (10 pts): Your implementation of determine_grade() passes my test cases, which exercises all the details of the function’s docstring.
  • (15 pts): Create a control flow graph (CFG) for each of your implementations of calculate_average() and determine_grade() following the rules from class.
    • You may use an online flow chart tool such as draw.io, Canva, or Lucidchart.
    • You may draw your CFG on paper and take a picture. Ensure it is legible.
    • In both cases, export your CVG image to a PDF to submit to Canvas.
  • (25 pts): Your test file can be run using pytest, has multiple test cases, and thoroughly tests the parameters, returns, computations, and exceptions raised of the functions as specified by their docstrings.
  • Your assignment will receive a score of 0 pts for any of the following:
    • print() or input() statements in calculate_average() or determine_grade()
    • Changing main() in any way
    • Changing the method signature of calculate_average() or determine_grade()
    • Your code or tests fail due to syntax error.

Submission due Feb 23

Submit your grades.py, your test file, and your CFG PDF file to the Canvas assignment page.

Setup for WSL Users

You will follow this process for every project on WSL from now on.

  1. Open PyCharm and select the File menu, then Close Project.
  2. Select WSL on the left, then the + button to create a new project.
  3. Select the ... button to pick the Project directory.
  4. Pick your Ubuntu instance at the top, then navigate to home/<your_id>/seng-201/ and create a new folder (icon at the top) for assn2/.
  5. Select the new directory and hit OK.
  6. Click Start IDE and Connect on the screen. PyCharm will take a minute to finish configuring. It should open a new window with a main.py file showing some boilerplate code.
  7. Select the File menu, then Settings.
  8. Select Project: assn2 in the left pane, then click the Python Interpreter link.
  9. Select the Add Interpreter link near the top right, then Add Local Interpreter.
  10. Leave the default options selected and hit OK. If you see a red error message, contact the instructor.
  11. OK out of the settings screen.
  12. Finally, open a new Terminal within PyCharm. Type which pip. You should see something like
    • /home/<your_id>/seng-201/assn2/.venv/bin/pip, or;
    • /home/<your_id>/virtualenvs/assn2/bin/pip
    • but not /usr/bin/python
  13. You will run all subsequent Terminal commands from the integrated Terminal in PyCharm.
  14. run the following in the integrated Terminal:
    pip install pytest pytest-cov
    
  15. Complete the setup instructions at the top of this lab.

3 - Assignment 3 - Information Literacy

Putting your information literacy skills to the test.

Objectives

  • Write effective Internet search queries and use official documentation to research answers.
  • Examine and compare information from various sources to evaluate its accuracy, authority, currency, and relevance.
  • Properly cite and apply the researched information to help solve software engineering problems.

Overview

Software engineers constantly use the Internet to learn how to achieve functionality and to help debug errors.

You will find plenty of wrong or misleading answers on the Internet. Who has time for that? You need to be able to discern between good sources from time wasters.

Instructions

Download and complete assignment_3.docx. Complete it with a partner in class and submit to Canvas.

Code for Task 1

Run the following Python code. You will get an error and stack trace:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
def calculate_cart_total(cart_items, tax_rate):
    """Calculate total cart cost including tax."""
    subtotal = sum(item["price"] * item["quantity"] for item in cart_items)
    return subtotal + (subtotal * tax_rate)

def display_cart_summary(cart_items, tax_rate):
    """Display a summary of the cart."""
    total_cost = calculate_cart_total(cart_items, tax_rate)
    print(f"Total cost (including tax): ${total_cost:.2f}")

def main():
    """Main function to simulate a shopping cart."""
    cart = [
        {"name": "Laptop", "price": 1000.0, "quantity": 1.0},
        {"name": "Headphones", "price": 200.0, "quantity": 2.0},
        {"name": "Mouse", "price": 50.0, "quantity": 1.0},
        {"name": "Keyboard", "price": 150.0, "quantity": 1.0},
        {"name": "Monitor", "price": 300.0, "quantity": 1.0},
        {"name": "HDMI Cable", "price": 10.0, "quantity": 2.0},
        {"name": "USB Drive", "price": "20.0", "quantity": 3.0},
        {"name": "External Hard Drive", "price": 100.0, "quantity": 1.0},
    ]
    tax_rate = 0.07  # 7% sales tax
    display_cart_summary(cart, tax_rate)

if __name__ == "__main__":
    main()

Submission due Friday Feb 28 @ 11:59pm via Canvas

Upload your completed assignment_3.docx to the Canvas assignment page.