ulrail.gif

HW 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

Course Project

This homework is a continuation of the course project, which is to implement a modified version of the Oregon Trail computer game.  You will be implementing the Party (1D arrays) and Vehicle classes which are very different from the classes that you have previously implemented for this project. 

  1. Writing the Party class will give you practice working with and accessing elements in single-dimensional arrays. 
  2. Writing the Vehicle class will give you more practice in writing classes.

So far, you have already implemented half (6 of the 12 classes) that make up our version of the Oregon Trail computer game.  This includes the following classes:

  • Location (hw 7): This class represents a location on the map.  A Location has a given type of terrain, and can have a Depot.
  • Person (hw 7): This class represents a single person in the game.
  • Depot (hw 8): This is a "market" (fort, etc.) that can occur at a location.  You can buy and sell goods at a Depot.
  • Inventory (hw 8): This class represents the inventory that holds items.  Every depot, as well as the player's Vehicle, will have an inventory.  It holds such things as food, money, and oxen.
  • Parser (lab 9): This class is in charge of obtaining all user input.  No user input is read in in any other class.
  • Descriptions (lab 10): This class allows you to customize the game -- you choose the names for various things in the game, such as the party members, the name of the vehicle, etc.
  • Game (all assignments): This class has the main() method that will run the game. So far, you have been using the Game class to test most of your class implementations.

In this assignment you will implement the Party and Vehicle classes.

  • Party (hw 9): This class represents a party of people that are traveling.  There will be 5 or so people per party (i.e. you are leading a group of 5 or so people).
  • Vehicle (hw 9): This class represents the player's vehicle.  The vehicle contains the party (i.e. all the people that are traveling), as well as an Inventory (how much food, etc. the player has).

After that, there will only be 3 more classes beyond Game.java that remain to be implemented:

  • Game (all assignments): This class has the main() method that will run the game.
  • Map (hw 10): The 2-D grid that composes the board of the game.  It's a 2-D grid of Locations.
  • MapPrinter (lab 11): This class will print out the Map to the screen.
  • Control (hw 10): This class provides a number of methods that allow the game to progress (such as handling the turns, ending the game, etc.)

Purpose

For this homework, you are responsible for implementing the Party and Vehicle classes.  You must submit these two files, along with the Game.java file.  We are not providing skeleton code for Party.java, but are providing a portion of Vehicle.java Please note that we ask you to add comments to the move() method which we provide for you.  Your comments should demonstrate that you understand what the method is doing.

Getting Started

Most of the classes in this project are dependent on other classes.  For that reason, you will need to download skeleton code for a few of the other classes to complete this homework.  Please note that this code will not compile without error until you implement the Party class.  If you have completed the party class and are still getting compiler errors from the skeleton code, please see a TA in office hours:

  • Control.java:  This is an incomplete version of Control.java that has dummy methods needed by Party and Vehicle classes.  These dummy methods will allow you to implement and test the Party and Vehicle classes.  Please note that you will need to uncomment some code in Control.java AFTER you've implemented the Vehicle class.  There are comments in the code that point out which lines these are.
  • Vehicle.java:  This is a skeleton framework for the Vehicle class for you to use.  We've given you the implementations for the accessor and mutator methods. You should use this base to implement the rest of the class.
  • Game.java:  This is the file containing game constants and main().  Please see and follow the directions in the comments in main(). You will have to uncomment some lines of code AFTER you implement your Vehicle class.  Comments are included in Game.java that point out these lines.

In addition to this, you will need the following classes.  You may use your own classes that you have developed in previous homeworks or labs, or download the ones below to use. See the course project documentation site for details of the properties and methods available in each class. (NOTE: If you choose to use your own classes, you will need to make sure that your classes address the issues in project errata list.  Please note that your previous assignments/labs will not be penalized for not addressing these issues.)  Also, if you are having strange bugs in your code that you cannot figure out, we suggest you use our pre-compiled versions here.

Game class

We provide a Game.java file, which includes a number of fields that you will need for the homework, including a number of constants that will be used in this project.

main() method

You will have to modify the main() method, as described below.  You should read the homework through before you begin, as completing the main() method will be easier if done while developing your Party and Vehicle classes.

Party class

The first class you must implement for this homework represents the group that is making the journey across the frontier.  As with the Vehicle class, nothing should be static, all fields must be private, and (unless specified otherwise), all methods public.  Your Party class needs to be declared public, and in a Party.java file.

All fields must be private.  Unless specified otherwise, all methods must be public.

Properties

