|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "raw", |
| 5 | + "id": "f46d5103", |
| 6 | + "metadata": { |
| 7 | + "vscode": { |
| 8 | + "languageId": "raw" |
| 9 | + } |
| 10 | + }, |
| 11 | + "source": [ |
| 12 | + "---\n", |
| 13 | + "layout: post\n", |
| 14 | + "title: Classes and Constructors Homework\n", |
| 15 | + "description: Classes and Constructors Homework\n", |
| 16 | + "categories: [Javascript, Homework]\n", |
| 17 | + "permalink: /classes-constructors-HW\n", |
| 18 | + "author: Makers\n", |
| 19 | + "---" |
| 20 | + ] |
| 21 | + }, |
| 22 | + { |
| 23 | + "cell_type": "markdown", |
| 24 | + "id": "b0f73322", |
| 25 | + "metadata": {}, |
| 26 | + "source": [ |
| 27 | + "## <u>JavaScript Classes and Constructors Homework<u>\n", |
| 28 | + "By now you should have a decent grasp of classes and constructors, and how to make and build one. The following exercises should help you solidify your understanding of classes and constructors in JavaScript." |
| 29 | + ] |
| 30 | + }, |
| 31 | + { |
| 32 | + "cell_type": "markdown", |
| 33 | + "id": "4be7bd0a", |
| 34 | + "metadata": { |
| 35 | + "vscode": { |
| 36 | + "languageId": "javascript" |
| 37 | + } |
| 38 | + }, |
| 39 | + "source": [ |
| 40 | + "## Popcorn Hack 1\n", |
| 41 | + "1. The class TennisPlayer has been defined for you. Create a constructor with the arguements name, rank, and rankPoints.\n", |
| 42 | + "2. Call the class with the arguments Novak Djokovic, 1, 16000. \n", |
| 43 | + "3. Add one or more of the following arguments to the initial constructor: age, tournamentsPlayed, titlesWon. Add this as part of the profile output." |
| 44 | + ] |
| 45 | + }, |
| 46 | + { |
| 47 | + "cell_type": "code", |
| 48 | + "execution_count": null, |
| 49 | + "id": "7399ab93", |
| 50 | + "metadata": { |
| 51 | + "vscode": { |
| 52 | + "languageId": "javascript" |
| 53 | + } |
| 54 | + }, |
| 55 | + "outputs": [], |
| 56 | + "source": [ |
| 57 | + "%%javascript\n", |
| 58 | + "\n", |
| 59 | + "\n", |
| 60 | + "class TennisPlayer {\n", |
| 61 | + " constructor(name, rank, rankPoints,age) {\n", |
| 62 | + " \n", |
| 63 | + " this.name = name;\n", |
| 64 | + " this.rank = rank;\n", |
| 65 | + " this.rankPoints = rankPoints;\n", |
| 66 | + " this.age = age;\n", |
| 67 | + " }\n", |
| 68 | + " profile () {\n", |
| 69 | + " console.log(\"Hi my name is \" + this.name + \", my rank is \" + this.rank + \", I have \" + this.rankPoints + \" ranking points\" + \", and I am \" + this.age + \" years old.\");\n", |
| 70 | + " }\n", |
| 71 | + "\n", |
| 72 | + "};\n", |
| 73 | + "\n", |
| 74 | + "\n", |
| 75 | + "let player1 = new TennisPlayer(\"Novak Djokovic\", 1, 16000, 38);\n", |
| 76 | + "player1.profile();" |
| 77 | + ] |
| 78 | + }, |
| 79 | + { |
| 80 | + "cell_type": "markdown", |
| 81 | + "id": "847cedc1", |
| 82 | + "metadata": {}, |
| 83 | + "source": [ |
| 84 | + "## Popcorn Hack 2\n", |
| 85 | + "\n", |
| 86 | + "1. Create a class called library\n", |
| 87 | + "2. Within library create a class called book with a constructors that allows the two methods - add book and remove book\n", |
| 88 | + "3. Add another inner class called computers - and have it output the number of computers on\n", |
| 89 | + "\n", |
| 90 | + "Below is the starter code to get you started" |
| 91 | + ] |
| 92 | + }, |
| 93 | + { |
| 94 | + "cell_type": "code", |
| 95 | + "execution_count": null, |
| 96 | + "id": "e7bad2b3", |
| 97 | + "metadata": { |
| 98 | + "vscode": { |
| 99 | + "languageId": "javascript" |
| 100 | + } |
| 101 | + }, |
| 102 | + "outputs": [], |
| 103 | + "source": [ |
| 104 | + "%%javascript\n", |
| 105 | + "// Step 1: Create the main class called Library\n", |
| 106 | + "class Library {\n", |
| 107 | + " constructor(name) {\n", |
| 108 | + " this.name = name;\n", |
| 109 | + " console.log(`Welcome to the`, this.name, `Library!`);\n", |
| 110 | + " }\n", |
| 111 | + "\n", |
| 112 | + " // Step 2: Create an inner class called Book\n", |
| 113 | + " static Book = class {\n", |
| 114 | + " constructor() {\n", |
| 115 | + " this.books = []; \n", |
| 116 | + " }\n", |
| 117 | + " //create a constructor with that takes the this.books argument.\n", |
| 118 | + "\n", |
| 119 | + " addBook(title) {\n", |
| 120 | + " this.books.push(title);\n", |
| 121 | + " console.log(`Added \"${title}\" to the library.`);\n", |
| 122 | + " }\n", |
| 123 | + "\n", |
| 124 | + " removeBook(title) { \n", |
| 125 | + " const index = this.books.indexOf(title);\n", |
| 126 | + " if (index > -1) {\n", |
| 127 | + " this.books.splice(index, 1);\n", |
| 128 | + " console.log(`Removed \"${title}\" from the library.`);\n", |
| 129 | + " } else {\n", |
| 130 | + " console.log(`\"${title}\" not found in the library.`);\n", |
| 131 | + " }\n", |
| 132 | + " }\n", |
| 133 | + " }\n", |
| 134 | + "\n", |
| 135 | + " // Step 3: Create another inner class called Computers\n", |
| 136 | + " static Computers = class {\n", |
| 137 | + " constructor(computersOn) {\n", |
| 138 | + " this.computersOn = computersOn; \n", |
| 139 | + " }\n", |
| 140 | + " showComputersOn() {\n", |
| 141 | + " console.log(`There are ${this.computersOn} computers currently on.`); \n", |
| 142 | + " }\n", |
| 143 | + " }\n", |
| 144 | + "}\n", |
| 145 | + "\n", |
| 146 | + "// --- Example Usage ---\n", |
| 147 | + "\n", |
| 148 | + "\n", |
| 149 | + " const myLibrary = new Library(\"Downtown\");\n", |
| 150 | + " const bookManager = new Library.Book();\n", |
| 151 | + " const techRoom = new Library.Computers(8);\n", |
| 152 | + "\n", |
| 153 | + " bookManager.addBook(\"The Hobbit\");\n", |
| 154 | + " bookManager.addBook(\"1984\");\n", |
| 155 | + " bookManager.removeBook(\"The Hobbit\");\n", |
| 156 | + " techRoom.showComputersOn();\n" |
| 157 | + ] |
| 158 | + }, |
| 159 | + { |
| 160 | + "cell_type": "markdown", |
| 161 | + "id": "24d1fadb", |
| 162 | + "metadata": {}, |
| 163 | + "source": [ |
| 164 | + "## Homework\n", |
| 165 | + "Create and expand the Cookie Clicker project:\n", |
| 166 | + "1. Fill out the cookies and cookiesPerClick variables.\n", |
| 167 | + "2. Define what should happen upon clicking the cookie.\n", |
| 168 | + "3. Create an `Upgrade` class that multiplies cookies per click, and expand the original cookieclicker class to integrate upgardes.\n", |
| 169 | + "5. Print how each upgrade changes the total cookie output.\n", |
| 170 | + "6. Add a cookie type variable which sets the specific type of cookie (ex: Chocolate chip, Oatmeal, etc.)\n", |
| 171 | + "The following code is to help you get started.\n", |
| 172 | + "\n", |
| 173 | + "#### Extra credit: Up to 0.03 points\n", |
| 174 | + "- Create a new cell, apply the ALL of the above changes to a blank cookie clicker project, and submit that code for the cookie clicker project." |
| 175 | + ] |
| 176 | + }, |
| 177 | + { |
| 178 | + "cell_type": "code", |
| 179 | + "execution_count": null, |
| 180 | + "id": "e9c84d6f", |
| 181 | + "metadata": { |
| 182 | + "vscode": { |
| 183 | + "languageId": "javascript" |
| 184 | + } |
| 185 | + }, |
| 186 | + "outputs": [], |
| 187 | + "source": [ |
| 188 | + "%%javascript\n", |
| 189 | + "\n", |
| 190 | + "class CookieClicker {\n", |
| 191 | + " constructor(cookies,cookiesPerClick, cookieType) {\n", |
| 192 | + "\n", |
| 193 | + " //Part 1.cookies and cookiesPerClick variables\n", |
| 194 | + " this.cookies = cookies; //start value\n", |
| 195 | + " this.cookiesPerClick = cookiesPerClick;\n", |
| 196 | + " this.cookieType = cookieType; //Step 5. Create cookie types\n", |
| 197 | + " }\n", |
| 198 | + "\n", |
| 199 | + " click() {\n", |
| 200 | + " //Part 2. Define what happens when cookie is clicked\n", |
| 201 | + " \n", |
| 202 | + " this.cookies += this.cookiesPerClick;\n", |
| 203 | + " console.log(\"You clicked a cookie!\");\n", |
| 204 | + " console.log(\"You clicked a \" + this.cookieType + \" cookie!\"); //Part 5. Type of cookie you clicked\n", |
| 205 | + " console.log(\"You have \" + this.cookies + \" cookies.\");\n", |
| 206 | + " }\n", |
| 207 | + "\n", |
| 208 | + " //Part 3. Create a upgrade class that mutliplies cokkies per click\n", |
| 209 | + " applyUpgrade(upgrade) {\n", |
| 210 | + " this.cookiesPerClick *= upgrade.multiplier;\n", |
| 211 | + " // Part 4: Show change in cookie output\n", |
| 212 | + " console.log(\"Upgrade applied! Multiplier: \" + upgrade.multiplier);\n", |
| 213 | + " this.showStatus(); \n", |
| 214 | + " }\n", |
| 215 | + "\n", |
| 216 | + " showStatus() {\n", |
| 217 | + " console.log(\"Each click gives \" + this.cookiesPerClick + \" cookies. Total: \" + this.cookies);\n", |
| 218 | + " }\n", |
| 219 | + "}\n", |
| 220 | + "\n", |
| 221 | + "class Upgrade {\n", |
| 222 | + " constructor(multiplier) {\n", |
| 223 | + " this.multiplier = multiplier;\n", |
| 224 | + " }\n", |
| 225 | + "}\n", |
| 226 | + "\n", |
| 227 | + "\n", |
| 228 | + "console.log(\"Starting CookieClicker Game...\");\n", |
| 229 | + "const game = new CookieClicker(0, 1, \"Chocolate chip\");\n", |
| 230 | + "console.log(\"Initial setup complete.\\n\");\n", |
| 231 | + "\n", |
| 232 | + "\n", |
| 233 | + "console.log(\"Clicking cookies...\");\n", |
| 234 | + "game.click();\n", |
| 235 | + "game.click();\n", |
| 236 | + "console.log(\"First round complete.\\n\");\n", |
| 237 | + "\n", |
| 238 | + "\n", |
| 239 | + "console.log(\" Applying upgrade...\");\n", |
| 240 | + "const upgrade = new Upgrade(2);\n", |
| 241 | + "game.applyUpgrade(upgrade);\n", |
| 242 | + "console.log(\"Upgrade applied.\\n\");\n", |
| 243 | + "\n", |
| 244 | + "\n", |
| 245 | + "console.log(\"Clicking cookies after upgrade...\");\n", |
| 246 | + "game.click();\n", |
| 247 | + "console.log(\"Final click complete.\\n\");\n", |
| 248 | + "\n", |
| 249 | + "\n", |
| 250 | + "console.log(\" Final Game Status:\");\n", |
| 251 | + "game.showStatus();" |
| 252 | + ] |
| 253 | + }, |
| 254 | + { |
| 255 | + "cell_type": "markdown", |
| 256 | + "id": "0180a89f", |
| 257 | + "metadata": {}, |
| 258 | + "source": [ |
| 259 | + "This is where you will find homework: [Github Homework Link](https://github.com/Open-Coding-Society/pages/tree/main/_notebooks/CSSE/JavaScriptLessons/Classes_and_Methods)" |
| 260 | + ] |
| 261 | + } |
| 262 | + ], |
| 263 | + "metadata": { |
| 264 | + "kernelspec": { |
| 265 | + "display_name": "venv", |
| 266 | + "language": "python", |
| 267 | + "name": "python3" |
| 268 | + }, |
| 269 | + "language_info": { |
| 270 | + "name": "python", |
| 271 | + "version": "3.12.3" |
| 272 | + } |
| 273 | + }, |
| 274 | + "nbformat": 4, |
| 275 | + "nbformat_minor": 5 |
| 276 | +} |
0 commit comments