|
| 1 | +# Class Attributes and Methods Lab |
| 2 | + |
| 3 | +## Learning Goals |
| 4 | + |
| 5 | +- Use class attributes and methods to write durable and powerful code. |
| 6 | + - Store and access song data using class attributes and methods. |
| 7 | +- Accomplish complex programming tasks using knowledge from previous modules. |
| 8 | + |
| 9 | +*** |
| 10 | + |
| 11 | +## Key Vocab |
| 12 | + |
| 13 | +- **Attribute**: variables that belong to an object. |
| 14 | +- **Constant**: variable whose value cannot be changed. |
| 15 | +- **Instance**: one specific working copy of a class. It is created when a |
| 16 | + class's `__init__` method is called. |
| 17 | +- **Class**: a bundle of data and functionality. Can be copied and modified to |
| 18 | + accomplish a wide variety of programming tasks. |
| 19 | +- **Static**: an attribute or method that cannot manipulate the class or |
| 20 | + instance it belongs to. |
| 21 | +- **Exception**: an error that occurs during the execution of a program. |
| 22 | + Exceptions can be anticipated and handled without disrupting the execution of |
| 23 | + the program. |
| 24 | + |
| 25 | +*** |
| 26 | + |
| 27 | +## Introduction |
| 28 | + |
| 29 | +In this lab, we'll be dealing with a `Song` class. The `Song` class can produce |
| 30 | +individual songs. Each song has a name, an artist and a genre. We need our |
| 31 | +`Song` class to be able to keep track of the number of songs that it creates. |
| 32 | + |
| 33 | +```ruby |
| 34 | +Song.count |
| 35 | +# => 30 |
| 36 | +``` |
| 37 | + |
| 38 | +We need our `Song` class to be able to show us all of the artists of existing |
| 39 | +songs: |
| 40 | + |
| 41 | +```ruby |
| 42 | +Song.artists |
| 43 | +# => ["Jay-Z", "Drake", "Beyonce"] |
| 44 | +``` |
| 45 | + |
| 46 | +We need our `Song` class to be able to show us all of the genres of existing |
| 47 | +songs: |
| 48 | + |
| 49 | +```ruby |
| 50 | +Song.genres |
| 51 | +# => ["Rap", "Pop"] |
| 52 | +``` |
| 53 | + |
| 54 | +We also need our `Song` class to be able to keep track of the number of songs of |
| 55 | +each genre it creates. |
| 56 | + |
| 57 | +In other words, calling: |
| 58 | + |
| 59 | +```ruby |
| 60 | +Song.genre_count |
| 61 | +``` |
| 62 | + |
| 63 | +Should return something like this; |
| 64 | + |
| 65 | +```ruby |
| 66 | +{"rap" => 5, "rock" => 1, "country" => 3} |
| 67 | +``` |
| 68 | + |
| 69 | +Lastly, we want our `Song` class to reveal to us the number of songs each artist |
| 70 | +is responsible for. |
| 71 | + |
| 72 | +```ruby |
| 73 | +Song.artist_count |
| 74 | +# => {"Beyonce" => 17, "Jay-Z" => 40} |
| 75 | +``` |
| 76 | + |
| 77 | +We'll accomplish this with the use of **class variables** and **class methods**. |
| 78 | + |
| 79 | +## Instructions |
| 80 | + |
| 81 | +Define your `Song` class such that an individual song is initialized with a |
| 82 | +name, artist and genre. |
| 83 | + |
| 84 | +There should be an `attr_accessor` for those three attributes. |
| 85 | + |
| 86 | +```ruby |
| 87 | +ninety_nine_problems = Song.new("99 Problems", "Jay-Z", "rap") |
| 88 | + |
| 89 | +ninety_nine_problems.name |
| 90 | +# => "99 Problems" |
| 91 | + |
| 92 | +ninety_nine_problems.artist |
| 93 | +# => "Jay-Z" |
| 94 | + |
| 95 | +ninety_nine_problems.genre |
| 96 | +# => "rap" |
| 97 | +``` |
| 98 | + |
| 99 | +Create a class variable, `@@count`. We will use this variable to keep track of |
| 100 | +the number of new songs that are created from the `Song` class. Set this |
| 101 | +variable equal to `0`. |
| 102 | + |
| 103 | +At what point should we increment our `@@count` of songs? Whenever a new song is |
| 104 | +created. Your `#initialize` method should use the `@@count` variable and |
| 105 | +increment the value of that variable by `1`. |
| 106 | + |
| 107 | +Next, define the following class methods: |
| 108 | + |
| 109 | +`Song.count`: returns the total number of songs created. |
| 110 | + |
| 111 | +`Song.genres`: returns an array of all of the genres of existing songs. This |
| 112 | +array should contain only _unique genres_ — no duplicates! Think about what |
| 113 | +you'll need to do to get this method working: |
| 114 | + |
| 115 | +- You'll need a class variable, let's call it `@@genres`, that is equal to an |
| 116 | + empty array. |
| 117 | +- When should you add genres to the array? Whenever a new song is created. |
| 118 | + Your `#initialize` method should add the genre of the song being created to |
| 119 | + the `@@genres` array. All genres should be added to the array. Control for |
| 120 | + duplicates when you code your `.genres` class method, not when you add |
| 121 | + genres to the original `@@genres` array. We will want to know how many songs |
| 122 | + of each genre have been created. We'll revisit that job a little later on. |
| 123 | + |
| 124 | +`Song.artists`: returns an array of all of the artists of the existing |
| 125 | +songs. This array should only contain unique artists––no repeats! Once again |
| 126 | +think about what you need to do to implement this behavior. |
| 127 | + |
| 128 | +- You'll need a class variable, let's call it `@@artists`, that is equal to an |
| 129 | + empty array. |
| 130 | +- When should you add artists to this array? Whenever a new song is |
| 131 | + initialized. Your `#initialize` method should add artists to the `@@artists` |
| 132 | + array. All artists should be added to the array. Control for duplicates when |
| 133 | + you code your `.artists` class method, not when you add artists to the |
| 134 | + original `@@artists` array. We will want to know how many songs each have |
| 135 | + been assigned to each artist. We'll revisit that job a little later on when |
| 136 | + we write our `.artist_count` method. |
| 137 | + |
| 138 | +`Song.genre_count`: returns a hash in which the keys are the names of each |
| 139 | +genre. Each genre name key should point to a value that is the number of songs |
| 140 | +that have that genre. |
| 141 | + |
| 142 | +```ruby |
| 143 | +Song.genre_count |
| 144 | + # => {"rap" => 5, "rock" => 1, "country" => 3} |
| 145 | +``` |
| 146 | + |
| 147 | +This manner of displaying numerical data is called a |
| 148 | +[histogram](https://en.wikipedia.org/wiki/Histogram). How will you create your |
| 149 | +histogram? There are a few ways! |
| 150 | + |
| 151 | +- You can need to iterate over the `@@genres` array and populate a hash with the |
| 152 | + key/value pairs. You will need to check to see if the hash already contains a |
| 153 | + key of a particular genre. If so, increment the value of that key by one, |
| 154 | + otherwise, create a new key/value pair. |
| 155 | +- You can also look into the [`#tally`][tally docs] method. |
| 156 | + |
| 157 | +`Song.artist_count`: returns a histogram similar to the one above, but for |
| 158 | +artists rather than genres. |
| 159 | + |
| 160 | +## Resources |
| 161 | + |
| 162 | +- [`#tally`][tally docs] |
| 163 | + |
| 164 | +[tally docs]: https://ruby-doc.org/core-2.7.0/Enumerable.html#method-i-tally |
| 165 | + |
| 166 | + |
| 167 | +*** |
| 168 | + |
| 169 | +## Resources |
| 170 | + |
| 171 | +- [Python Documentation](https://docs.python.org/3/) |
| 172 | +- [Classes - Python](https://docs.python.org/3/) |
| 173 | +- [Python Class Attributes: An Overly Thorough Guide - Toptal](https://www.toptal.com/python/python-class-attributes-an-overly-thorough-guide) |
| 174 | +- [Python's Instance, Class, and Static Methods Demystified - Real Python](https://realpython.com/instance-class-and-static-methods-demystified/) |
| 175 | +- [The Factory Method Pattern and Its Implementation in Python - Real Python](https://realpython.com/factory-method-python/) |
0 commit comments