The Party class has a number of instance properties:

  • Person [] theParty: This will hold the people who are members of the party. The default for each entry is null.  Note that the size of this array may very well be different than the number of people in the party.  For example, the array may be of size 5, but there are only 3 people in the party -- in this case, the last two spots in the array would be null.
     

  • int currentPartySize: This will keep track of the number of people in the party.  The default is 0.

Constructors

There is one constructor for this class.

  • Party(): This constructor will do the following
    • Initialize theParty to an array of Person of the default party size (See the constants list, grouped by class).  After the constructor is called, theParty should contain DEFAULT_PARTY_SIZE null Person references.  You must use DEFAULT_PARTY_SIZE as the initial size of the array (and not the numerical value 5), as this is how we are going to test your code.

Please make sure you understand what this constructor does.

Accessors and Mutators

The Party class is different from the other classes you have previously implemented for the course project.  Your Party class needs to have an accessor for the currentPartySize property of the Party class, but does not need to have a mutator for currentPartySize.   This accessor must be called getCurrentPartySize(), which follows the standard naming scheme as discussed in lecture.  You do not need to implement a mutator or accessor method for theParty.

Other Methods

There are ten other methods that are required for this class.  The only method which should be private is increaseCapacity(); all other methods should be public.

  • void increaseCapacity():  This is the only private method in the Party class.  It is called when adding a Person to the Party and there is not enough space left in the array to do so.  This method allocates a new array of Person that is double the size of the current theParty array. It then copies the existing Party members into it.  The last action that is performed is that the Party's private array of Person, theParty, is pointed at the newly created array.  This process is discussed in the lecture notes (the section on the Vector class).
     
  • Person getPerson(int position):  If a valid position is given, this method returns the Person object at the specified position.  If an invalid position is passed, print an error message and return null.  An invalid position is anything greater than the number of people in the party (which may be different than the size of the array).
     
  • void addLeader(String name): The leader always occupies position 0 in the Party.  If the leader has not already been added, then this method creates a new Person object with the passed parameter, name, adds that object to spot 0 in the array, and updates currentPartySize to reflect this addition. If a leader already exists, a message is printed to notify the player, and the additional leader is not added.
     

  • void addPartyMember(String name): Party members can only be added to positions 1 or greater, since position 0 is reserved for the leader.  Further, party members may not be added before a leader is added to the party.  If these two previous conditions are satisfied, then the addPartyMember() method checks to see whether theParty array is full, and if so, first increases the capacity of theParty before adding a a Person with the passed name. Once the Person is added, currentPartySize is updated to reflect the addition of another Person.
     
  • int getNumberAlive(): This method counts and returns the number of people in the Party who are still alive.  Recall that each Person in the array has a getIsAlive() method.
     
  • boolean isAnybodyAlive(): This method will return true if any member of the Party is alive.  It examines each Person in the Party and determines whether or not they are alive. If no member in the Party is alive, then a value of false is returned; if at least one person is alive, then true is returned.  Recall that each Person in the array has a getIsAlive() method.
     
  • Person pickRandomPerson(): This method generates a random integer value between 0 and the size of the Party and returns the Person at that position. (The random number can be generated using Game's random variable and using the nextInt() method.  For example, if the random number generated was 0, then the leader of the Party would be returned.
     
  • void starve(): This method reduces the health of each Party member by STARVATION_HEALTH_COST.  (This constant is defined in the Game class). 
     
  • String toString(): This method creates a string that represents the status of the entire Party.  It should create a String where each Person in the Party's information (name and health status) is on a separate line starting with the leader.  For example, it could print in the following format:
  • 	Aaron Bloomfield with health 10/10
            Assaf Sternberg with health 10/10
            Trevor Perrier with health 10/10
            Derek Thomas with health 10/10
            Nick Krainak with health 10/10
  • void printPartyStatus(): This method prints the name and health status (health value and alive/dead status) of each Party member.


Vehicle Class

The second class you must implement is the Vehicle class.  We are providing you with a few methods in the Vehicle class, in the skeleton code for Vehicle.java.  The Vehicle is the mode of transportation for the party.  Vehicles also have an inventory and a location associated with them.  Please note that we are asking you to add comments to the move() method which we have provided.  Your comments should demonstrate that you understand how the method is written and what it is doing.  You will be graded on this, and points will be deducted if you do not add comments.

No methods should be static.  Unless specified otherwise, all methods must be public.

Properties

The Vehicle class has several private properties:

  • Party theParty:  This is the group that travels in the Vehicle.  It initially starts as empty.
  • String vehicleType: This is a string description of the type of Vehicle (for example, "Conestoga Wagon").   It is initially a blank string.
  • Location myLocation: This represents the location that the Vehicle is currently in.  The default is null.
  • Inventory myInventory: This is the inventory of goods that the Vehicle has available to it.  The default is the object created by the default Inventory constructor.
  • double totalDistanceTraveled: This is the odometer for the Vehicle and keeps track of the total distance that the Vehicle has traveled.
  • double totalTimeTaken: This property keeps track of how much time is spent either traveling or resting during the game.

Constructors

There are two constructors:
  • Vehicle(): This is the default constructor.  It creates a Vehicle with all of the default values listed above.
  • Vehicle(int partySize): This is a specific constructor which creates a Vehicle with a party of a particular size. It initializes its party by first calling its addLeader() method, then repeatedly calling its addPartyMember() method until partySize members have been added to the Vehicle's party.  Names passed to addLeader() and addPartyMember are generated by using the getLeaderName() and getPartyMemberName() methods that were written in the Description class.

Accessors and Mutators

To allow you to focus on implementing the other methods, we have given you the implementations for the accessor and mutator methods.

Other Methods

There are several methods that you will implement for the Vehicle class.  You will need to call methods from classes that you have not yet implemented, namely Map, MapPrinter, and Control classes.  Here is a link to the course project documentation which provides some explanation of how the methods for the classes work.  You should consult with this documentation if you've forgotten which methods are available for the various classes.

  • void addLeader(String name): This method will simply call Party's addLeader() method to add a leader to the Vehicle's party.
     
  • void addPartyMember(String name): This method will call Party's addPartyMember() method to add a party member who is not the leader to the party.
     
  • Person getLeader(): This method returns the leader of the party. 
     
  • double getMoney(): This method returns the amount of money in the vehicle's inventory.
     
  • void setMoney(double money): This method will set the money in the inventory to the amount specified by money.
     
  • void addToMoney(double amount): This method adds the amount of money to the existing money in the inventory by updating the inventory's money total to its current value plus amount
     
  • void subtractFromMoney(double amount): This method is very similar to addToMoney(), except that it reduces the amount of money in the vehicle's inventory by amount.
     
  • void move(): The move() method takes care of the tasks involved with moving the Vehicle.  We have provided the implementation for this method.  You must add comments to it to demonstrate that you understand what actions are being performed.  Since there are several calls to methods outside of the Vehicle class, here are some links to the course project documentation site, and a list of associated methods that may help you in explaining some of the code:
    1. MapPrinter: printMap()
    2. Parser: getMoveDirection()
    3. Game: BOARD_HEIGHT, BOARD_WIDTH
    4. Map: getLocationAt(), setVisibility()
    5. Control: advanceTurn (), possiblyEndGame(). Due to inter-class dependences, the possiblyEndGame() method that we provide you with in Control.java only prints out a statement saying that possiblyEndGame() was called. You do not need to change this code.  possiblyEndGame() is a method which determines whether the game should end.
       

Game class, Revisited

Your main() method in the Game.java file should test all of the methods that you implement.  The basic idea is to call a method, and verify that it does what it was supposed to do by calling an accessor method.  For example, by calling the Party's specific constructor, it is supposed to create a Party with a particular number of members.  You will need to verify this by calling the appropriate print or accessor methods (depending on what you are testing), and checking to see that they return the right values.  You can use specific values for these tests -- i.e., you can create a Vehicle whose vehicleType is set to a String "X-Wing Fighter", and then check to see that the vehicleType of the Vehicle is indeed "X-Wing Fighter".

Where to Start

Work on the methods one by one, making sure that each one works before moving onto the next one.

While the main() method may seem like a lot of work, there are a couple of things that will make it much easier.  If you add the code to Game.java as you go along, it will make it much easier.  The idea is to implement a method or two, and then add the test code to Game.java.  Then make sure it compiles and works properly.  Then go onto adding more methods.  If you get stuck in a method (i.e. can't get it to work, it won't compile, etc.), then move onto another one, and come back to the one that is giving you problems.

This homework will be much easier if you work through it one method at a time.  Write the first method, and then sufficiently test it in your main() method.  Then write the second method, and sufficiently test it in your main() method.  This will make the entire homework much easier, and it will take much less time to complete.

Good Programming Practices

All the good programming practices from HW J1 should be included here as well.  Since you are providing code in the main() method of the Game class to test your other code, you don't need to include execution runs as comments.

Submission

When you are finished, submit the Party.java, Vehicle.java, and Game.java files.

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