Permalink
Browse files

Add homework 08

  • Loading branch information...
fredngo committed Nov 21, 2019
0 parents commit ec0a79841f14a5f8a008f0fb5e95d5c4a1847037
Showing with 252 additions and 0 deletions.
  1. +159 −0 README.md
  2. +49 −0 exercise1.py
  3. +44 −0 exercise2.py
159 README.md
@@ -0,0 +1,159 @@
# Assignment: Object-Oriented Programming - Class Variables and Methods

In this assignment, you'll practice:

* `if/else` statements
* Lists and iteration
* Defining classes and instantiating objects/instances
* Defining and using instance variables
* Defining and calling instance methods
* Inheritance

This assignment can be challenging! Feel free to collaborate with other students on this assignment, but you should still hand in your own assignment.

## Deliverables & Submitting

You know what you're doing by now! **Fork**/**Clone**/**Commit**/**Push**/**Submit** :grin:

---

# Exercise 1: Insert Coin(s)

The objective of this exercise is to write a simple game, where the player can **collect treasure** and **do battle**. Every time the player collects 10 gold coins, they should **level up**. But, if they lose all their health points, the game restarts.

Each player needs to keep track of the following pieces of data:

* The player's **level** -- The player should start at level 1
* The player's **health** -- The player should start with 10 health points
* Number of **gold coins** in the player's possession -- The player should start with 0 coins

Some starter code is already in the file named `exercise1.py`. Your job is to fill in all of the instance methods of `Player` and make the game actually work.

Take a look now at `exercise1.py` and familiarize yourself with the layout of the starter code. Notice the placeholder `???`s -- you need to replace them with real code!

**Note**: Do **not** add any new functions or methods! Just fill out the `???`s.

Your job:

1. Implement the `__init__` method
* What parameter(s) do you need? Replace `???` with the required parameter(s)
* What instance variables do you need to work with?
* Recall that at the beginning of the game, the player should start at a level of 1, health of 10, and 0 gold coins
1. Implement the `__str__` method
* What parameter(s) do you need? Replace `???` with the required parameter(s)
* Analyze the example output to figure out how you need to format the string representation of the player.
1. Implement the `level_up` method
* What parameter(s) do you need? Replace `???` with the required parameter(s)
* What should this method do? What instance variable(s) does it need to modify?
1. Implement the `collect_treasure` method
* What parameter(s) do you need? Replace `???` with the required parameter(s)
* What should this method do? What instance variable(s) does it need to modify?
* Recall that every time the player collects 10 coins, the player should level up!
* **Hint:** The player should not lose any of the collected coins when they level up! It's theirs to keep... unless they die!
1. Implement the `restart` method
* What parameter(s) do you need? Replace `???` with the required parameter(s)
* What does it mean to "restart" the game anyway?
* What instance variable(s) does this method need to modify?
* **STRETCH:** Finally, after you finish this method -- do you see any repetition of code? Is there a way to write this method without repeating yourself?
1. Implement the `do_battle` method
* What parameter(s) do you need? Replace `???` with the required parameter(s)
* What should this method do? What instance variable(s) does it need to modify?
* Recall that if the player loses all of their health points, the game should restart!
1. Finally, look in the `main` function and fill in the one and only `???` with the correct code, to instantiate a new player.

### Expected Output

Assuming that you have implemented everything correctly, this is the expected output when you run `exercise1.py`:

```
You are at level 1, have 0 gold, and 10 health points
You are at level 1, have 9 gold, and 10 health points
You are at level 2, have 10 gold, and 10 health points
You are at level 3, have 20 gold, and 10 health points
You are at level 3, have 20 gold, and 1 health points
You are at level 1, have 0 gold, and 10 health points
```

---

# Exercise 2: Solar System

Next, let's explore our Solar System!

For this exercise, some starter code is already in the file named `exercise2.py`. Take a look at it now and familiarize yourself with the layout of the starter code.

Your job is to use your knowledge of **inheritance** and **instance methods** to fill in all of the `???` areas of the code and make the program actually run.

**Note**: Do **not** add any new functions or methods! Just fill out the `???`s.

Let's start with the basics:

