ulrail.gif

Lab 10: 1-D Arrays

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

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

br-2a.gif   br-2c.gif brrail.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 three 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 has a static Random variable, which is used in your Descriptions class, as well as constants for the terrain types.
  • Descriptions.java: The skeleton code, it has all the method prototypes, which must be filled in.  It doesn't have a main() method -- you should execute the DescriptionGenerator.java file instead.

Be sure to create a separate directory for this lab -- you don't want to overwrite your previous Game.java with the one for this lab.

Customize Your Game

In this lab, you will be able to customize your game.  First, think about what theme you want your game to be.  It can be the original idea of traveling across the US frontier to west coast via the Oregon trail.  Or you could travel across a space-based map, along the lines of Star Wars / Star Trek / Serenity.  Or it can be traveling across UVa's grounds.  Or Harry Potter.  Or anything else that you can imagine -- you can choose any theme that you want.

As you do think about it, there are a few categories of things that you will need to pick for your customization:

  • The party's leader.  It can be "you", or your actual name, or any other name that you want (Darth Vader, Captain Picard, John Casteen, Voldemort, etc.)
  • Other party members: this is a list of 5 other people that will travel with the leader.
  • The name of the vehicle.  In the original game, the players traveled in a Conestoga wagon.  You'll need to think about what you want for your game (Ferrari, Death Star, Starship Enterprise, etc.)
  • The name of the map.  In the original game, this would be "Western United States Frontier".  You should pick a name relevant to the theme you are choosing ("The universe", "UVa's grounds", etc.).
  • The propulsion name.  This is the thing that causes the vehicle to move.  In the original game, it was oxen; it could also be horses, rocket engines, warp nacelles, or engine cylinders.  Keep in mind that you can get multiple 'propulsion units' in your game (i.e. you can buy multiple oxen, and you will move faster as a result).
  • Health events: These are bad things that can happen to you on the way.  The original game had a number of these, such as getting dysentery or breaking an arm.
  • Depot names: These are names of depots, or perhaps cities, or other places that you can purchase items.
  • Terrain types: You can use the default terrain names of grassland / forest / mountain, or come up with something more creative.  Only three are needed.
  • Terrain adjectives: These are adjectives that describe a given terrain.  Examples are given below.

More detail about what exactly is required for each of these categories is described as this lab progresses.  Although there are 10 methods to implement, many of the methods operate the same way, so once you have written one, others should be easy.

During the course of this lab, we will primarily use two different themes: one from the original Oregon Trail game, and one modeled after the Firefly / Serenity universe.  The sample outputs at the end of this lab reflect these two themes.

Task 1: Methods that just return a String

Four of the methods just return a single String value.  This value can be kept in a String reference, or you can just have a return "foo"; as the sole line in the method body.  The four methods are:

  • public static String getLeaderName(): This method returns the name of the leader of the party ("you", "Malcolm Reynolds", etc.)
     
  • public static String getVehicleName(): This method returns the name of the vehicle that the player is traveling in ("Serenity", "Conestoga wagon", etc.)
     
  • public static String getMapTitle(): This method returns a title for what the map shows ("Western United States Frontier", "Reaver terrotory", "The 'verse", etc.)
     
  • public static String getPropulsionName(): This method returns the name of the propulsion unit ("Oxen", "Fuel cell", etc.).  The first letter of the first word should be capitalized.  In the original game, it was oxen; it could also be horses, rocket engines, warp nacelles, or engine cylinders.  Keep in mind that you can get multiple 'propulsion units' in your game (i.e. you can buy multiple oxen, and you will move faster as a result).

Once these methods are written, you can run the DescriptionGenerator class to make sure they are returning the correct values.

Task 2: Methods that return a random String

These four methods return a random String, selected from a set of Strings in an array.  For each of the methods, you will have to create a String array that is explicitly initialized to contain the various values.  Thus, you will be creating three arrays, one for each method listed below.  These arrays should go outside the methods (i.e. be part of the class).  Note that since you will be using the array from a static method, the arrays themselves must also be static.  And they should be private.

