ulrail.gif

Lab 9: 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 one to store items in a list.  In this lab, we will gain experience creating and manipulating arrays.  You will develop the Descriptions class, which will be used to generate ship names, ship adjectives, and ship home ports for the ships used in your game. 

This lab is a part of the course project, which is to implement a computer-based version of the game Battleship.  If you are not familiar with the game, then you should read the article at that link.

Once you are done with this lab, you can use this code in your HW J6, instead of the Descriptions.class file that we provide.

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 (the latter does not have a main() method).  Use of this file is explained below.
  • Game.java: This file is just used for it's static Random variable, which is needed in your Descriptions class.  It is NOT the same as the Game.java used in HW J6, and thus should be kept in a separate directory.

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

Customize Your Game

There are a few String lists that you will need to generate for this assignment: ship adjectives, ship types, and ship home ports.  In addition, you will need to determine the length of each of your ships.  Since each player will get 5 ships, you must have at least 5 ship names.  10 is probably better.  Feel free to have more, if you want.

Ship Types

You will need to come up with the ship types that you want to use in your game.  The ship types used in the traditional Battleship game are: Destroyer, Cruiser, Submarine, Battleship, and Carrier.  But be a bit creative!  It need not be limited to marine ships.  A few ideas:

  • Actual naval ship types, used in the original game (Destroyer, Cruiser, Submarine, Battleship, Carrier)
  • Hand-powered ships (rowboat, kayak, canoe, etc.)
  • Other floating things (surfboard, life preserver, jetski, etc.)
  • Sail boats (Sloop, Cutter, Catboat, etc.)
  • Automobile names (DeLorean, Porsche, Honda, Kia, etc.)
  • Space Shuttles (Enterprise, Atlantis, Discovery, Buran, etc.)
  • Star Wars ships (X-Wing, Millennium Falcon, Tie Fighter, etc.)

Each ship type must be a single word!  We see why in a bit, but if you use multi-word adjectives (i.e. "big and tall"), your extractShipType() method will not work properly.

Location names

The next 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 should have about 10 locations names for this game to work properly.

Ship adjectives

Each ship will have an adjective to describe it.  You should have about as many adjectives as locations (i.e. around 10). Note that it's not necessary to have all of the adjectives be from a single category, like "color".

  • Color adjectives: red, blue, green, etc.
  • Size adjectives: large, small, tiny, etc.
  • Interest level adjectives: boring, interesting, exciting, lonely, dull, bland, etc.
  • Size adjectives: tall, big, small, tiny, etc.
  • Speed adjectives: fast, slow, etc.
  • Miscellaneous adjectives: mighty, imposing, etc.

Each adjective must be a single word!  We see why in a bit, but if you use multi-word adjectives (i.e. "big and tall"), your extractShipType() method will not work properly.

Ship Lengths

In a standard Battleship game, there are 5 ships, of lengths 2 through 5, with two ships having length 3.  You are welcome to use your own lengths (as long as they are greater than zero), but you might want to stay with those lengths.

The Descriptions Class

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

Attributes

  • shipAdjectives (String[]): this array should be explicitly initialized to the adjectives you came up with above.  If you do not know what explicit array initialization is, you can see slides 15-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.
     
  • shipTypes (String[]): this array should be explicitly initialized to the ship types you came up with above.
     
  • shipLengths (int[]): this array holds the corresponding ship lengths of the ships listed in the shipTypes[] array.  Thus, these two arrays must be the same size.  To find the length of the ith ship type in the shipTypes[] array, find the ith integer in the shipLengths[] array.

Methods

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

  • String getShipAdjective(): this method will return a random adjective.  From the list of appearances in the shipAdjectives[] 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 (called Game.rand) already declared for your use, so you need to use this one instead of creating your own.  There is a further description here about how to generate random numbers.  Remember that the length of an array (i.e. the number of elements in it) can be found by using shipAdjectives.length.
     
  • String getShipLocation(): this method should return a random ship home port name (from the locationNames[] array), and is similar in function to the previous method.
     
  • String getShipType(): this method should a successive ship type, from the shipTypes[] array.  Note that this is not a random name.  More details on this below.
     
  • String getNextShipName(): this method will choose one adjective (via getShipAdjective()), one location (via getShipLocation()), and one ship type (via getShipType()).  They should then be concatenated together to form a single String of the form, "the mighty kayak from Charlottesville".
     
  • String extractShipType (String desc): this method will take in a full ship description, such as, "the mighty kayak from Charlottesville", and extract "kayak" from that.  This is described in more detail below.
     
  • int getShipLength(String desc): this method will be passed in a ship description (such as, "the mighty kayak from Charlottesville").  It will first extract the ship type (via a call to extractShipType()), then look up that element in the shipTypes[] array.  Once it finds the location of the ship type in the shipTypes[] array, it must return the integer at the same position in the shipLengths[] array.  Remember to use the .equals() methods for String comparison, not ==.

getShipType()

The idea is to cycle through all the ship types in the shipTypes[] array, and then start again at the beginning of the list of ship types.  To do this, you will need to keep track of the last ship type returned via a static int variable, which holds which element in the array is to be returned next.  Each time this method is called, the variable should be incremented (and you don't want to go beyond the bounds of the array).  This will allow you to cycle through the elements of the array.

It turns out this can be specified in a single line.  Assume that nextShip is an integer class variable initially set to zero.  Then the following line will accomplish this:

return shipTypes[nextShip++ % shipTypes.length];

Do you understand why this works?  It's a great exam question!

extractShipType()

This method is passed in a String such as, "the mighty ocean kayak from Charlottesville".  We must then extract the ship type -- "kayak" -- from the passed String.  To do this, we will use the split() method in the String class.  The idea of this method is to pass in a delimiter -- in our case, a space.  This method will then return an array out of the String, splitting it at each space.  Thus, if the String desc is "the mighty kayak from Charlottesville", and you call desc.split(" "), this method will return an array that contains five elements: "the", "mighty", "kayak", "from", and "Charlottesville".  This is why the adjectives and ship types must be single words -- if you put spaces more in there, you won't know which element in the array is the ship class.

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.

A test of the Descriptions class

5 ship adjectives:
dirty
dirty
black
imposing
tiny

5 ship locations:
Lilac
Dyton
Higgin's Moon
Heinlein
Bernadette

5 ship types:
Destroyer
Submarine
Cruiser
Battleship
Carrier

5 full fledged ship names:
the green Dinghy from Heinlein
the tiny Rowboat from Newhall
the black Canoe from Beaumonde
the broken Kayak from Santo
the tiny Raft from Ezra

5 tests of extractShipType():
the dirty Destroyer from Verbena: Destroyer
the slow Submarine from Triumph: Submarine
the tiny Cruiser from Regina: Cruiser
the wimpy Battleship from Beylix: Battleship
the black Carrier from Londinium: Carrier

5 tests of everything:
the fast Destroyer from Hera has length 2
the mighty Submarine from Santo has length 3
the blue Cruiser from Verbena has length 3
the purple Battleship from Dyton has length 4
the broken Carrier from Bellerophon has length 5

Hints on where to start

The DescriptionGenerator.java can be used to test all of your methods.  Focus on one method, such as getShipAdjective().  The bodies of the for loops are commented out -- remove the '//' for each for loop body to test each method.  Thus, you should implement one method, un-comment the corresponding for loop body, and make sure it works before moving onto the next one.

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 J6.

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