ulrail.gif

Lab 10: 1-D Arrays

  ur-2c.gif urrail.gif
blrail-nonavbar.gif

Home | Resources | Homeworks | Exams
Slides | Labs | Contacts | Submit | TAs

br-2a.gif   br-2c.gif brrail.gif
spacer.gif spacer.gif spacer.gif

Objective

Arrays are one of the ways that Java allows for storing items in a list.  In this lab, we will gain experience creating and manipulating arrays.

Files

There are two files that you need to download for this lab.

  • DescriptionGenerator.java: This file is used to test your Descriptions class.  You should execute this file, not Descriptions.java.  Use of this file is explained below.
  • Game.java: This file just has a static Random variable, which is used in your Descriptions class.

You will need to create and submit a Descriptions.java file, for which we are not providing skeleton code.

Customize Your Game

There are three lists that you will need to generate for this assignment: location names, location descriptions, and cargo names.  Your choice will allow you to customize your game.

Location names

The first task you must choose is what names you would like for the locations.  We have a number of ideas below, but feel free to choose your own.  You must have about 20 locations names for this game to work properly.

Location descriptions

Each location will have a single adjective that describes that location.  You should have about as many adjectives as locations (i.e. at least 20). Note that it's not necessary to have all of the adjectives be from a single category, like "weather".

  • Weather adjectives: "sunny", "cloudy", "rainy", "hot", "cold", "windy", etc.
  • Color adjectives: "red", "blue", "green", etc.
  • Size adjectives: "large", "small", "tiny", etc.
  • Terrain adjectives: "mountainous", "forested", "hilly", "oceanic", etc.
  • Population adjectives: "crowded", "heavily populated", "remote", etc.
  • Interest level adjectives: "boring", "interesting", "exciting", "wild", "lonely", "dull", "bland", etc.

Cargo names

Lastly, you will need to come up with names of cargo.  For example, you could buy and sell course textbooks at the UVa dorms.  A few ideas of cargo types are listed below.  You need to come up with at least 15 cargo types.

  • Precious metals / gems: "gold", "silver", "diamonds", etc.
  • Non precious metals: "aluminum", "steel", etc.
  • Natural resources: "wood", "coal", "water", "oil", etc.
  • Textbooks: for any subject you can think of
  • Manufactured items: "toys", "machines", "computers", "robots", "games", "cloth", "furniture", etc.
  • Refined items: "fuel", "gasoline", "plastic", etc.
  • Miscellaneous: "medicine", "waste", etc.

If you make up cargo types that are illegal (i.e. drugs), you will get a zero for this lab.  We don't want to get fired because the class made up a game to buy and sell drugs... :^)

The Descriptions Class

All the members of the descriptions class are to be static -- thus, we need no constructors, accessors, or mutators.

Attributes

  • appearances (String[]): this array should be explicitly initialized to the appearances you came up with above.  If you forget what explicit array initialization is, you can see slide 16 of the lecture notes on arrays.
     
  • locationNames (String[]): this array should be explicitly initialized to the list of location names you came up with above.
     
  • cargoTypes (String[]): this array should be explicitly initialized to the cargo types you came up with above.
     
  • cargoCostFactors (double[]): this array holds the relative cost factors of each Cargo type.  For example, gold is more expensive than water, so it will have a higher cost factor (although it has a similar name, this has nothing to do with the Market cost factors).  If there are 20 elements in your cargoTypes array, then there needs to be 20 elements in this array -- the 5th element in this array is the cost factor for the 5th element in the cargoTypes array.  Cost factors should range from 0.0 to 10.0, for example (although none should be 0.0). You should make reasonable guesses here for the relative value of each Cargo type, but it's not necessary to be truly accurate for real-world scenarios.

Methods

There are six methods that you need to create for this class.  All of the methods should be public and static.

  • String getAppearance(): this method will return a random appearance.  From the list of appearances in the appearances[] array, one should be selected at random.  To do this, you can use the nextInt() method in the Random class.  The Random class is in the java.util library, so you will have to have that import line at the top of your Descriptions.java file.  The Game.java file has a Random object (Game.rand) already declared for your use, so you can use this one instead of creating your own.
     
  • String getLocationName(): this method should return a random location name, and is similar in function to the previous method.
     
  • String getCargoType(): this method should return a random cargo type, and is similar in function to the previous two methods.
     
  • int getCargoTypeCount(): this method returns the number of elements in the cargoTypes[] array.
     
  • int getLocationNameCount(): this method returns the number of elements in the locationNames[] array.
     
  • double getCargoCostFactor(String cargo): this method returns the cost factor for the passed cargo type.  For example, if the passed String is "gems", then this method must first find that element in the cargoTypes[] array (if not found, return -1.0), and the same element index should then be returned from the cargoCostFactors[] array.

DescriptionGenerator

This class will test all the methods in your Descriptions class.  A sample execution output appears below. Don't worry if duplicate entries occur.

There are 31 locations defined.
There are 17 cargos defined.

rainy Greenleaf has Cloth to sell at a cost factor of 1.0
heavily populated Georgia has Furniture to sell at a cost factor of 2.0
heavily populated Ariel has Medicine to sell at a cost factor of 3.0
mild Haven has Fuel to sell at a cost factor of 4.0
sunny Persephone has Medicine to sell at a cost factor of 3.0
heavily populated Whitefall has Cloth to sell at a cost factor of 1.0
crowded Georgia has Furs to sell at a cost factor of 2.0
sunny Persephone has Food to sell at a cost factor of 2.0
hilly Persephone has Furs to sell at a cost factor of 2.0
dull Ezra has Furs to sell at a cost factor of 2.0
rainy Ariel has Water to sell at a cost factor of 1.0
bland Athens has Cloth to sell at a cost factor of 1.0
dull Dyton has Platinum to sell at a cost factor of 10.0
wet Greenleaf has Robots to sell at a cost factor of 6.0
mountainous Regina has Robots to sell at a cost factor of 6.0
rainy Ariel has Silver to sell at a cost factor of 4.0
dull Triumph has Java textbooks to sell at a cost factor of 100.0
forested Verbena has Cloth to sell at a cost factor of 1.0
sunny Ita has Waste to sell at a cost factor of 1.0
sunny St. Albans has Water to sell at a cost factor of 1.0

Submission

When you are finished, you just need to submit the Descriptions.java file.  Note that there are a few more questions that are asked for this lab submission.  As the DescriptionGenerator.java file was not modified, it does not need to be submitted.

If you are done early....

If you finish this lab early, use any remaining time to work on HW J7.

spacer.gif
spacer.gif footer-middle.gif spacer.gif