.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.
import hwk10prob2
a = hwk10prob2.Element(Z=47,name="Silver",symbol="Ag")
b = hwk10prob2.Element(Z=47,name="Silber",symbol="Ag") # German name
c = hwk10prob2.Element(19,"Potassium","K") # pass arguments positionally instead of using keywords
print("Testing Element.__str__:")
print(a)
print(c)
print("\nTesting Element.__repr__:")
print(a.__repr__())
print(c.__repr__())
print("\nTesting existence of attributes:")
print("Z =",a.Z)
print("name =",a.name)
print("symbol =",a.symbol)
print("\nTesting equality:")
print("In a case with expected result True:",a==b) # comparison should consider only Z, ignoring name and symbol
print("In a case with expected result False:",a==c)
print("In a case with expected result False:",a=="whatever")