Descriptions class assignment

From WLCS

Objective

  • You will learn to create a class comprised of variables and methods
  • You will learn to create arrays
  • You will learn to access arrays

Purpose

  • The Descriptions class will allow us to generate a random adjective to describe a room ("dark", "haunted", etc.) and a random room type ("living room", "parlor", etc.), as well as random weapon names ("katana", "sai", etc.) and monster names ("goblin", "troll", etc.).

Resources

Directions

  1. Download Media:Descriptions.java
  2. Examine the code in Media:Descriptions.java
    • There are two arrays: the first is the list of adjectives, and the second is the list of room types
    • The third instance variable is a Random object, used to create random numbers
    • The default constructor is there, but does not need to do anything
    • The other two methods (getNextAdjective() and getNextRoomType()) require completion for this part of the lab
  3. Add a few values to each of the arrays in Media:Descriptions.java (minimum of 5 each).
  4. Next, complete the getNextRoomAdjective() method. It currently returns null, but should return one of the adjectives in the roomAdjectives array, chosen at random. Thus, you need to generate an appropriate random number (via the nextInt() method in the Random class), then return that element in the roomAdjectives array.
  5. Similarly, the getNextRoomType() method should do the same for the roomTypes array. Note that the two arrays may be of different sizes, so be sure not to mix them up in your code.
  6. Similarly for the getNextMonsterName() and getNextWeaponName() methods.
  • Note:The Descriptions class itself can't be run, but the DescriptionGenerator class can be (as only the latter has a main() method). Every time you run the DescriptionGenerator, it will print out twenty random rooms, each with a random description, a random monster, and a random weapon.

Testing

  1. Download Media:DescriptionGenerator.java
  2. Examine the code in Media:DescriptionGenerator.java
  3. Execute Media:DescriptionGenerator.java to see how we generate random descriptions