Quick Index
Introduction


Overview


A drill to learn about static factory methods.

Objectives



Prerequisites


For reference while doing drills:


Hint -- click the "Try It" buttons on the examples to experiment with the example pseudocode (modify it with your own ideas).

Requirements


The requirements for this drill are:


Deliverables


Your deliverables for this drill:


Provided Code


In this zip file you will find the starter file "static-factory-methods-drill.txt":


Notes on Problems



Drill Problems


Problem 1 - Circle
Write pseudocode for a class named Circle. The class has one ivar named radius (an integer).

Preparation:

  • The class should include a getter and setter for the ivar radius.
  • The class should include the method toString.

Add the following static factory methods:

  • A method fromRadius(radius) that returns a new Circle using the passed param radius.
  • A method fromDiameter(diam) that returns a new Circle using the passed param diam.
Problem 2 - Game
Write pseudocode for a class named Game. The class has two ivars named playerCount and level (both integers).

Preparation:

  • The class should include a getter and setter for each ivar.
  • The class should include the method toString.

Add the following static factory methods:

  • A method fromPlayers(playerCount) that returns a new Game using the passed param playerCount and with level set to 5
  • A method fromLevel(level) that returns a new Game using the passed param level and with playerCount set to 2
Problem 3 - Discussion
Write a narrative discussion about the Circle and Game constructors you coded.

Discuss how you would implement standard constructors, e.g., "new Circle(4)" to support the two different ways that are required to construct these objects.

When considering the two problems, what are the advantages or disadvantages of static factory methods vs standard constructors?

This can be simple discussion -- a couple short paragraphs.