EX09 - classes


Exercise 00 – Ballin’

  1. In the comp110/exercises directory create a new folder named ex09_classes
  2. Create a file named basketball.py in your comp110/exercises/ex09_classes folder.
  3. Write a class called BballGame with the following specifications:
    1. Each BballGame should have a boolean attribute called biscuits, an int attribute called points, a str attribute called winning_team, and a str attribute called losing_team.
    2. Your Bballgame class should have a constructor that takes in points, winning_team, and losing_team. The constructor should also initialize biscuits to be False.
    3. Your class should also have 3 methods: check_points, winner, and reset_points. The functionality of these methods should be as described below.

    check_points

    This method does not have any parameters (besides self) and returns None. check_points should check if there are at least 100 points, in which case biscuits should be set to True.

    winner

    This method does not have any parameters (besides self) and returns a str. If the winning team is "UNC" and the losing team is "Dook", then the string "GTHD!!" should be returned. In the case that UNC wins, but Duke is not the opposing team, winner should return "woohoo". If UNC doesn’t win, return "daggum".

    reset_points

    This method does not have any parameters (besides self) and returns an int. reset_points should return the current number of points, and reset the points to be 0.

Exercise 01 – Animal Kingdom

  1. Create a file named animal_kingdom.py in your comp110/exercises/ex09_classes folder.
  2. Add the following import statements directly below your module level docstring. You’ll get a syntax error if you put them below your __author__ variable.
from __future__ import annotations
from typing import List

Note: The first import statement is most likely unfamiliar. The purpose of this statement is to allow the use of a class as a type inside of itself.

  1. Write a class called Animal with the following specifications:
    1. Each Animal should have a str attribute called species, an int attribute called danger_level, and a str attribute called emoji.
    2. Your Animal class should have a constructor that takes in species, danger_level and emoji.
    3. Your class should also have 1 method calledfight. The functionality of this method should be as described below:

    fight

    This method takes in an Animal parameter named opponent (and self) and returns Animal.
    • fight should return the animal with the higher danger_level.
    • If they have the same danger_level then the opponent should win (be returned).
  2. Write a class called Team with the following specifications:
    1. Each Team should have a str attribute called team_name, an List[Animal] attribute called animals, an int attribute called score.
    2. Your Team class should have a constructor that takes in team_name and animals and initializes score to 0.
    3. Your class should also have 2 methods called battle and who_won. The functionality of these methods should be as described below:

    battle

    This method takes in a Team parameter named opponent (and self) and returns List[Animal]. battle should:
    • Make sure the animals list in self and opponent are of equal size and return an empty list if not.
    • Iterate through the animals lists in self and opponent and make use of the fight method in Animal to match up the animals at equivalent indices. For instance self.animals[0] should fight opponent.animals[0].
    • Increase the scor` attribute of the correct team when one of their fighters wins.
    • Return a list that contains the winners of each fight. For instance the winner of self.animals[0] vs opponent.animals[0] should be at index 0 of the return list.

    who_won

    This method takes in a Team parameter named opponent (and self) and returns str. who_won should:
    • Return “The battle hasn’t happened yet” if both scores are 0
    • Return “It was a tie!” if both scores are equal but not 0
    • Return “Team <team_object.team_name> won!” using which ever team has the higher score.
  3. Copy and paste the following main function add the bottom of your file:

The expected output should be as following:

Hello Kitty vs BIG
🦁  vs 🐘
The lion wins!
🐏  vs 🦍
The gorilla wins!
🐷  vs 🐪
The camel wins!
Team BIG won!
  1. For full credit, create at least two more Animal objects in main and add them to the two Team objects until the printed output of who_won is
It was a tie!

A list of unicodes for different emojis can be found here: https://unicode.org/emoji/charts/full-emoji-list.html

Formatting and Documentation

  1. You should have a module docstring with a complete first sentence describing your progam at the start of each exercise.
  2. In each of your files, initialize a global variable named author set equal to your PID as a string

Submission

Go ahead and delete any submission zips lingering around in your workspace from the previous exercise.

When you are ready to submit for grading, close out any open Python Debug Console terminals using the Trash Can and then open up a clean, new terminal.

python -m tools.submission comp110/exercises/ex09_classes

This should produce a submission timestamped with the current date/time for uploading on Gradescope.

Make a Backup Commit

  1. Open the Source Control panel (Command Palette: “Show SCM” or click the icon with three circles and lines on the activity panel).
  2. Notice the files listed under Changes. These are files you’ve made modifications to since your last backup.
  3. Move your mouse’s cursor over the word Changes and notice the + symbol that appears. Click that plus symbol to add all changes to the next backup. You will now see the files listed under “Staged Changes”.
    • If you do not want to backup all changed files, you can select them individually. For this course you’re encouraged to back everything up.
  4. In the Message box, give a brief description of what you’ve changed and are backing up. This will help you find a specific backup (called a “commit”) if needed. In this case a message such as, “Finished Exercise 0!!” will suffice.
  5. Press the Check icon to make a Commit (a version) of your work.
  6. Finally, press the Ellipses icon (…) and select “Push” to send this backed up version to your workspace repository space on GitHub.