Classes and Methods

Popcorn Hack 1 - Dice Roller

Instructions: Finish the code in the cell below (fill in underlines and add lines of code) so it works as described:

  • Create a class called Dice that:
    • Has a property for the number of sides.
    • Has a method roll() that returns a random number between 1 and the number of sides.

At the bottom, test your game by making a DiceGame object and calling roll() a few times.

The code is currently NOT functional. When you have completed this popcorn hack, the code should work. Have fun!

%%javascript

class _____ { // What should the name of the class be?
  constructor(sides) { // Constructor to initialize the number of sides
    this.sides = sides; // Number of sides on the dice
  }

  // ____() { // What should the name of the method be?
    return Math.floor(Math.random() * _________) + 1; // Rolls the dice and returns a number between 1 and the number of sides
  }


// Test the Dice class:
// INSTANTIATE A NEW DICE OBJECT WITH A NUMBER OF SIDES

// CALL THE ROLL METHOD A FEW TIMES AND PRINT THE RESULTS
<IPython.core.display.Javascript object>

Popcorn Hack 2 - Pet Simulator

This hack is a bit more complicated than the first. Points will be awarded based on effort and completion.

Analyze the block of code below. Then, complete the following instructions:

1) Create a simple Pet class by filling in the _____.

2) Add at least 2 properties (like hunger, energy, or happiness). EX: Stats decrease over time.

3) Add at least two functioning methods (such as feed, play, or sleep) EX: Buttons increase stats.

4) Give your pet a name and print their current properties.

5) Bonus: Add a “status” method to print all your pet’s stats.

The code is currently NOT functional. When you have completed this popcorn hack, the code should work. Have fun!

%%javascript

class _______ {
  constructor(name) {
    this.name = name;
    this.hunger = 5;   // you can change these values
    this.energy = 5;
    this.happiness = 5;
  }

  // TODO: Write at least 2 methods here
  //Example:
  // feed()
  //____________________________ // allow user to feed pet (add button to call this method)
  //____________________________ // increase energy, decrease hunger, increase happiness
  //____________________________ // print that the pet has been fed to console
  // play()
  //____________________________ // allow user to play w/ pet by clicking a button to call this method
  //____________________________ // decrease energy, increase hunger, increase happiness
  //____________________________ // print that the pet has been played with to console
}

const myPet = new pet("___________"); // give your pet a name
console.log(______________________); // print the properties of your pet to console
output.innerText = "____________________________"; // print the properties of your pet

// Test your methods below:
myPet.feed();
myPet.play();

Homework

Complete the following questions in the next code block.

1) Create a class Person with properties ‘name’ and ‘age’. Print their properties.

2) Add a method greet() to your Person class that prints a greeting using the person’s name.

3) Create a class Book with properties ‘title’, ‘author’, and ‘pages’. Instantiate a book and print its properties.

4) Add a method read(pages) to your Book class that increments a pagesRead property and prints a message.

5) Add a method isLongBook() that returns true if pages > 300, otherwise false. Print the result for your book.

6) Create a class Car with properties ‘make’, ‘model’, and ‘fuel’. Add methods drive(distance) and refuel(amount). Print messages in each method.

7) Add a static method info() to your Car class that prints ‘Cars are vehicles.’

8) Create a class ElectricCar that extends Car. Add a property ‘battery’ and a method charge(amount) that increases battery. Print battery level after charging.

10) Create a routine that combines your objects: drive a Car, charge an ElectricCar, read pages from a Book, and call greet() on Person. Print outputs for each action.

11) Extra Credit: Submit the original OOP Breakout Game Code (found on OpenCS) after making the following edits:

  • Add another feature to the game: Add a method so that the ball must collide with some bricks twice for a brick to break completely.
  • Edit a method in the ball class to increase/decrease the speed of the ball: Please add a COMMENT on the game code that specifies which line you edited.
%%javascript

console.log("Code, code, code!");
<IPython.core.display.Javascript object>