.py
files containing your work. (If you upload a screenshot or other file format, you won't get credit.)This homework assignment must be submitted in Gradescope by 10am CST on Tuesday, November 2, 2021.
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 is prohibited, and you may only access resources (books, online, etc.) listed below.
The course materials you may refer to for this homework are:
oop
folder)(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.)
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 |
Ask your instructor or TA a question by email, in office hours, or on discord.
Gradescope will show the results of the automated syntax check of all submitted files as the score for problem 1.
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:
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
self
, so e.g. an Element
object e
has attributes e.Z
, e.name
, and e.symbol
.__str__
method returns a string representation in this format: Lithium (#3)
. (It doesn't print anything, it just returns the relevant string.)__repr__
method returns the same thing as __str__
. (It doesn't print anything, it just returns the relevant string.)==
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.
'''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