From 8a19c1d9a229efe94d6a88043269756eaad7ac79 Mon Sep 17 00:00:00 2001 From: Parallaxes Date: Mon, 4 Nov 2024 09:28:24 -0800 Subject: [PATCH] Finished JS Classes & Methods Lesson --- .../2024-10-27-classes-intro.ipynb | 20 +- .../2024-10-27-methods-in-depth.ipynb | 44 ++- .../2024-11-04-classes-hw.ipynb | 186 +++++++++++ .../2024-11-04-static-variables.ipynb | 310 ++++++++++++++++++ 4 files changed, 538 insertions(+), 22 deletions(-) create mode 100644 _notebooks/CSSE/Lessons/Classes_and_Methods/2024-11-04-classes-hw.ipynb create mode 100644 _notebooks/CSSE/Lessons/Classes_and_Methods/2024-11-04-static-variables.ipynb diff --git a/_notebooks/CSSE/Lessons/Classes_and_Methods/2024-10-27-classes-intro.ipynb b/_notebooks/CSSE/Lessons/Classes_and_Methods/2024-10-27-classes-intro.ipynb index e945a193..213b29c5 100644 --- a/_notebooks/CSSE/Lessons/Classes_and_Methods/2024-10-27-classes-intro.ipynb +++ b/_notebooks/CSSE/Lessons/Classes_and_Methods/2024-10-27-classes-intro.ipynb @@ -1,15 +1,23 @@ { "cells": [ { - "cell_type": "markdown", - "metadata": {}, + "cell_type": "code", + "execution_count": null, + "metadata": { + "vscode": { + "languageId": "yaml" + } + }, + "outputs": [], "source": [ "---\n", + "comments: true\n", "layout: post\n", - "title: Javascript Classes and Methods \n", - "description: Popcorn hack 1 \n", - "categories: [JavaScript]\n", - "comments: True\n", + "title: JavaScript Classes & Methods Introduction\n", + "description: An introduction to JavaScript Classes & Methods\n", + "author: Lucas Masterson\n", + "permalink: /csse/javascript/fundamentals/classes/intro/\n", + "categories: [JavaScript Fundamentals]\n", "---" ] }, diff --git a/_notebooks/CSSE/Lessons/Classes_and_Methods/2024-10-27-methods-in-depth.ipynb b/_notebooks/CSSE/Lessons/Classes_and_Methods/2024-10-27-methods-in-depth.ipynb index 06b11be5..b08bc458 100644 --- a/_notebooks/CSSE/Lessons/Classes_and_Methods/2024-10-27-methods-in-depth.ipynb +++ b/_notebooks/CSSE/Lessons/Classes_and_Methods/2024-10-27-methods-in-depth.ipynb @@ -1,5 +1,26 @@ { "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "vscode": { + "languageId": "yaml" + } + }, + "outputs": [], + "source": [ + "---\n", + "comments: true\n", + "layout: post\n", + "title: JavaScript Methods\n", + "description: An in-depth look at JavaScript methods.\n", + "author: Alex Van Linge & Lucas Masterson (reviewer)\n", + "permalink: /csse/javascript/fundamentals/classes/methods/\n", + "categories: [JavaScript Fundamentals]\n", + "---" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -10,7 +31,7 @@ "Now that we have some of the basics out of the way, now it is time to go into methods in greater detail and depth than what was mentioned in the intro.\n", "\n", "## Defining Methods \n", - "Methods in JavaScript are essentially functions inside of objects. Methods are defined on the prototype of each class and shared by all instances." + "Methods in JavaScript are essentially functions inside of objects. To refresh your memory, objects are anything with Key-Value Pairs inside of them (ex: Name: \"Alex\"), and functions are blocks of code that perform specific tasks or actions, like console.log." ] }, { @@ -68,7 +89,7 @@ "source": [ "## Accessor Methods\n", "\n", - "A variant of methods that can be particularly useful in many scenarios are `Accessor Methods`. Accessor methods are functions defined within a class that allow you to retrieve (access) the values of the object's properties without modifying them. \n", + "A variant of methods that can be particularly useful in many scenarios are `Accessor Methods`. Accessor methods are functions defined within an object that allow you to retrieve (access) the values of the object's properties without modifying them. \n", "\n", "Accessor methods make debugging code much easier and just make the code as a whole more cohesive and consistent.\n" ] @@ -189,13 +210,6 @@ "// person.setAge(-5); // Output: \"Please enter a valid age.\"" ] }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Generally speaking, you won't use generator or async at an intro level, so we can skip them as of now. If you're interested, you can find the JavaScript MDN Web Docs on methods." - ] - }, { "cell_type": "markdown", "metadata": {}, @@ -204,7 +218,7 @@ "\n", "For the popcorn hack, use this code cell below and create an object that must include these three things \n", "\n", - " - A constructor to build class instances\n", + " - Four Key-Value Pairs that at least include your name, age, and two others of your choosing\n", " - Accessor methods (At least 2)\n", " - Mutator methods (At least 2 as well) \n", " " @@ -225,13 +239,13 @@ "// Have Fun!\n", "\n", "class Person {\n", - " // Define the constructor here\n", + " // Define Key-Value Pairs\n", "\n", "\n", - " // Method of choosing can go here\n", + " // Methods of choosing can go here\n", "\n", "\n", - " // Method of choosing can go here\n", + " // Methods of choosing go here\n", "\n", "\n", "}\n", @@ -258,9 +272,7 @@ "2. W3Schools.com. https://www.w3schools.com/js/js_classes.asp\n", "\n", "****\n", - "*Building The Future And Keeping The Past Alive Are One In The Same Thing.*\n", - "\n", - "*Anything that can go wrong will go wrong.*" + "*Building The Future And Keeping The Past Alive Are One In The Same Thing.*" ] } ], diff --git a/_notebooks/CSSE/Lessons/Classes_and_Methods/2024-11-04-classes-hw.ipynb b/_notebooks/CSSE/Lessons/Classes_and_Methods/2024-11-04-classes-hw.ipynb new file mode 100644 index 00000000..3f535f27 --- /dev/null +++ b/_notebooks/CSSE/Lessons/Classes_and_Methods/2024-11-04-classes-hw.ipynb @@ -0,0 +1,186 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "vscode": { + "languageId": "yaml" + } + }, + "outputs": [], + "source": [ + "---\n", + "comments: true\n", + "layout: post\n", + "title: JavaScript Classes & Methods Homework\n", + "description: The JavaScript Classes & Methods Homework assignment for the JavaScript Fundamentals course.\n", + "author: Lucas Masterson\n", + "permalink: /csse/javascript/fundamentals/classes/hw/\n", + "categories: [JavaScript Fundamentals]\n", + "---" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Classes & Methods Homework\n", + "****" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Homework!\n", + "\n", + "As you read through this, notice the new content introduced. We encourage you to do your own research (this can be Google, ChatGPT, documentation, etc.). Here, we introduce the `_` property (memory safety), `extends` keyword, and `super()` method.\n", + "\n", + "To complete the homework, implement the `TODO:` comments. The example usage should guide you and work if all implementation is done correctly. Have fun! \n", + "\n", + "*If anything simply cannot go wrong, it will anyway.*" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "vscode": { + "languageId": "javascript" + } + }, + "outputs": [ + { + "data": { + "application/javascript": "\nclass Aircraft {\n constructor(model, capacity, range) {\n this._model = model;\n this._capacity = capacity;\n this._range = range;\n }\n\n // Getter for model\n get model() {\n return this._model;\n }\n\n // Setter for model\n set model(newModel) {\n this._model = newModel;\n }\n\n // Getter for capacity\n get capacity() {\n return this._capacity;\n }\n\n // Setter for capacity\n set capacity(newCapacity) {\n this._capacity = newCapacity;\n }\n\n // Getter for range\n get range() {\n return this._range;\n }\n\n // Setter for range\n set range(newRange) {\n this._range = newRange;\n }\n\n // Method to display aircraft details\n displayDetails() {\n return `Model: ${this._model}, Capacity: ${this._capacity}, Range: ${this._range} km`;\n }\n\n // Static method\n static compareRange(aircraft1, aircraft2) {\n return aircraft1.range - aircraft2.range;\n }\n}\n\n// Example usage\nlet boeing = new Aircraft('Boeing 747', 416, 13800);\nlet airbus = new Aircraft('Airbus A380', 853, 15700);\n\nconsole.log(boeing.displayDetails());\nconsole.log(airbus.displayDetails());\nconsole.log(`Range difference: ${Aircraft.compareRange(boeing, airbus)} km`);\nclass FighterJet extends Aircraft {\n constructor(model, capacity, range, speed) {\n super(model, capacity, range);\n this._speed = speed;\n }\n\n // Getter for speed\n get speed() {\n return this._speed;\n }\n\n // Setter for speed\n set speed(newSpeed) {\n this._speed = newSpeed;\n }\n\n // Method to display fighter jet details\n displayDetails() {\n return `Model: ${this._model}, Capacity: ${this._capacity}, Range: ${this._range} km, Speed: ${this._speed} km/h`;\n }\n}\n\n// Example usage\nlet f16 = new FighterJet('F-16', 1, 4220, 2400);\nlet f22 = new FighterJet('F-22', 1, 2960, 2410);\n\nconsole.log(f16.displayDetails());\nconsole.log(f22.displayDetails());\nconsole.log(`Range difference: ${Aircraft.compareRange(f16, f22)} km`);\n", + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "%%js\n", + "\n", + "class Aircraft {\n", + " // Constructor\n", + " constructor(model, capacity, range) {\n", + " this._model = model; // _model is a convention to indicate that it's a private property\n", + " this._capacity = capacity; // _capacity is a convention to indicate that it's a private property\n", + " this._range = range; // _range is a convention to indicate that it's a private property\n", + " }\n", + "\n", + " // Getter for model\n", + " get model() {\n", + " return this._model;\n", + " }\n", + "\n", + " // Setter for model\n", + " set model(newModel) {\n", + " this._model = newModel;\n", + " }\n", + "\n", + " // Getter for capacity\n", + " get capacity() {\n", + " return this._capacity;\n", + " }\n", + "\n", + " // Setter for capacity\n", + " set capacity(newCapacity) {\n", + " this._capacity = newCapacity;\n", + " }\n", + "\n", + " // Getter for range\n", + " get range() {\n", + " // TODO: get the range\n", + " }\n", + "\n", + " // Setter for range\n", + " set range(newRange) {\n", + " // TODO: set the range\n", + " }\n", + "\n", + " // Method to display aircraft details\n", + " displayDetails() {\n", + " // TODO: display the details (model, capacity, range)\n", + " }\n", + "\n", + " // Static method to compare range\n", + " static compareRange(aircraft1, aircraft2) {\n", + " return aircraft1.range - aircraft2.range;\n", + " }\n", + "}\n", + "\n", + "// Example usage\n", + "let boeing = new Aircraft('Boeing 747', 416, 13800);\n", + "let airbus = new Aircraft('Airbus A380', 853, 15700);\n", + "\n", + "console.log(boeing.displayDetails());\n", + "console.log(airbus.displayDetails());\n", + "console.log(`Range difference: ${Aircraft.compareRange(boeing, airbus)} km`);\n", + "\n", + "// New content in the HW! Lo and behold, the FighterJet class! \n", + "// For 1) do research. What does the extend keyword do in JavaScript?\n", + "// Here's a quick rundown if you're in a hurry: \n", + "// the extends keyword creates a child class (FighterJet) that inherits from a parent class (Aircraft).\n", + "class FighterJet extends Aircraft {\n", + " constructor(model, capacity, range, speed) {\n", + " super(model, capacity, range); // We call the parent class constructor with super\n", + " this._speed = speed;\n", + " }\n", + "\n", + " // TODO: Getter for speed\n", + " // Remember to use the _speed property for memory safety\n", + "\n", + " // TODO: Setter for speed\n", + " // Remember to use the _speed property and add a function paramater.\n", + "\n", + " // TODO: Method to display fighter jet details (model, capacity, range, speed)\n", + "}\n", + "\n", + "// Example usage\n", + "let f16 = new FighterJet('F-16', 1, 4220, 2400);\n", + "let f22 = new FighterJet('F-22', 1, 2960, 2410);\n", + "\n", + "console.log(f16.displayDetails());\n", + "console.log(f22.displayDetails());\n", + "console.log(`Range difference: ${Aircraft.compareRange(f16, f22)} km`);" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "****\n", + "\n", + "*If you perceive that there are four possible ways in which a procedure can go wrong, and circumvent these, then a fifth way, unprepared for, will promptly develop.*\n", + "\n", + "*You have no purpose because you fear to seek one. That fear is your failure. Your fear brings you pain. We know pain. Our purpose is its end.*" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.3" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/_notebooks/CSSE/Lessons/Classes_and_Methods/2024-11-04-static-variables.ipynb b/_notebooks/CSSE/Lessons/Classes_and_Methods/2024-11-04-static-variables.ipynb new file mode 100644 index 00000000..9725038a --- /dev/null +++ b/_notebooks/CSSE/Lessons/Classes_and_Methods/2024-11-04-static-variables.ipynb @@ -0,0 +1,310 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "vscode": { + "languageId": "yaml" + } + }, + "outputs": [], + "source": [ + "---\n", + "comments: true\n", + "layout: post\n", + "title: JavaScript Static Variables\n", + "description: An introduction to JavaScript Static Variables\n", + "author: Ethan Fu & Lucas Masterson (reviewer)\n", + "permalink: /csse/javascript/fundamentals/classes/statics/\n", + "categories: [JavaScript Fundamentals]\n", + "---" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Static Variables\n", + "****\n", + "\n", + "The keyword ``static`` can be used to define static methods and properties, which apply to entire classes rather than its instances (AKA your objects). Think of these as global variables for classes. Below is an example (taken from online) of how static methods and properties work.\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "vscode": { + "languageId": "javascript" + } + }, + "outputs": [ + { + "data": { + "application/javascript": "\nclass ClassWithStaticMethod {\n static staticProperty = 'someValue';\n static staticMethod() {\n return 'static method has been called.';\n }\n static {\n console.log('Class static initialization block called');\n }\n}\n\nconsole.log(ClassWithStaticMethod.staticProperty);\n// Expected output: \"someValue\"\nconsole.log(ClassWithStaticMethod.staticMethod());\n// Expected output: \"static method has been called.\"\n", + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "%%js\n", + "\n", + "class ClassWithStaticMethod {\n", + " static staticProperty = 'someValue';\n", + " static staticMethod() {\n", + " return 'static method has been called.';\n", + " }\n", + " static {\n", + " console.log('Class static initialization block called');\n", + " }\n", + "}\n", + "\n", + "console.log(ClassWithStaticMethod.staticProperty);\n", + "// Expected output: \"someValue\"\n", + "console.log(ClassWithStaticMethod.staticMethod());\n", + "// Expected output: \"static method has been called.\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Why use static variables?\n", + "****\n", + "\n", + "Static variables can be used for multiple things, but two of the most common use cases are counting the number of instances of a class object there are and defining global constants throughout a class.\n", + "\n", + "### Example 1\n", + "\n", + "Below is an example of counting class instances using static variables:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "vscode": { + "languageId": "javascript" + } + }, + "outputs": [ + { + "data": { + "application/javascript": "\nclass User {\n static instanceCount = 0; //static variable, starts with a value of zero\n\n constructor(name) {\n this.name = name;\n User.instanceCount++; //increments the instanceCount when a new user is entered\n }\n\n static getInstanceCount() {\n return User.instanceCount; //returns the updated instanceCount variable, which has now been incremented\n }\n}\n\nconst user1 = new User(\"Ethan\");\nconst user2 = new User(\"Lucas\");\nconst user3 = new User(\"Alex\")\n\nconsole.log(\"Number of users: \" + User.getInstanceCount()); // Expected output: \"Number of users: 3\"\n", + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "%%js\n", + "\n", + "class User {\n", + " static instanceCount = 0; //static property, starts with a value of zero\n", + "\n", + " constructor(name) {\n", + " this.name = name;\n", + " User.instanceCount++; //increments the instanceCount when a new user is entered\n", + " }\n", + "\n", + " static getInstanceCount() {\n", + " return User.instanceCount; //static method, returns the updated instanceCount variable, which has now been incremented\n", + " }\n", + "}\n", + "\n", + "const user1 = new User(\"Ethan\");\n", + "const user2 = new User(\"Lucas\");\n", + "const user3 = new User(\"Alex\")\n", + "\n", + "console.log(\"Number of users: \" + User.getInstanceCount()); // Expected output: \"Number of users: 3\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Explanation\n", + "\n", + "We use instanceCount to start the inital value of the amount of objects there are, then we increment that value each time an object is created using 'User.instanceCount++'. Finally, we use a static method to return the updated instanceCount so it can be updated again later on.\n", + "\n", + "If did not use static variables, the value of instanceCount would be \"reset\" each time we made a new object, so we would not be able to count them.\n", + "\n", + "### Example 2\n", + "\n", + "Below is an example of creating and using a global constant for multiple objects using static variables:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "vscode": { + "languageId": "javascript" + } + }, + "outputs": [ + { + "data": { + "application/javascript": "\nclass Product {\n static TAX_RATE = 0.10; // Static variable for tax rate (10%)\n \n constructor(name, price) {\n this.name = name; // instance property\n this.price = price; // instance property\n }\n \n // Instance method to calculate the price including tax\n calculatePriceWithTax() {\n return this.price * (1 + Product.TAX_RATE);\n }\n }\n \n // Creating individual product instances\n const product1 = new Product(\"Laptop\", 1200);\n const product2 = new Product(\"Smartphone\", 800);\n \n console.log(\"Name of product 1: \" + product1.name); // Output: Laptop\n console.log(\"Name of product 2: \" + product2.name); // Output: Smartphone\n\n // Using an instance method to calculate prices with tax\n console.log(\"Product 1 price pre-tax: \" + product1.price); // Output: 1200\n console.log(\"Product 2 price pre-tax: \" + product2.price); // Output: 800\n\n // Using an instance method to calculate prices with tax\n console.log(\"Product 1 price with tax: \" + product1.calculatePriceWithTax()); // Output: 1296 (1200 + 10% tax)\n console.log(\"Product 2 price with tax: \" + product2.calculatePriceWithTax()); // Output: 864 (800 + 10% tax)\n \n // Accessing the static variable directly from the class\n console.log(\"Tax rate: \" + Product.TAX_RATE); // Output: 0.10\n \n", + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "%%js\n", + "\n", + "class Product {\n", + " static TAX_RATE = 0.10; // Static variable for tax rate (10%)\n", + " \n", + " constructor(name, price) {\n", + " this.name = name; // instance property\n", + " this.price = price; // instance property\n", + " }\n", + " \n", + " // Instance method to calculate the price including tax\n", + " calculatePriceWithTax() {\n", + " return this.price * (1 + Product.TAX_RATE);\n", + " }\n", + " }\n", + " \n", + " // Creating individual product instances\n", + " const product1 = new Product(\"Laptop\", 1200);\n", + " const product2 = new Product(\"Smartphone\", 800);\n", + " \n", + " console.log(\"Name of product 1: \" + product1.name); // Output: Laptop\n", + " console.log(\"Name of product 2: \" + product2.name); // Output: Smartphone\n", + "\n", + " // Using an instance method to calculate prices with tax\n", + " console.log(\"Product 1 price pre-tax: \" + product1.price); // Output: 1200\n", + " console.log(\"Product 2 price pre-tax: \" + product2.price); // Output: 800\n", + "\n", + " // Using an instance method to calculate prices with tax\n", + " console.log(\"Product 1 price with tax: \" + product1.calculatePriceWithTax()); // Output: 1320 (1200 + 10% tax)\n", + " console.log(\"Product 2 price with tax: \" + product2.calculatePriceWithTax()); // Output: 880 (800 + 10% tax)\n", + " \n", + " // Accessing the static variable directly from the class\n", + " console.log(\"Tax rate: \" + Product.TAX_RATE); // Output: 0.10" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Explanation\n", + "\n", + "The product class contains a static property TAX_RATE that is assigned a value of 0.10, a constructor that defines the properties of a created object from the given class, and a static method that applies the tax on the price value of the object to get the final price.\n", + "\n", + "Here, static variables are given a certain value, then it is applied to all instances of the class. This makes the code simpler and more organized than they would have been otherwise.\n", + "****\n", + "\n", + "## Popcorn Hack 3\n", + "\n", + "Try to use static variables to both count the number of class instances you have and apply a multiplier on " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "vscode": { + "languageId": "javascript" + } + }, + "outputs": [ + { + "data": { + "application/javascript": "\nclass Value {\n static MULTIPLIER = 1.5; // Static variable for tax rate (8%)\n static totalNumber = 0; // Static variable to track the total number of products\n\n constructor(value) {\n this.value = value; // instance property\n Value.totalNumber++; // Increment total product count every time a new product is created\n }\n\n // Instance method to calculate the price including tax\n calculateValueWithMultiplier() {\n return this.value * (Value.MULTIPLIER);\n }\n\n // Static method to get the total number of products\n static getTotalNumbers() {\n return Value.totalNumber;\n }\n}\n\n// Creating individual product instances\nconst value1 = new Value(5);\nconst value2 = new Value(10)\n\nconsole.log(\"Value 1: \" + value1.value);\nconsole.log(\"Value 2: \" + value2.value);\n\n// Using an instance method to calculate prices with tax\nconsole.log(\"Value 1 with multiplier: \" + value1.calculateValueWithMultiplier()); // Output: 1296 (1200 + 8% tax)\nconsole.log(\"Value 2 with multiplier: \" + value2.calculateValueWithMultiplier()); // Output: 864 (800 + 8% tax)\n\n// Accessing the static variable directly from the class\nconsole.log(\"Multiplier: \" + Value.MULTIPLIER); // Output: 1.5\n\n// Accessing the static method to get the total number of products\nconsole.log(\"Total number of values: \" + Value.getTotalNumbers()); // Output: 2\n", + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "%%js\n", + "\n", + "class Value {\n", + " static MULTIPLIER = 1.5; // Static variable for tax rate (8%)\n", + " static totalNumber = 0; // Static variable to track the total number of products\n", + "\n", + " constructor(value) {\n", + " this.value = value; // instance property\n", + " // TODO: Increment total product count every time a new product is created\n", + " }\n", + "\n", + " // Instance method to calculate the price including tax\n", + " calculateValueWithMultiplier() {\n", + " // TODO: return Value * MULTIPLIER\n", + " }\n", + "\n", + " // Static method to get the total number of products\n", + " static getTotalNumbers() {\n", + " // TODO: return totalNumber\n", + " }\n", + "}\n", + "\n", + "// Creating individual product instances\n", + "const value1 = new Value(5);\n", + "const value2 = new Value(10)\n", + "\n", + "console.log(\"Value 1: \" + value1.value);\n", + "console.log(\"Value 2: \" + value2.value);\n", + "\n", + "// Using an instance method to calculate prices with tax\n", + "console.log(\"Value 1 with multiplier: \" + value1.calculateValueWithMultiplier()); // Output: 1296 (1200 + 8% tax)\n", + "console.log(\"Value 2 with multiplier: \" + value2.calculateValueWithMultiplier()); // Output: 864 (800 + 8% tax)\n", + "\n", + "// Accessing the static variable directly from the class\n", + "console.log(\"Multiplier: \" + Value.MULTIPLIER); // Output: 1.5\n", + "\n", + "// Accessing the static method to get the total number of products\n", + "console.log(\"Total number of values: \" + Value.getTotalNumbers()); // Output: 2" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "****\n", + "\n", + "*If there is a possibility of several things going wrong, the one that will cause the most damage will be the one to go wrong. Corollary: If there is a worse time for something to go wrong, it will happen then.*" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.12" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}