CS 151 - Day 16

Cover page image

Cay S. Horstmann

Day 16 Clicker Question 1

Which of these define a deposit method? Check all that apply

  1. function deposit(account, amount) {
      account.balance += amount
    }
  2. let account = {
      balance: 0,
      deposit: function(amount) { this.balance += amount }
    }
  3. let account = {
      balance: 0,
      deposit(amount) { this.balance += amount }
    }
  4. let account = {
      balance: 0,
      deposit: amount => { this.balance += amount }
    }

Day 16 Clicker Question 2

When using the class syntax to define a JavaScript class, where are the methods stored? Check all that apply.

  1. In each object
  2. In the prototype object
  3. In the constructor
  4. In the class

Day 16 Clicker Question 3

In the “hard objects” technique, where are instance variables stored? Check all that apply.

  1. As local variables of the constructor
  2. As parameter variables of the constructor
  3. As fields of a JavaScript object
  4. As name/value pairs of a map

Lab

lab

A Node Class

  1. Implement a class for circle nodes. Use the “hard objects” technique. Start with a factory function createCircleNode(x, y, size, color) that returns an object with a single method getBounds. The getBounds method should return an object { x: ..., y: ..., width: ..., height: ... }. (Look into ch08/graphed2/CircleNode.java for guidance.)
  2. Write a test case for the getBounds method.
  3. Add a contains method that accepts a parameter p of the form { x: ..., y: ...}. The condition is (x + size / 2 - p.x) ** 2 + (y + size / 2 - p.y) ** 2 <= size ** 2 / 4
  4. Write a test case for the contains method.

A Graph Class

  1. Implement a graph class, using the class syntax. Provide a constructor that produces an empty graph.
  2. Add an add method that adds a node, exactly like in ch08/graphed/Graph.java
  3. Add a findNode method. Unlike lab 15, call contains on the nodes.
  4. Produce a test case for findNode.

Polymorphism

  1. Add square nodes, either with hard objects (createSquareNode) or with the class syntax.
  2. Implement getBounds and contains.
  3. Enhance the test case for findNode to a graph with a circle node and a square node.
  4. findNode calls contains on two node types. Is that polymorphism?