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
- Create a
Vanilla
CodeSandbox and name it "JavaScript Classes Lab 3." - Follow along with the instructions below, adding to your
index.js
file. - 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 commandgit 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 thename
from a parameter in the constructor method.price
- integer, set as15
.
-
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
- setname
from a parameter in the constructor method.age
- initially 0height
- initially 0weight
- initially 0mood
- integer starting at 0 initiallyhamsters
- empty array initiallybankAccount
- initially set to 0
-
Methods:
getName()
- returnsname
.getAge()
- returnsage
.getWeight()
- returnsweight
.greet()
- logs a message with person'sname
.eat()
- incrementweight
, incrementmood
.exercise()
- decrementweight
.ageUp()
- incrementage
, incrementheight
, incrementweight
, decrementmood
, incrementbankAccount
by 10 (birthday money).buyHamster(hamster)
- push thehamster
object onto thehamsters
array, incrementmood
by 10, decrementbankAccount
by the value of thehamster
(hint: usegetPrice()
).
🔴 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.
- Instantiate a new
Person
named Timmy. - Age Timmy five years.
- 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.
- 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.
- Age Timmy 9 more years.
- Create a
Hamster
named "Gus." - Set Gus's owner to the string "Timmy."
- Have Timmy "buy" Gus.
- Age Timmy more 15 years.
- Have Timmy eat twice.
- 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 ofDinner
.- Add a constructor to
Dinner
that sets the string properties:appetizer
,entree
, anddessert
. - Add a method on
Chef
that takes three arguments and returns a newDinner
based on those arguments. - Have the
Chef
create threeDinner
s, log theDinner
s.
🔴 Hard Mode - Save & Commit your work!
Your commit message should read something like: "dinner is served".
🟢 Normal Mode - Make sure it works so far.