CSE1030 Week 03 Homework Problems

Mixing Static and Non-static Features

Short Problems

1. Many kinds of video games require the player to manage some kind of finite resource (such as food or gold). Implement a class named Gold that represents quantities (weight) of mined gold. The class defines a total reserve of gold that is available to be mined. Once the reserve is exhausted only gold objects having zero weight can be created.

Your class should implement the following API. Test your class by writing a main method that creates gold objects until the reserves are exhausted; confirm that any gold objects created after the reserves are exhausted actually have zero weight.

 

2. One possible use of the singleton pattern is to keep track of the game state in a video game. As a simple example, implement a singleton class that keeps track of the score in a game where the score is stored as an int value.

Your class should implement the following API. Test your class by writing a main method that begins as shown below. Use both s and t to modify the score using increase and decrease; check that the score is correctly modified regardless of which reference you use.

  public static void main(String[] args) {
    GameScore s = GameScore.getInstance();
    GameScore t = GameScore.getInstance();
  }

 

Moderate Problems

1. One possible use of the multiton pattern is to manage the mapping of student numbers to CSE computer accounts. For example, when a student first requests a CSE account using their student number, the multiton would check if that student number had already been used to create an account; if not, then a new account would be created, otherwise, the existing account corresponding to the student number would be returned. Similar examples would include mapping user names to email accounts, mapping user names to shopping accounts, and mapping passport numbers to people.

Create a multiton class that manages any of the above scenarios. Formulate a plan to test your class and execute your plan.

 

2. An old practice labtest. You need to implement the class Vector3d described by the API.

 

3. Another old practice labtest. You need to implement the class Hamster described by the API.

 

4. Another old practice labtest. You need to implement the class Book described by the API. The unique identifier for the book can be generated using the static counter technique described in the notes and lectures.

 

Long/Creative Problems

None this week.