Classes and Constructors Homework
Categories: Javascript HomeworkClasses and Constructors Homework
JavaScript Classes and Constructors Homework
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.
Popcorn Hack 1
- The class TennisPlayer has been defined for you. Create a constructor with the arguements name, rank, and rankPoints.
- Call the class with the arguments Novak Djokovic, 1, 16000.
- Add one or more of the following arguments to the initial constructor: age, tournamentsPlayed, titlesWon. Add this as part of the profile output.
%% javascript
class TennisPlayer {
constructor(name, rank, rankPoints, age) {
this.name = name;
this.rank = rank;
this.rankPoints = rankPoints;
this.age = age;
};
profile() {
console.log("Hi my name is " + this.name + ", my rank is " + this.rank + " and I have " + this.rankPoints + " ranking points." + " I am " + this.age + " years old.");
}
};
const player1 = new TennisPlayer("Novak Djokovic", 1, 16000, 38);
player1.profile();
<IPython.core.display.Javascript object>
Popcorn Hack 2
- Create a class called library
- Within library create a class called book with a constructors that allows the two methods - add book and remove book
- Add another inner class called computers - and have it output the number of computers on
Below is the starter code to get you started
%% javascript
// Step 1: Create the main class called Library
class Library {
constructor(name) {
this.name = name;
console.log(`Welcome to the`, this.name, `Library!`);
}
// Step 2: Create an inner class called Book
static Book = class {
constructor() {
this.books = [Harry Potter, The Great Gatsby, Diary of a Wimpy Kid];
}
addBook(title) {
this.books.push(title);
console.log(`Added "${title}" to the library.`);
}
removeBook(title) {
const index = this.books.indexOf(title);
if (index > -1) {
this.books.splice(index, 1);
console.log(`Removed "${title}" from the library.`);
} else {
console.log(`"${title}" not found in the library.`);
}
}
// Step 3: Create another inner class called Computers
static Computers = class {
// TODO: Create a constructor that takes computersOn as an argument\
constructor(computersOn) {
this.computersOn = computersOn;
}
showComputersOn() {
console.log(`There are currently ${this.computersOn} computers on.`);
}
// TODO: Create a method that outputs the number of computers on
}
}
// --- Example Usage ---
// Uncomment after writing your constructors!
const myLibrary = new Library("Downtown");
const bookManager = new Library.Book();
const techRoom = new Library.Computers(8);
bookManager.addBook("The Hobbit");
bookManager.addBook("1984");
bookManager.removeBook("The Hobbit");
techRoom.showComputersOn();
<IPython.core.display.Javascript object>
Homework
Create and expand the Cookie Clicker project:
- Fill out the cookies and cookiesPerClick variables.
- Define what should happen upon clicking the cookie.
- Create an
Upgrade
class that multiplies cookies per click, and expand the original cookieclicker class to integrate upgardes. - Print how each upgrade changes the total cookie output.
- Add a cookie type variable which sets the specific type of cookie (ex: Chocolate chip, Oatmeal, etc.) The following code is to help you get started.
Extra credit: Up to 0.03 points
- 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.
%% javascript
class Upgrade {
constructor(name, multiplier) {
this.name = name;
this.multiplier = multiplier;
}
}
class CookieClicker {
constructor(cookies, cookiesPerClick, cookieType) {
this.cookies = cookies;
this.cookiesPerClick = cookiesPerClick;
this.cookieType = cookieType;
}
click() {
this.cookies += this.cookiesPerClick;
console.log(`You have ${this.cookies} ${this.cookieType} cookies.`);
}
applyUpgrade(upgrade) {
this.cookiesPerClick *= upgrade.multiplier;
console.log(`Upgrade applied: ${upgrade.name}. Cookies per click is now ${this.cookiesPerClick}.`);
}
}
let game = new CookieClicker(0, 1, "Chocolate Chip");
game.click();
let upgrade1 = new Upgrade("Golden Oven", 2);
game.applyUpgrade(upgrade1);
game.click();
let upgrade2 = new Upgrade("Mega Mixer", 3);
game.applyUpgrade(upgrade2);
game.click();
This is where you will find homework: Github Homework Link