A document from MCS 260 Fall 2021, instructor David Dumas. You can also get the notebook file.

MCS 260 Fall 2021 Homework 10 Solutions

  • Course instructor: David Dumas
  • Solutions prepared by: Johnny Joyce

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. (If you upload a screenshot or other file format, you won't get credit.)

Deadline

This homework assignment must be submitted in Gradescope by 10am CST on Tuesday, November 2, 2021.

Topic

This homework focuses on object-oriented programming. It is a bit shorter than usual to give you more time to work on Project 3.

Collaboration

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

Resources you may consult

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

(Lecture videos are not linked on worksheets, but are also useful to review while working on worksheets. Video links can be found in the course course Blackboard site.)

Point distribution

This homework assignment has 2 problems, numbered 2 and 3. Problem 2 is a bit longer than usual, so it gets 8 points this time. Thus the grading breakdown is:

Points Item
2 Autograder
6 Problem 2
8 Total

What to do if you're stuck

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

( 1. There's no problem 1 )

Gradescope will show the results of the automated syntax check of all submitted files as the score for problem 1.

2. Chemical element class

Create a file hwk10prob2.py that contains a single class definition, for a class Element. Objects in this class will store chemical elements. The class should have the following behavior:

  • The constructor has three required arguments other than self. They are:
    • Z, the atomic number of the element (a positive integer that uniquely identifies it), e.g. 3
    • name, the full name of the element, e.g. Lithium
    • symbol, the one- or two-letter symbol used to represent the element, e.g. Li
  • The constructor stores its arguments as attributes of self, so e.g. an Element object e has attributes e.Z, e.name, and e.symbol.
  • The __str__ method returns a string representation in this format: Lithium (#3). (It doesn't print anything, it just returns the relevant string.)
  • The __repr__ method returns the same thing as __str__. (It doesn't print anything, it just returns the relevant string.)
  • The == operator is overloaded by a method __eq__ of this class, which checks whether the object this element is being compared to has the same atomic number, returning a boolean. If the other object has a different atomic number, or if it isn't an instance of class Element, then this method should return False.

Below is some test code that should work once you've defined this class, and the expected output is shown below it. Don't include this code in your submission. Use it to test your work, but then only submit hwk10prob2.py containing a single class definition.

Make sure there are docstrings at the file, class, and method level.

Solution

In [1]:
'''A module containing a class to represent chemical elements'''

class Element:
    '''A class used to represent chemical elements.'''
    
    def __init__(self, Z, name, symbol):
        '''Inputs: Z:      the element's atomic number, 
                   name:   the element's name, 
                   symbol: the element's chemical symbol.
        Inputs are saved as attributes of self.'''
        self.Z = Z
        self.name = name
        self.symbol = symbol
        
    def __str__(self):
        '''Return a string with the element's name and number,
        e.g. "Carbon (#6)" or "Iron (#26)"
        '''
        return "{} (#{})".format(self.name, self.Z)
    
    def __repr__(self):
        '''Returns the string representation of the element'''
        return str(self)
    
    def __eq__(self, other):
        '''Returns True if self and other are both instances of Element
        AND they have the same number. Returns False otherwise.'''
        # Check if both objects are instances of the Element class
        if isinstance(other, Element):
            # Return a boolean (True/False) indicating whether atomic numbers match
            return self.Z == other.Z
        else:
            return False

Revision history

  • 2021-11-02 Initial release of solutions