![]() | |
Teachers Assignments Assignments Other Pages | Project 12: Assigned: Fri Nov 22 2024 In this project, you'll use methods to make classes more useable. Part 0, 3 points: Create another version of your >>> gary = create_pokemon('Gyarados', 130, ['Water', 'Flying'], 83)
>>> gary.print_pokemon()
Pokemon: Gyarados (130) HP: 83/83 In order to do this, you'll need to make sure to add it indented underneath your Pokemon class. This is the right idea: The following example won't work, because you've ended the Pokemon class before adding the method: I do recommend starting by copy-pasting your code from the non-method function into the correct place. (You need to keep the old function working!) Also, remember that you should only create each class once in your code, so make sure to use the Pokemon class you created previously! Tester.Part 1, 3 points: That name doesn't really make much sense! It's a method in the class, so let's get rid of the second part of the function name. Change the name of the method (make a copy) to >>> gary = create_pokemon('Gyarados', 130, ['Water', 'Flying'], 83)
>>> gary.print()
Pokemon: Gyarados (130) HP: 83/83 Tester.Part 2, 3 points: Methodize >>> gary = create_pokemon('Gyarados', 130, ['Water', 'Flying'], 83)
>>> gary.has_full_hp()
True Tester.Part 3, 5 points: Keep going! Methodize >>> gary = create_pokemon('Gyarados', 130, ['Water', 'Flying'], 83)
>>> gary.hit_points = 35
>>> spray_potion(gary)
>>> gary.print()
Pokemon: Gyarados (130) HP: 55/83 Tester.Part 4, 2 points: Let's turn >>> vandal = Pokemon()
>>> vandal.initialize('Jigglypuff', 39, ['Normal', 'Fairy'], 44)
>>> vandal.print()
Pokemon: Jigglypuff (39) HP: 44/44 In order to make this work, you'll need to make three changes to the function:
Part 5, 3 points: Okay, python has a special method, Part 6, 0 points: There's a reason we changed the name. Now you should be create Pokemon on one line like this: >>> vandal = Pokemon('Jigglypuff', 39, ['Normal', 'Fairy'], 44) This happens because python calls special methods in indirect ways. Here, whenever you create a Pokemon object with Pokemon(...) , python looks to see if a constructor exists. If so, it calls that after it creates the object.Part 7, 4 points: Let's create a class to model coordinates on the Earth! Create a >>> lh_latitude = GeographicCoordinate('N', 28, 1, 26)
>>> lh_longitude = GeographicCoordinate('W', 81, 56, 39)
>>> lh_latitude.print()
28°1'26"N
lh_longitude.print()
81°56'39"W To get that ° character in Python, you can use: \N{DEGREE SIGN} . Tester.Part 8, 3 points: What would be more useful than a >>> vandal = Pokemon('Jigglypuff', 39, ['Normal', 'Fairy'], 44)
>>> str(vandal)
Pokemon: Jigglypuff (39) HP: 44/44
>>> print(str(vandal))
Pokemon: Jigglypuff (39) HP: 44/44 In fact, print automatically calls str on it's parameters, so you can simplify that last line:>>> print(vandal)
Pokemon: Jigglypuff (39) HP: 44/44 Tester.Part 9, 3 points: Create a >>> lh_latitude = GeographicCoordinate('N', 28, 1, 26)
>>> lh_longitude = GeographicCoordinate('W', 81, 56, 39)
>>> str(lh_latitude)
'28°1’26"N'
>>> print(lh_longitude)
81°56'39"W In order to get that ° character in python, you can use \u00b0 , which will show up as that character if used in a string. Tester.Part 10, 3 points: If we want to model a single location on the globe, that will consist of two coordinates. Create a new class, >>> lh_latitude = GeographicCoordinate('N', 28, 1, 26)
>>> lh_longitude = GeographicCoordinate('W', 81, 56, 39)
>>> lh_location = Location(lh_latitude, lh_longitude)
>>> print(lh_location)
(28°1’26"N, 81°56'39"W) Tester.Part 11, 3 points: Let's create a class to model a geographic feature (even though we don't have many of them in Florida). Create a >>> britton_lat = GeographicCoordinate('N', 30, 59, 18)
>>> britton_long = GeographicCoordinate('W', 86, 16, 55)
>>> britton_location = Location(britton_lat, britton_long)
>>> britton_hill = Mountain('Britton Hill', 105, britton_location)
>>> print(britton_hill)
Britton Hill (105m tall @(30°59’18"N, 86°16'55"W)) Tester.Part 12, 3 points: Let's add another method to our Mountain class. Add a method >>> britton_lat = GeographicCoordinate('N', 30, 59, 18)
>>> britton_long = GeographicCoordinate('W', 86, 16, 55)
>>> britton_location = Location(britton_lat, britton_long)
>>> britton_hill = Mountain('Britton Hill', 105, britton_location)
>>> print(britton_hill)
Britton Hill (105m tall @(30°59’18"N, 86°16'55"W))
>>> mw_latitude = GeographicCoordinate('N', 44, 16, 14)
>>> mw_longitude = GeographicCoordinate('W', 71, 18, 12)
>>> mw_location = Location(mw_latitude, mw_longitude)
>>> washington = Mountain('Mount Washington', 1916, mw_location)
>>> britton_hill.taller_than(washington)
False Tester.Part 13, 2 points: Start a new class, >>> p = Point(4, 12) The attributes of a Point should be named x and y. Although I won't be grading it, I recommend creating a __str__ method. Tester.Part 14, 2 points: Add the >>> spot = Point(3, 4)
>>> another_spot = Point(4, 3)
>>> location = Point(3, 4)
>>> spot == another_spot
False
>>> spot == location
True Tester.Part 15, 3 points: Write a new function (not a method, so this won't be inside the Point class) >>> origin = Point(0, 0)
>>> out_there = Point(7, -6)
>>> middle = midpoint(origin, out_there)
>>> print('middle:', middle)
middle: Point at (3.5, -3.0) (You'll need an __str__ method to get that last line printed out nicely like that.)Tester.Part 16, 2 points: Add a new class, >>> corner_a = Point(6, 6)
>>> corner_b = Point(10, 6)
>>> corner_c = Point(10, 9)
>>> triangle = Triangle(corner_a, corner_b, corner_c) Tester.Part 17, 3 points: Add the >>> corner_a = Point(6, 6)
>>> corner_b = Point(10, 6)
>>> corner_c = Point(10, 9)
>>> t_0 = Triangle(corner_a, corner_b, corner_c)
>>> t_1 = Triangle(corner_b, corner_a, corner_c)
>>> t_0 == t_1
True
>>> Tester.Part 18, 1 points: Add a new method to your Triangle class, Part 19, 0 points: There's some tricky math and logic in the Triangle class, but sometimes classes are very simple. Let's create a class to represent one side of a DNA double-helix, also known as an Oligo (short for a strand of Oligodeoxyribonucleotides). Start a new class, Part 20, 2 points: Add the Part 21, 2 points: Add a new method, Part 22, 2 points: Add a new method, >>> oligo_a = Oligo('ATCG')
>>> oligo_b = Oligo('ATGC')
>>> oligo_c = Oligo('CGAT')
>>> oligo_a.is_complement(oligo_b)
False
>>> oligo_a.is_complement(oligo_c)
True This should always return True:>>> oligo = Oligo(anything)
>>> oligo.is_complement(oligo.get_complement()) Tester.Submitting your Project: Make sure all your code is in a file labelled with your user name (everything before the @ in your school email address) followed by |