Lab 4, CSC 101

Note that this lab is due on February, 4th.

This lab introduces lists, repetition, and reading from a file.

Download lab4.zip, place it in your cpe101 directory, and unzip the file.

Lists

This part of the lab introduces lists. You must not use any loops in these functions. The goal of this initial exercise is to understand the mechanics of using lists so you should focus only on that.

In the poly directory create poly.py and poly_tests.py (for your test cases).

You must provide at least two test cases for each of these functions.

Polynomial Arithmetic

For this part of the lab you will develop two functions that perform basic arithmetic on polynomials. A polynomial will be represented as a list. The values in the list will represent the coefficients of the terms whereas the indices will represent the exponents for the terms. This means that the polynomial 2.7x2 + 3.1x + 2 will be represented by the following list. Notice that the term with exponent 0 is first in the list while the term with exponent 2 is last (i.e., the terms in the list are in reverse order of how they are typically written in mathematics).

poly1 = [
   2,       # This is the coefficient for x^0.
   3.1,     # This is the coefficient for x^1.
   2.7      # This is the coefficient for x^2.
]

This list could, of course, be defined on a single line as follows.


poly1 = [2, 3.1, 2.7]

You may think this mapping of a polynomial to a list is a bit odd. In fact, attributing meaning to the indices of a list (and not just the values within the list) is a pretty important skill that allows a list to be used as more than just a substitution for a bunch of variables.

poly_add2

In poly.py, define the poly_add2 function. This function takes two polynomials (as lists) of degree two and returns a new polynomial (also a list) that represents the sum of the argument polynomials. Recall that the degree of a polynomial (of one variable) is the greatest exponent in the polynomial (the list will have a length of one greater than the degree).

Though the testing framework does work with lists, it does not support an "almost" equal check for the contents of a list. In the provided testing file you will find assertListAlmostEqual. It can be used, in a testing function, as follows.


   def test_poly(self):
      poly1 = [2.3, 4.7, 1.0]
      poly2 = [1.2, 2.1, -3.2]

      poly3 = poly.poly_add2(poly1, poly2)
      self.assertListAlmostEqual(poly3, [3.5, 6.8, -2.2])

poly_mult2

In poly.py, define the poly_mult2 function. This function will take two polynomials of degree two and return the product of the two polynomials.

Note carefully: The polynomial resulting from a multiplication will, in general, be of degree greater than the argument polynomials. In this case, the result can be of at most degree four, so your result list (declared in your test cases) may be larger than initially expected.

Again, though the use of loops would allow one to generalize this function, for this exercise you cannot use any loops. Think carefully about how to compute the product of polynomials and how that relates to the representation of polynomials in this lab.

Looping

For this part of the lab you will implement generalized versions of the polynomial functions from the first part.

poly_add

In poly.py, define the poly_add function. This function will take two polynomials (represented as lists) as arguments and return their sum. Unlike the earlier part of this lab, the argument polynomials can be of any degree (and need not be of the same degree).

You will use loops for this function.

poly_mult

In poly.py, define the poly_mult function. This function will take two polynomials (represented as lists) as arguments and return their product. Unlike the earlier part of this lab, the argument polynomials can be of any degree (and need not be of the same degree).

You will use loops for this function.

Reading Files

Copy into the file directory all of the files from your solution to the ball portion of the previous lab.

In this part of the lab you will extend your bouncing ball program to read the locations and radii for multiple balls from a file and to animate all of the balls.

Modify your program to open the file named balls. Each line of this file contains (in this order) the x-coordinate, y-coordinate, and radius (and color, but that may be used later). Your program should read each line of the file, split the line into a list, and then create a Ball with the values read (you can convert a string value into the corresponding integer with the int function) and with a color of your choosing. Gather each of these balls into a list.

Modify your movement and drawing functions so that each ball in the list is moved and drawn.

Once you have all of the balls moving properly, consider how you might initialize the color based on the contents of the file.

Demonstration

Demonstrate the test cases from each part of the lab to your instructor to have this lab recorded as completed. In addition, be prepared to show the source code to your instructor.