* A **Celestial Body** is a naturally occurring physical entity like a Star, Planet, or a Moon
* A [Planetary System](https://en.wikipedia.org/wiki/Planetary_system), like our own [Solar System](https://en.wikipedia.org/wiki/Solar_System), can contain several of these celestial bodies

We can model the different types of celestial bodies as follows:

1. Each `Body`, no matter which kind (star, planet, or moon) has to keep track of two pieces of information -- Its `name` and `mass`
* The `name` of a `Body` should be a string
* The `mass` of a `Body` should be an integer
* **Hint:** Use `instance variables` to keep track of these!
* Your job is to implement the `__init__` method of the `Body` class
1. Each `Star` (which is a kind of `Body`), has an additional piece of information to keep track of: Its `type`
* The `type` of a `Star` should be a string
* **Hint:** Stars also have `name`s and a `mass`! But you need to use inheritance to keep track of a Star's name and mass. Remember that a `Star` is a `Body`!
* Your job is to:
* Use Inheritance to deal with the `name` and `mass` of Stars,
* Implement the rest of the `__init__` method of the `Star` class
1. Next, each `Planet` is also a kind of `Body`
* Planets, in addition to having a `name` and `mass`, also has a `day_length` and a `year_length`
* The `day_length` should be a integer and it represents the number of hours in a day (For example: `24` for the Earth)
* The `year_length` should also be an integer and it represents the number of days in a year (For example: `365` for the Earth)
* Your job is to:
* Use Inheritance again to deal with the `name` and `mass` of Planets,
* Implement the rest of the `__init__` method of the `Planet` class
1. Finally, each `Moon` is also a kind of `Body`
* Moons, in addition to having a `name` and `mass`, also has a `month_length` and a `orbit_planet`
* The `month_length` should be a integer and it represents the number of days in a month (For example: `30` for the Earth's moon - Luna)
* The `orbit_planet` should be an instance of the planet that the moon orbits around!
* For example, Luna orbits the Earth
* **Hint:** This means the Earth must exist before its moon can be created!
* Your job is to:
* Use Inheritance again to deal with the `name` and `mass` of Moons,
* Implement the rest of the `__init__` method of the `Moon` class

Next, we can model our Solar System with a top-level class, let's call it `System`, which can contain as many celestial bodies as needed.

* A `System` should start with no bodies inside of it
* What kind of instance variables do you need to take care of multiple bodies?
* Implement the `__init__` method of `System` class to reflect this
* It should have the ability to `add` bodies to itself
* Implement the `add` method of `System`, to accept some `body`
* This `body` should be added to the System's already existing collection of bodies
* Finally, a `System` can report on the `total_mass` of all the celestial bodies inside the system.
* The return value of the `total_mass` method should the sum of all the masses of the bodies inside the system
* **Note**: A `System` is **not** a `Body`! Instead, it *contains* bodies.

Finally, fill out the parts of the `main` function, in order to instantiate a `sun`, an `earth`, a `luna`, and a `solar_system`.

* For the `sun`, Use the name `'Sun'`, mass of `200`, and `type` of `'G-Type'`
* For the `earth`, Use the name `'Earth'`, mass of `150`, `day_length` of `24`, and `year_length` of `365`
* For `luna`, Use the name `'Luna'`, mass of `100`, `month_length` of `30`, and `orbit_planet` of `earth`

### Expected Output

Assuming that you have implemented everything correctly, this is the expected output when you run `exercise2.py`:

```
Earth
Sun
Luna
450
```

---

# Food for Thought

Isn't it fun to explore unknown new stuff?

![](https://media.giphy.com/media/26zyYdiV4pdZZUWEU/source.gif)
@@ -0,0 +1,49 @@
class Player:

def __init__(???):
???

def __str__(???):
???

def level_up(???):
???

def collect_treasure(???):
???

def restart(???):
???

def do_battle(???):
???

def main():
# Start the game
player_1 = ???
print(player_1)

# Testing that the player can collect_treasure 9 times
for i in range(9):
player_1.collect_treasure()
print(player_1)

# This final treasure should cause the player to level up
player_1.collect_treasure()
print(player_1)

# The player should level up again
for i in range(10):
player_1.collect_treasure()
print(player_1)

# Testing do_battle
player_1.do_battle(9)
print(player_1)

# This final damage should cause a restart of the game
player_1.do_battle(1)
print(player_1)

# Run the game
main()
@@ -0,0 +1,44 @@
class Body:
def __init__(???):
???

class Star(???):
def __init__(???):
???

class Planet(???):
def __init__(???):
???

class Moon(???):
def __init__(???):
???

class System:
def __init__(???):
???

def add(???):
???

def total_mass(???):
???

def main():
# Create the solar system and its bodies
sun = ??? # Use the name 'Sun', mass of 200, and type of 'G-Type'
earth = ??? # Use the name 'Earth', mass of 150, day_length of 24, and year_length of 365
luna = ??? # Use the name 'Luna', mass of 100, month_length of 30, and orbit_planet of earth
solar_system = ???
solar_system.add(earth)
solar_system.add(sun)
solar_system.add(luna)

# Print each body in the solar system to see if they have been added correctly
for body in solar_system.bodies:
print(body.name)

# Checking to see that total mass is correct
print(solar_system.total_mass())

main()

0 comments on commit ec0a798

Please sign in to comment.