ALAB 308A.2.3 - Classes and Factories


Learning Objectives

After this lab, learners will have demonstrated the ability to:

  • Create complex JavaScript classes.
  • Create methods to set and modify class properties.
  • Create methods that allow objects to interact with other objects.
  • Create a factory.

 CodeSandbox

This lab uses CodeSandbox as one of its tools.

If you are unfamiliar with CodeSandbox, or need a refresher, please visit our reference page on CodeSandbox for instructions on:

  • Creating an Account
  • Making a Sandbox
  • Navigating your Sandbox
  • Submitting a link to your Sandbox to Canvas

Instructions

  1. Create a Vanilla CodeSandbox and name it "JavaScript Classes Lab 3."
  2. Follow along with the instructions below, adding to your index.js file.
  3. Submit the link to your CodeSandbox on Canvas when you are finished.

Note: There is a prompt below each section. "Hard Mode" is intended to reinforce your understanding of git.

  • For 🔴 Hard Mode it will tell you to save and commit your work.
  • For 🟢 Normal Mode it will tell you to save and run your code in CodeSandbox.

🔴 Hard Mode Setup (practice command line and recall git):

  • mkdir a project folder.
  • cd into that folder and run the command git init.
  • Create a file called lab.js.
  • Open the file in VS Code.
  • Do your lab in this file.
  • Run your code periodically by opening the terminal/hyper/VS Code terminal and running node lab.js.
  • If for some reason you have an issue and you cannot do this successfully, it's fine. You can get help at office hours. Submit your lab with CodeSandbox; this is just a bonus. We'll be practicing this workflow in class during the upcoming weeks.
  • Follow the prompts to commit your work with git add -A && git commit -m "Message Here".
  • When you are done with your lab, make an empty repository on github.com and then go through the steps to push your local git repository.
  • When you are finished, submit the link to your GitHub repo to Canvas.
  • If at any point you get stuck and can't get unstuck, copy and paste your code to CodeSandbox and submit that.
  • Git issues are not an acceptable reason to not turn in this assignment, the git work is a bonus.

Deliverables

  • A link to a CodeSandbox or GitHub repository that contains your completed lab with no errors (comment things out if they do not work).

Hamster Class

Create a Hamster class with the following requirements met:

  • Attributes:

    • owner - string, initially set as an empty string.
    • name - string, set the name from a parameter in the constructor method.
    • price - integer, set as 15.
  • Methods:

    • wheelRun() - log "squeak squeak".
    • eatFood() - log "nibble nibble".
    • getPrice() - return the price.

🔴 Hard Mode - Save & Commit your work!
Your commit message should read something like: "created Hamster class".
🟢 Normal Mode - Make sure it works so far.


Person Class

Create a Person class with the following requirements met:

  • Attributes:

    • name - set name from a parameter in the constructor method.
    • age- initially 0
    • height - initially 0
    • weight - initially 0
    • mood - integer starting at 0 initially
    • hamsters - empty array initially
    • bankAccount - initially set to 0
  • Methods:

    • getName() - returns name.
    • getAge() - returns age.
    • getWeight() - returns weight.
    • greet() - logs a message with person's name.
    • eat() - increment weight, increment mood.
    • exercise() - decrement weight.
    • ageUp() - increment age, increment height, increment weight, decrement mood, increment bankAccount by 10 (birthday money).
    • buyHamster(hamster) - push the hamster object onto the hamsters array, increment mood by 10, decrement bankAccount by the value of the hamster (hint: use getPrice()).

🔴 Hard Mode - Save & Commit your work!
Your commit message should read something like: "created Person class".
🟢 Normal Mode - Make sure it works so far.


Create a Story

Feel free to update or add methods to automate some of these tasks.

  1. Instantiate a new Person named Timmy.
  2. Age Timmy five years.
  3. At this point, Timmy's a little bummed. As a precocious child, he feels he's "seen it all" already. Have him eat five times to improve his mood.
  4. Now Timmy's a little heavier than he wants to be. Kindergarten's coming up and he wants to look good. Have him exercise five times.
  5. Age Timmy 9 more years.
  6. Create a Hamster named "Gus."
  7. Set Gus's owner to the string "Timmy."
  8. Have Timmy "buy" Gus.
  9. Age Timmy more 15 years.
  10. Have Timmy eat twice.
  11. Have Timmy exercise twice.

🔴 Hard Mode - Save & Commit your work!
Your commit message should read something like: "created Timmy's story".
🟢 Normal Mode - Make sure it works so far.


Chefs Make Dinners

Using the following two classes, we'll create a factory (Chefs are effectively dinner factories, after all!).

class Dinner {

}

class Chef {

}

Fill in these classes to meet the following requirements:

  • Chef should be a factory of Dinner.
  • Add a constructor to Dinner that sets the string properties: appetizer, entree, and dessert.
  • Add a method on Chef that takes three arguments and returns a new Dinner based on those arguments.
  • Have the Chef create three Dinners, log the Dinners.

🔴 Hard Mode - Save & Commit your work!
Your commit message should read something like: "dinner is served".
🟢 Normal Mode - Make sure it works so far.

Copyright © Per Scholas 2024