If you forget about explicit initialization, see the slide set for arrays, slides 16-17.  Each of these methods will choose a random integer (from 0 to the size of the array), and return that particular element from the associated array.  You will need to use the Game.rand object (it's of class Random -- so you use the nexInt() method).  And recall that an array's length can be found by the .length property.

All of these methods must have a corresponding array that contains the value, and must select a random value from that array -- you cannot have a big if-else-if clause or a switch statement for these methods.

Note that it is possible to have successive calls to this method return the same value -- such is the case when randomly choosing items from an array in this manner.  However, you will have to pick 10-20 values for each method, so the occurrence of duplicate choices will be reduced.

  • public static String generateRandomTerrainAdjective(): This method will return a random terrain adjective.  A few ideas are below (you are more than welcome to come up with your own).  Your method should have at least 20 adjectives.
    • 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.
       
  • public static String generateRandomDepotName(): This method will come up with possible names of markets.  A few ideas are below (again, feel free to come up with your own).  Your method should have at least 10 depot names.
  • public static String generateRandomHealthEvent(): This method will return a random health event, meaning something that can go wrong health-wise.  The original game had a number of such events, including: "has dysentery", "has a broken arm", "was bitten by a snake", "has a broken leg", "has typhoid", "has the mumps", "was mauled by a bear", and "was struck by lightning".  You should be able to stick a name in front of the event and have it be a complete sentence.  For example, "was struck by lightning" works as a health event, because you can say "Aaron Bloomfield was struck by lightning", but "lightning strike" does not work because "Aaron Bloomfield lightning strike" is not grammatically correct (unless I've become Zeus, but that's not really what we're after here).  You can have events that increase health (such as "finds wild fruit") as well.  Your method should have at least 10 health events.

Task 3: getPartyMemberName()

This method will return the names of the party members that are traveling with the leader.  Like the previous methods, you will need an explicitly initialized array (also defined as static and defined outside the method) that contains the name of the party members.  However, whereas the previous methods returned a random value from an array, this method will return successive values -- thus, you will not be able to get two party members of the same name.  The easiest way to do this is to keep track (as a static int variable) of the last value returned, so that you can return the next value when the method is called again.

You will need to choose at least 5 party member names, as the DescriptionGenerator class will try to obtain 5 such names.

Task 4: getCorrespodingHealthChange()

Once you know what health affliction one of your party members has, we will need to determine how much of a health penalty that affliction has caused.  For example, it's probably more serious to be struck by lightning than to break an arm.  Keep in mind that a person in the game will a maximum health of 10 -- so if you choose 10 (or more!) as the penalty for a given event, you will cause that person to die.  You can also have events that happen that will raise the person's health -- for example, "find wild fruit" in the original game was beneficial.

The method is defined as public static int getCorrespondingHealthChange (String event).  The parameter that it takes in will be one of the events returned by the getRandomHealthEvent() method, described above.

When you defined the getRandomHealthEvent() method, you defined an array that contained the Strings of the various health events.  For the getCorrespondingHealthChange() method, you must define a second array OF THE SAME LENGTH.  This second array will be of int values.  Each value in the first array will correspond to the value in the same position in the second array.  For example, if the 3rd value in the first array is "has dysentery", and the 3rd value in the second array is -5, then this means that when somebody has dysentery, it will cost 5 points of their health.  A negative value in the second array will be a penalty to health; a positive value in the second array will be a bonus to health.

The getCorrespodingHealthChange() method will thus need to search the first array to find that health event (remember that you cannot compare strings with ==; you must use the .equals() method).  Remember it's position.  Once found, you should return the int value in the same position in the second array.

You MUST implement this method as a searching of an array; you cannot implement it as a big if-else-if clause or a switch statement.

Task 5: getTerrainTypeName()

This method will return the types of terrain.  In the default game, they are "grassland", "forest", and "mountain".  Whatever terrain types you choose, the one that corresponds to grassland should be easy to travel through ("street", "empty space", etc.).  The one that corresponds to forest should be moderately difficult to travel through ("dorm", "nebulae", etc.).  And the one that corresponds to mountain should be very difficult to travel through ("star cluster", "crowd of people", etc.).  As we will see later in the game, it takes more time (and thus will consume more food) to travel through mountains than grassland.  Alternatively, you could make the terrain corresponding to a mountain something that should be avoided (such as in the second output run, below).

The method is defined as public static String getTerrainType (int terrainType).  The value it takes in will be one of the integer constants in the Game class: TERRAIN_UNSET, TERRAIN_GRASSLAND, TERRAIN_FOREST, and TERRAIN_MOUNTAIN.  If TERRAIN_UNSET is passed in as the parameter, then the method should just return "unset".  Otherwise, it should return the String of your choice for that terrain type.

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 (unless they occur with the getPartyMemberName() method, which would be bad).  The first execution run uses the original Oregon Trail theme.

This class will test the Descriptions class

Task 1: methods that just return a String
The party leader is: 'Aaron Bloomfield'
The vehicle name is: 'Conestoga wagon'
The map title is: 'Western United States Frontier'
The propulsion name is: 'Oxen'

Task 2: methods that return a random String
10 random terrain adjectives: sloped, wet, wet, rocky, flat, rocky, overcast, cloudy, sunny, bland
10 random depot names: Watermelon, Apple, Gourd, Potato, Watermelon, Date, Gourd, Apple, Gourd, Fig
10 random health events (and associated health penalty): 
        has typhoid (-6)
        has dysentery (-5)
        has a broken leg (-4)
        has a broken arm (-2)
        has dysentery (-5)
        was bitten by a snake (-3)
        has typhoid (-6)
        has the mumps (-4)
        has dysentery (-5)
        has dysentery (-5)

Task 3: getPartyMemberName()
5 party member names: Peeka, Mila, Gin, Tonic, Arlo

Task 4: getCorrespondingHealthChange()
This method was tested above (with getRandomHealthEvent())

Task 5: getTerrainTypeName()
Unset terrain: unset
Grassland equivalent: grassland
Forest equivalent: forest
Mountain equivalent: mountain

 

The second execution run uses the Firefly / Serenity theme.

This class will test the Descriptions class

Task 1: methods that just return a String
The party leader is: 'Malcolm Reynolds'
The vehicle name is: 'Serenity'
The map title is: 'The 'verse'
The propulsion name is: 'Fuel cells'

Task 2: methods that return a random String
10 random terrain adjectives: dry, overcast, rocky, overcast, deserted, overcast, dry, dry, dry, dry
10 random depot names: Lilac, St. Albans, Verbena, Miranda, Dyton, Londinium, Santo, Bellerophon, Sihnon, Londinium
10 random health events (and associated health penalty):
        was knocked out (-3)
        was tortured by the bad guys (-5)
        was tortured by the bad guys (-5)
        was slashed by River (-2)
        was attacked by Reavers (-8)
        was tortured by the bad guys (-5)
        was shot (-5)
        had their amygdala stripped (-4)
        was attacked by Reavers (-8)
        was stabbed (-2)

Task 3: getPartyMemberName()
5 party member names: Zoe, Wash, Kaylee, Jayne, Inara

Task 4: getCorrespondingHealthChange()
This method was tested above (with getRandomHealthEvent())

Task 5: getTerrainTypeName()
Unset terrain: unset
Grassland equivalent: empty space
Forest equivalent: alliance patrol
Mountain equivalent: Reavers!

Submission

When you are finished, you just need to submit the Descriptions.java file.  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 8.

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