A document from MCS 275 Spring 2023, instructor David Dumas. You can also get the notebook file.

MCS 275 Spring 2023 Homework 3

  • Course Instructor: David Dumas

Instructions:

  • Complete the problems below, which ask you to write Python scripts.
  • Upload your python code directly to gradescope, i.e. upload the .py files containing your work.

Deadline

This homework assignment must be submitted in Gradescope by Noon central time on Tuesday 31 January 2023.

Collaboration

Collaboration is prohibited, and you may only access resources (books, online, etc.) listed below.

Content

This assignment is about object-oriented programming, based on worksheets 1-3 and lectures 1-4. It focuses on the material of worksheet 3.

Resources you may consult

The course materials you may refer to for this homework are:

Point distribution

This homework assignment has 2 problems, numbered 2 and 3. The grading breakdown is:

Points Item
3 Autograder
6 Problem 2
6 Problem 3
15 Total

The part marked "autograder" reflects points assigned to your submission based on some simple automated checks for Python syntax, etc. The result of these checks is shown immediately after you submit.

What to do if you're stuck

Ask your instructor or TA a question by email, in office hours, or on discord.

Problem 1 doesn't exist

In Gradescope, the score assigned to your homework submission by the autograder (checking for syntax and docstrings) will be recorded as "Problem 1". Therefore, the numbering of the actual problems begins with 2.

This will happen on every assignment this semester. Starting on the next homework, I won't comment on this practice in the homework, and will just omit problem 1.

Problem 2: The universal mimetic class

Put the work you do for this problem in a file hwk3prob2.py.

Make a Python class UniversalMimic whose constructor takes no arguments (e.g. you can make one with UniversalMimic()). Use operator overloading to make it so that any instance of this class thinks it is equal to every other object. For example:

In [10]:
U = UniversalMimic()
In [11]:
1 == U
Out[11]:
True
In [12]:
U == 0
Out[12]:
True
In [13]:
U == None
Out[13]:
True
In [14]:
U == "a slice of apple pie"
Out[14]:
True
In [47]:
U == 2.75
Out[47]:
True
In [49]:
U == { "course": "MCS 275", "meeting time": "12:00pm", "enrollment_max": 28 }
Out[49]:
True

Note that in an expression like A == UniversalMimic(), it's possible that A might have its own __eq__ method that returns False, and since Python checks with the left object first, you can't avoid this test returning False. Don't worry about that. Just make your best effort to ensure all equality comparisons this class controls will return True.

Remark: This is a contrived test of overloading, and not something you should do in actual code.

Problem 3: Into the third dimension

Put the work you do for this problem in a file hwk3prob3.py.

In lecture 4, we built a module plane.py that represents

  • Two-dimensional points $(x,y)$ using the Point2 class, and
  • Two-dimensional vectors $\langle x,y \rangle$ using the Vector2 class

Take that module and modify it (renaming it to hwk3prob3.py) so that it instead contains these two classes:

  • Point3 representing three-dimensional points $(x,y,z)$ and
  • Vector3 representing three-dimensional vectors $\langle x,y,z \rangle$.

Mostly this will involve small changes to account for the third coordinate/component.

But also add one new method to Vector3:

  • def cross(self,other): : Returns the vector cross product $(\mathrm{self}) \times (\mathrm{other})$. Thus if v and w are Vector3 objects, you can compute the cross product (which is another Vector3) as v.cross(w).

Also take care to update __abs__ so it knows the right way to compute length in 3 dimensions.

3D geometry

No prerequisite course of MCS 275 has 3-dimensional geometry in its required topic list, so here are the things you need to know in this problem:

  • Adding 3-dimensional vectors to each other and to points is just like the 2-dimensional case: It happens separately for each component, but now there are $x$, $y$, and $z$ components.
  • The vector cross product is given by this formula:
$$ \langle a,b,c \rangle \times \langle d,e,f \rangle = \langle bf - ce, \, cd-af, \, ae-bd\rangle $$
  • The length of vector $\langle a,b,c \rangle$ is given by the formula
$$\sqrt{a^2 + b^2 + c^2}.$$

If anything about the geometric side of this question is unclear, please ask! We mean for it to be a question about translating those ideas into code.

Revision history

  • 2023-01-26 Initial publication